NavigationDrawer与背景图像

时间:2017-03-21 12:28:41

标签: 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>

Result of this markup

4 个答案:

答案 0 :(得分:10)

NavigationView放入RelativeLayout并设置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>

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

     <?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>

答案 1 :(得分:2)

您不应将背景设置为新的ImageView。您应该将其作为NavigationView的background

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@drawable/sidebar"
    ...

答案 2 :(得分:0)

将图像作为背景

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:background="@drawable/sidebar"

设置

layout_width和layout_height作为换行内容或匹配父级。

或单独导出标题文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/view_container"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:gravity="bottom"
    android:orientation="vertical"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    >

  <ImageView
      android:id="@+id/img_header_bg"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:scaleType="fitXY"
      android:src="@mipmap/ic_launcher"
      />

...

答案 3 :(得分:0)

在您的NavigationView中只需更改背景颜色

android:background="@color/menuSemi"

To Background Drawable

android:background="@drawable/YOUR_IMAGE_NAME"

要使图像半透明,请使用活动中的以下代码。

YOUR_NAVIGATION_VIEW.getBackground().setAlpha(50);