How to add title in Navigation drawer layout?

不羁的心 提交于 2019-11-29 20:19:42

Put a TextView above a ListView, and wrap it inside a vertical LinearLayout . Give to your ListView android:layout_weight="1" and android:layout_height="0dip"

You would do that the same way as you would add headings in any other ListView, by teaching your ListAdapter to return heading rows as well as detail rows. At the low level, this involves overriding methods like getViewTypeCount() and getItemViewType() in your ListAdapter, plus having getView() know the difference between the row types. Or, use an existing high-level implementation like https://github.com/emilsjolander/StickyListHeaders or http://code.google.com/p/android-amazing-listview/ or any of the others found when searching for android listview headers.

Maybe it's a bit late but I think I have a simpler solution. In your Activity's layout, instead of adding a listView inside the DrawerLayout, you can add for example a LinearLayout, and you can easily add separators and rows. For example:

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <com.astuetz.viewpager.extensions.PagerSlidingTabStrip
            android:id="@+id/indicator"
            android:layout_height="48dip"
            android:layout_width="fill_parent"/>

        <ViewPager
            android:id="@+id/pager"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>

</RelativeLayout>

<LinearLayout 
    android:orientation="vertical"
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#111">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Separator 1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First button"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Separator 2"/>

</LinearLayout>

And in the Activity, you can add the listeners to the buttons.

Hope that helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!