导航抽屉项目的背景
Background of a Navigation Drawer item
我想为导航抽屉中的每个项目设置不同的背景。
我要实现
你们谁能解释一下怎么做?
我的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"
/>
创建自定义 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>
你会得到你想要的。
谢谢
我想为导航抽屉中的每个项目设置不同的背景。
我要实现
你们谁能解释一下怎么做?
我的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"
/>
创建自定义 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>
你会得到你想要的。
谢谢