android - 我将如何在 NavigationDrawer 中创建标签指示器?

标签 android navigation-drawer

如何创建这样的导航绘制标签指示器? NavigationDrawer 类是否有像 TabLayout 这样的指示器?

enter image description here

最佳答案

您可以定义一个带有左边框的自定义可绘制对象,并将其设置为所选项目的背景。按照下面给出的步骤,

Step 1: Define a drawable with left border(selected_item.xml) in drawables

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <solid android:color="#000000" />
    </shape>
</item>
<item android:left="2dp">
    <shape android:shape="rectangle">
        <solid android:color="#FF0000" />
    </shape>
</item>
</layer-list>

Step 2: Define a selector(navi_selector.xml) in drawables

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true" android:drawable="@drawable/selected_item"/>
    <item android:state_pressed="true" android:drawable="#000000"/>

</selector>

Step 3: set the navi_selector for app:itemBackground

 <android.support.design.widget.NavigationView
        app:itemTextColor="@android:color/white"
        android:fitsSystemWindows="true"
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/activity_main_drawer"
        android:background="#000000"
        app:itemBackground="@drawable/navi_selector"/>

关于android - 我将如何在 NavigationDrawer 中创建标签指示器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56963174/

相关文章:

android - 日期选择器对话框选择了错误的年份

android - 抽屉导航重叠在 ActionBar 导航选项卡内容上

android - 抽屉导航 : Add Titles to Groups, 不是项目

java - Firebase/DrawerLayout - 无缘无故注销?

android - 如何在没有支持库的情况下创建抽屉导航

android - 如何在Android中自动为大数字添加逗号?

android - buildConfigField、resValue 和 manifestPlaceholders 之间有什么区别?

android - 亚行因某种原因停止了吗?

android - 错误 "java.lang.NoClassDefFoundError: com.google.repacked.apache.commons.io.FileUtils"

android - 在通过 fragment 导航时正确处理 DrawerLayout 动画