导航抽屉 - Gmail App之类的部分之间的分隔线

时间:2014-11-13 10:31:14

标签: android navigation-drawer

我正在更新我的应用中的导航抽屉。我想在Gmail应用程序中添加部分分隔符。我该如何添加它们?只需将它们添加为视图,这是一种简单的方法。但是,我想知道,这是正确的方法吗?

Gmail应用:

enter image description here

现在,我正在使用带有标题视图的列表视图。

我当前的xml:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar_with_spinner" />

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </FrameLayout>
    </LinearLayout>

    <ListView
        android:id="@+id/listview_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        android:choiceMode="singleChoice"
        android:divider="@null"
        android:dividerHeight="0dp" />

</android.support.v4.widget.DrawerLayout>

实现Gmail导航等功能的正确方法是什么?

3 个答案:

答案 0 :(得分:2)

使用菜单资源而不是listview。菜单中的组标签创建分隔线

menu_drawer

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single"
        >
        <item
            android:id="@+id/id1"
            android:icon="@drawable/icon1"
            android:title="@string/title1"
            />
        <item
            android:id="@+id/id2"
            android:icon="@drawable/icon2"
            android:title="@string/title2"
            />
        <item
            android:id="@+id/id3"
            android:icon="@drawable/icon3"
            android:title="@string/title3"
            />

    </group>

    <group android:checkableBehavior="none"
        android:id="@+id/menu_nav_temp_gid"
        >
        <item
            android:id="@+id/settings"
            android:icon="@drawable/ic_settings"
            android:title="@string/settings" />
    </group>
</menu>

在导航视图中链接菜单资源

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ac_hs_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

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

    <android.support.design.widget.NavigationView
        android:id="@+id/ac_hs_nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/layout_nav_header"
        app:menu="@menu/menu_drawer"

        >

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

</android.support.v4.widget.DrawerLayout>

答案 1 :(得分:0)

我使用headerview和footerview将图像添加到顶部,将分隔符添加到底部。分隔符是视图。

drawer_list_footer_divider.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:layout_gravity="bottom"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <View
        android:id="@+id/left_viewline"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:background="?android:attr/listDivider" />

</LinearLayout>

drawer_list_footer_view.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="50dp"
    android:layout_gravity="bottom"
    android:background="@android:color/white"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingLeft="5dp"
    android:paddingRight="5dp" >

    <ImageView
        android:id="@+id/imageView_settings"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:contentDescription="@string/empty"
        android:src="@drawable/ic_action_settings" />

    <TextView
        android:id="@+id/textView_settings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:padding="10dp"
        android:singleLine="true"
        android:text="@string/action_settings" />

</LinearLayout>

在我的代码中:

private void setUpHeaderAndFooter() {

    LayoutInflater inflater = getLayoutInflater();
    View header = (View) inflater.inflate(R.layout.drawer_list_header_view,
            mDrawerList, false);
    mDrawerList.addHeaderView(header, null, false);

    View footer_divider = (View) inflater.inflate(
            R.layout.drawer_list_footer_divider, null, false);
    mDrawerList.addFooterView(footer_divider, null, false);

    // This view is Settings view 
    View footer = (View) inflater.inflate(R.layout.drawer_list_footer_view,
            mDrawerList, false);

    footer.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
                mDrawerLayout.closeDrawer(Gravity.LEFT);
            }

            if (android.os.Build.VERSION.SDK_INT < 11) {
                startActivity(new Intent(MainActivity.this,
                        Settings1Activity.class)
                        .setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
            } else {
                startActivity(new Intent(MainActivity.this,
                        Settings2Activity.class)
                        .setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
            }

        }
    });

    mDrawerList.addFooterView(footer, null, true);

}

答案 2 :(得分:0)

将DrawerLayout中的NavigationView用作其第二个子节点。在菜单中插入项目并将菜单属性应用于NavigationView以在导航抽屉中显示列表项。 然后,您可以将组ID分配给要放置在其中的菜单项,这将自动创建分隔符。 如需更广泛的解释,请点击:

How to create a simple divider in the new NavigationView?