NavigationDrawer与背景图像

Ful*_*xel 3 java android

我想在菜单后面的抽屉的背景中有一个图像.在这段代码中,图像位于菜单前面.我不确定,但在这种特殊情况下我不能使用FrameLayout.

图像也应保持其纵横比,半透明颜色应保持在其前面.这种颜色由@ color/menuSemi提供

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/content_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        android:background="@color/menuSemi"
        app:itemTextColor="@color/white"
        app:itemIconTint="@color/white"
        android:paddingTop="16dp"
        style="@style/AppTheme.navigation"
        app:menu="@menu/activity_main">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start">

            <ImageView
                android:id="@+id/drawer_bg"
                android:src="@drawable/sidebar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:alpha="1"
                android:scaleType="centerCrop"/>
        </RelativeLayout>

    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

此标记的结果

Kar*_*lsi 10

把你NavigationViewRelativeLayout和设置RelativeLayout gravity "start",并添加ImageView里面Relativelayout的第一个元素.

并且,设置NavigationView背景透明

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <ImageView
            android:id="@+id/drawer_bg"
            android:src="@drawable/sidebar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="1"
            android:scaleType="centerCrop"/>
<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    android:background="@android:color/transparent"
    app:itemTextColor="@color/white"
    app:itemIconTint="@color/white"
    android:paddingTop="16dp"
    style="@style/AppTheme.navigation"
    app:menu="@menu/activity_main">

</android.support.design.widget.NavigationView>

    </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

所以,你的布局现在看起来像: -

     <?xml version="1.0" encoding="utf-8"?>
     <android.support.v4.widget.DrawerLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/content_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
 <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start">

            <ImageView
                android:id="@+id/drawer_bg"
                android:src="@drawable/sidebar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:alpha="1"
                android:scaleType="centerCrop"/>
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        android:background="@color/menuSemi"
        app:itemTextColor="@color/white"
        app:itemIconTint="@color/white"
        android:paddingTop="16dp"
        style="@style/AppTheme.navigation"
        app:menu="@menu/activity_main">

    </android.support.design.widget.NavigationView>

        </RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)