android工具栏:如何在背景中显示带有图像的工具栏和顶部的菜单项

San*_*idi 11 android android-toolbar

使用材质设计如何使用背景图像创建工具栏.

以下是我想要的:

带图像和溢出的工具栏和搜索菜单图标.

我正在尝试以下代码:

<?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>
Run Code Online (Sandbox Code Playgroud)

而我得到的是:

在此输入图像描述

RWI*_*WIL 12

如果您坚持使用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>
Run Code Online (Sandbox Code Playgroud)


W.K*_*K.S 6

更合适的解决方案是使用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>
Run Code Online (Sandbox Code Playgroud)


yen*_*rah 5

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

<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>
Run Code Online (Sandbox Code Playgroud)

如果要在Java代码中更改它,可以调用 toolbar.setBackground(ContextCompat.getDrawable(yourcontext, R.drawable/puri);