导航抽屉项目的背景

时间:2018-09-29 07:35:17

标签: android navigation-drawer

我想为导航抽屉中的每个项目设置不同的背景。 我想实现enter image description here

你们中的任何一个都可以解释如何做吗?

我的Activity_main.xml

<android.support.design.widget.NavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:menu="@menu/nav_menu"
        android:fitsSystemWindows="true"
        android:background="@android:color/black"
        app:itemTextColor="@android:color/white"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_header"
        app:itemIconTint="@android:color/white"
        app:itemBackground="@drawable/header_background"
        />

1 个答案:

答案 0 :(得分:1)

创建自定义的NavigationView布局,并包括如下所示的布局,

  <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="end"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main">

        <include layout="@layout/custom_layout"></include>

    </android.support.design.widget.NavigationView>

custom_layout.xml文件

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>

现在只需像这样绘制一个可绘制对象,即可(根据需要更改颜色)

btn_green_border.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="@color/colorSkyBlue"
tools:targetApi="lollipop">
<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">

        <solid android:color="@color/colorAccent" />
        <stroke
            android:width="@dimen/_1dp"
            android:color="@color/colorLightBlue"></stroke>
        <corners android:radius="@dimen/_30dp"></corners>
        <size
            android:width="@dimen/_50dp"
            android:height="@dimen/_100dp"></size>

    </shape>
</item>
</ripple>

并在布局中为您的RecyclerView项目使用此可绘制对象,如下所示:

item_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_card"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
    android:id="@+id/btn_refer"
    android:layout_width="200dp"
    android:layout_height="@dimen/_40dp"
    android:background="@drawable/btn_green_border"
    android:text="Navigation Item Name"
    android:textAllCaps="false"
    android:textColor="@color/colorPrimary" />

</LinearLayout>

您将实现您想要的。

谢谢