【发布时间】:2019-11-25 18:29:22
【问题描述】:
我的 android 应用中有一个导航抽屉。代码如下:
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:fitsSystemWindows="true"
android:background="@color/white"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu"
app:itemTextColor="@color/drawer_item"
app:itemIconTint="@color/drawer_item"
app:itemBackground="@drawable/nav_divider"/>
我已使用post 的答案在项目之间添加分隔符。这是我在NavigationView 中用作itemBackground 的nav_divider 可绘制对象:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="@dimen/activity_horizontal_margin">
<shape android:shape="rectangle">
<solid android:color="@color/light_gray"/>
</shape>
</item>
<item android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="@color/white"/>
</shape>
</item>
</layer-list>
现在,问题是如何更改所选项目的背景颜色。我知道如何在没有分隔符的情况下做到这一点,但是使用分隔符会更复杂。
更新:
这是我尝试过的:
我创建了另一个名为 nav_divider_selected 的可绘制对象,它与上面的 nav_divider 完全相同,只是颜色不同。
然后,我像这样创建了一个drawer_item_background 可绘制对象:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/nav_divider_selected" android:state_selected="true"/>
<item android:drawable="@drawable/nav_divider"/>
</selector>
并且,用这个新的选择器 (app:itemBackground="@drawable/drawer_item_background") 替换了 NavigationView 的 itemBackground。但是,它没有用。出于某种原因,这些物品看起来都是黑色的。
【问题讨论】:
-
您是要突出显示顶部和底部分隔线还是只突出底部,您能否发布您想要的最终结果应该是什么样子的屏幕截图
-
我不想突出显示分隔线。我希望突出显示所选项目。我认为不需要截图,是吗?
-
你用选择器试过了吗?
-
我知道应该用选择器来完成,但是选择器应该长什么样子?
-
你能发布你到目前为止所尝试的内容吗
标签: android navigation-drawer android-navigationview