我想在菜单后面的抽屉背景中有一张图片。在这段代码中,图像在菜单前面。我不确定,但在这种特殊情况下我不能使用 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>
最佳答案
将你的 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/