【问题标题】:android toolbar: how to have a toolbar with image in the background and menu items on topandroid工具栏:如何在背景中有一个带有图像的工具栏和顶部的菜单项
【发布时间】:2017-11-08 20:35:41
【问题描述】:

使用材料设计如何创建带有背景图像的工具栏。

以下是我想要的:

我正在尝试以下代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/actionBar"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:contentInsetEnd="0dp"
    app:contentInsetStart="0dp" 
    android:minHeight="200dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageViewplaces"
        android:src="@drawable/puri" 
        android:layout_gravity="center"/>

</android.support.v7.widget.Toolbar>

我得到的是:

【问题讨论】:

  • 尝试使用'background'属性而不是'src'?
  • 我试过了。一样的。图片不覆盖整个工具栏区域。
  • android:layout_width="match_parent" 用于ImageView,因为工具栏是match_parent,以便在整个工具栏上应用图像。您应该将 ImageView 宽度修改为match_parentwrap_content会根据ImageView中提供的图片显示图片的宽高
  • 我试过静止图像不占据整个工具栏。首先是不是可能。在工具栏中相对放置菜单项和子视图

标签: android android-toolbar


【解决方案1】:

如果您坚持使用 ImageView 而不是使用 .setBackground(...) 函数。

您可以尝试使用RelativeLayoutToolbar 覆盖ImageView,如下所示:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageViewplaces"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/YOUR_IMAGE" />

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar_actionbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

    </android.support.v7.widget.Toolbar>


</RelativeLayout>

【讨论】:

  • 在哪里提到工具栏的高度
  • ImageView 的 android:adjustViewBounds="true" 属性将缩放图像以适应窗口。这意味着保持源图像的宽度和高度之间的关系,同时 layout_width 被拉伸到 match_parent 巫婆在这种情况下是屏幕的宽度
【解决方案2】:

不是将带有图像的元素添加到工具栏,而是将图像添加为工具栏的背景,代码如下。

<android.support.v7.widget.Toolbar     
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/actionBar"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:contentInsetEnd="0dp"
    app:contentInsetStart="0dp"
    android:background="@drawable/puri"
    android:minHeight="200dp">

    </android.support.v7.widget.Toolbar>

如果你想在java代码中改变它,你可以调用toolbar.setBackground(ContextCompat.getDrawable(yourcontext, R.drawable/puri);

【讨论】:

  • 我可以这样添加。我知道。但我想使用 imageview 添加它。我希望图像是一个单独的视图。这样我就可以轻松地使用裁剪和缩放
  • 好吧,对不起。对于 ImageViews,您可以使用值 centerCrop 或其他值设置 scaleType 属性,您是否已经尝试过?还有,你为什么将layout_width 设置为wrap_content
【解决方案3】:

更合适的解决方案是使用AppBarLayout

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbarlayout"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:src="@drawable/bg"
        android:scaleType="centerCrop"/>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
    </RelativeLayout>
</android.support.design.widget.AppBarLayout>

【讨论】:

    猜你喜欢
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多