简体   繁体   English

具有选定背景颜色的导航抽屉项目分隔符

[英]Navigation Drawer item divider with selected background color

I have a navigation drawer in my android app.我的 android 应用中有一个导航抽屉。 Here's the code:这是代码:

<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"/>

I've used one the answer to this post to add dividers between the items.我已经使用了这篇文章的答案在项目之间添加了分隔线。 Here's the nav_divider drawable that I'm using as itemBackground in my NavigationView :这是我在NavigationView用作itemBackgroundnav_divider可绘制itemBackground

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

Now, the question is how do I change the background color of the selected item.现在,问题是如何更改所选项目的背景颜色。 I know how to do that without the divider, but with the divider, it's more complicated.我知道如何在没有分频器的情况下做到这一点,但是有了分频器,事情就复杂多了。

UPDATE:更新:

Here's what I've tried:这是我尝试过的:

I created another drawable called nav_divider_selected which is exactly like the nav_divider above except with different colors.我创建了另一个名为nav_divider_selected drawable,它与上面的nav_divider完全一样,只是颜色不同。

Then, I created a drawer_item_background drawable like this:然后,我创建了一个drawer_item_background drawable,如下所示:

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

And, replaced the itemBackground of the NavigationView with this new selector ( app:itemBackground="@drawable/drawer_item_background" ).并且,取代了itemBackground所述的NavigationView与这个新的选择器( app:itemBackground="@drawable/drawer_item_background" But, it didn't work.但是,它没有用。 The items all look black, for some reason.出于某种原因,这些项目看起来都是黑色的。

I got it.我知道了。 It was a simple issue.这是一个简单的问题。 I was just using the wrong state.我只是使用了错误的状态。 So, here's the solution.所以,这是解决方案。 I created two different layer lists (to add the divider to the navigation items):我创建了两个不同的图层列表(将分隔线添加到导航项):

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_selected:

<?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/light_blue"/>
        </shape>
    </item>
</layer-list>

Then, created a drawer_item_background drawable like this ( state_checked should be used not state_selected ):然后,创建一个drawer_item_background绘制这样的( state_checked应使用不state_selected ):

<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/nav_divider_selected" android:state_checked="true"/>
        <item android:drawable="@drawable/nav_divider"/>
</selector>

And, then I used this drawable as itemBackground in my NavigationView :然后我在我的NavigationView使用了这个 drawable 作为itemBackground

<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/drawer_item_background"/>

Also if in-case you do not want to use a selector to achieve that you can do so like this此外,如果您不想使用选择器来实现,您可以这样做

<group android:checkableBehavior="single">
                <item
                    android:id="@+id/nav_share"
                    android:icon="@drawable/ic_menu_share"
                    android:title="@string/menu_share" />
            </group>

By wrapping the element under <group android:checkableBehavior="single"> we get a selected background.通过将元素包裹在<group android:checkableBehavior="single">我们得到了一个选定的背景。

Try the below item_background.xml in app:itemBackground="@drawable/item_background" for NavigationView在 app:itemBackground="@drawable/item_background" 中为 NavigationView 尝试以下 item_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/rgb_red" android:state_checked="true" />
    <item android:drawable="@color/rgb_red" android:state_activated="true" />
    <item>
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
          <item android:drawable="@color/rgb_white" />
          <item android:start="-2dp"
                android:top="-2dp"
                android:end="-2dp">
               <shape>
                 <solid android:color="@android:color/transparent" />
                 <stroke android:width="1dp" android:color="@color/rgb_pale_gray"/>
               </shape>
          </item>
        </layer-list>
    </item>
</selector>

hope it will help you希望它会帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM