简体   繁体   中英

how to add title to navigation drawer in android

I have created a navigation drawer having a listview with icon and text. Now, I want to add a title above it and also another below it. Below are two xml I have used:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- <fragment
    android:id="@+id/badMap"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" /> -->
<FrameLayout 
    android:id="@+id/testFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<android.support.v4.widget.DrawerLayout

android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Framelayout to display Fragments -->
<FrameLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >



 </FrameLayout>  





<!-- Listview to display slider menu -->
<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"       
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background"/>

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

And:

 <?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="40dp"
android:background="@drawable/list_selector">



<LinearLayout
  android:id="@+id/itemLayout"
  android:layout_width="fill_parent"
  android:layout_height="55dp"
  android:layout_alignParentLeft="true"
  android:orientation="vertical"

  android:layout_marginTop="0dp"

 >




    <LinearLayout

              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:minHeight="55dp"
    >
        <ImageView
            android:id="@+id/drawerIcon"
            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/high"
            android:layout_centerVertical="true" 
            android:gravity="center" />

        <TextView
            android:id="@+id/drawerTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/drawerIcon"
            android:gravity="center"
            android:minHeight="?android:attr/listPreferredItemHeightSmall"
            android:layout_centerVertical="true" 
            android:text="@string/drawerlistTitle"
            android:textAppearance="?android:attr/textAppearanceListItemSmall"
            android:textColor="@color/list_item_title" />
        </LinearLayout>

    <View
  android:layout_width="match_parent"
  android:layout_height="1dp"
  android:layout_marginBottom="1dp"
  android:layout_marginTop="1dp"
  android:layout_marginLeft="10dp"
  android:layout_marginRight="10dp"
  android:background="#DADADC"

   ></View>

</LinearLayout>

</RelativeLayout>

Any ideas on how to add a titles in this?

It's simple actually :-)

Instead of using a ListView as your drawer, you can wrap the ListView in a RelativeLayout for instance like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

        <android.support.v4.widget.DrawerLayout
            android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <!-- Framelayout to display Fragments -->

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

            <!-- Listview to display slider menu -->

            <RelativeLayout
                android:layout_width="200dp"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/titleTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Title" />

                <ListView
                    android:id="@+id/list_slidermenu"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/titleTextView"
                    android:layout_above="@+id/titleUnderListView"
                    android:layout_gravity="start"
                    android:background="@color/list_background"
                    android:choiceMode="singleChoice"
                    android:divider="@color/list_divider"
                    android:dividerHeight="1dp"
                    android:listSelector="@drawable/list_selector" />

                <TextView
                    android:id="@+id/titleUnderListView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="true"
                    android:text="SubTitle" />

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

</RelativeLayout>

You can put whatever you like as the layout for the NavigationDrawer - the sky is the limit :-)

Another solution could be to add a header and footer to your current ListView , but then it wouldn't be fixed on top or the bottom.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM