如何创建"类别"在列表项中?

时间:2016-06-20 13:33:53

标签: android menuitem listitem drawer

我想创建一些类别,以分隔抽屉菜单中的项目名称。但我在strings.xml中只有这个:

    <?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Slider Menu</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="drawer_open">Slider Menu Opened</string>
    <string name="drawer_close">Slider Menu Closed</string>

    <!-- Nav Drawer Menu Items -->
    <string-array name="nav_drawer_items">
        <item >Home</item>
        <item >Connection</item>
        <item >Register</item>
        <item >Communities</item>
        <item >Pages</item>
        <item >Logout</item>
    </string-array>

    <!-- Nav Drawer List Item Icons -->
    <!-- Keep them in order as the titles are in -->
    <array name="nav_drawer_icons">
        <item>@drawable/ic_home</item>
        <item>@drawable/ic_people</item>
        <item>@drawable/ic_photos</item>
        <item>@drawable/ic_communities</item>
        <item>@drawable/ic_pages</item>
        <item>@drawable/ic_whats_hot</item>
    </array>

    <!-- Content Description -->
    <string name="desc_list_item_icon">Item Icon</string>

</resources>

在那里,它是我的drawer_list_item.xml:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@drawable/list_selector">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:contentDescription="@string/desc_list_item_icon"
        android:src="@drawable/ic_home"
        android:layout_centerVertical="true" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/icon"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:textColor="@color/list_item_title"
        android:gravity="center_vertical"
        android:paddingRight="40dp"/>

    <TextView android:id="@+id/counter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/counter_bg"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="8dp"
        android:textColor="@color/counter_text_color"/>

</RelativeLayout>

例如,我想在&#34; Home&#34;之间用小条(或其他)进行分离。和&#34;连接&#34; ...

2 个答案:

答案 0 :(得分:1)

在您的活动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"
    app:menu="@menu/activity_main_drawer" />

在res&gt;中创建activity_main_drawer.xml菜单,

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_gallery"
        android:icon="@drawable/ic_menu_gallery"
        android:title="Gallery" />
    <item
        android:id="@+id/nav_slideshow"
        android:icon="@drawable/ic_menu_slideshow"
        android:title="Slideshow" />
</group>

<item android:title="Communicate">
    <menu>
        <group android:checkableBehavior="single">
            <item
                android:id="@+id/nav_manage"
                android:icon="@drawable/ic_menu_manage"
                android:title="Tools" />
        </group>
    </menu>
</item>

将标题和图标更改为您喜欢的内容。您还可以在app:headerLayout中添加NavigationView,以便在菜单顶部显示标题。

答案 1 :(得分:0)

按建议phasecorrelate使用菜单资源(包含群组)而非string-array