java - 带背景图片的抽屉导航

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

最佳答案

将你的 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>

关于java - 带背景图片的抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42927189/

相关文章:

android - 什么时候应该调用 super.onResume()?

Android 检查 wifi 连接

java - 如何将以字符串表示的度分秒位置转换为 double

java - 为什么 subsequence(a,b).toString() 比 substring(a,b) 快?

Java - 我的字母顺序排序算法并不总是按预期工作

android - 无法在未调用 Looper.prepare() 的线程内创建处理程序

android - 控件显示在设计器中但不显示在模拟器中

java - Eclipse 静态元素 : Unable to Call/load CSS file from Xhtml

java - 将 2-3-4 树转换为红黑树

android - 与其他 fragment 通信 - 为什么最佳实践改变了