如何自定义导航抽屉菜单

6
我需要自定义我的导航抽屉,以下是res/menu/activity_main_drawer.xml中的一部分代码。
<item
    android:title="Discover">
    <menu>
        <item
            android:id="@+id/nav_qrcode"
            android:icon="@drawable/read_qr"
            android:title="QRCode" />

        <item
            android:id="@+id/favorite"
            android:icon="@drawable/favorite"
            android:title="favorite" />
    </menu>
</item>

这是在res/layout/activity_home.xml中的布局。
<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    android:background="#B59154"
    app:itemTextColor="#ffffff"
    app:menu="@menu/activity_main_drawer" />

我想给第一个项目分配一个背景颜色,我该怎么做?

“Discover”是一个类别,其他项目是菜单(可点击)。

我不知道如何为我的“类别”分配不同的布局。

1个回答

12

您始终可以选择创建自己的自定义布局,以便为导航抽屉设计您自己的方式。例如:

导航视图

<android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start">



    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
            <include layout="@layout/lay_drawer_menu" />

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

lay_drawer_menu.xml

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

    <TextView
        android:id="@+id/txt1"
        android:textcolor = "whatever you want"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Activities" />

    <TextView
        android:id="@+id/txt2"
      android:textcolor = "whatever you want"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Add Detail"
        android:textColor="@android:color/black" />


</LinearLayout>

活动

  @Override
        public void onClick(View view) {

            switch (view.getId()) {
                case R.id.txt1: {
                    drawerLayout.closeDrawers();

                   //your code.

                }
                break;
               case R.id.txt2: {
                    drawerLayout.closeDrawers();

                   //your code.

                }
                break;

            }

        }

希望这可以帮到你。


它实际上很有帮助 - Tuan Nguyen
你好,我无法检测到onclick事件。你能帮我吗? - Yesha Shah
我已经尝试过了,但是我的ScrollView无法在垂直方向上滚动。 - Qadir Hussain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接