导航抽屉-Gmail应用程序等部分之间的分隔线 [英] Navigation Drawer - Divider between sections like Gmail App

查看:79
本文介绍了导航抽屉-Gmail应用程序等部分之间的分隔线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am updating the Navigation Drawer in my app. I want to add section dividers as the Gmail App has. How do I add them? Just add them as views, which is a simple approach. But, i want to know, is it the correct approach?

Gmail应用程序:

Gmail App:

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

Right now, I am using a listview with a header view.

我当前的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导航之类的正确方法是什么?

What is the correct approach of achieving something like the Gmail Navigation?

推荐答案

我使用headerview和footerview在顶部添加了图像,在底部添加了分隔线.分隔线是一个视图.

I used headerview and footerview to add the image on top and divider at the bottom. The divider is a View.

drawer_list_footer_divider.xml

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

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);

}

这篇关于导航抽屉-Gmail应用程序等部分之间的分隔线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆