如何在android中添加导航抽屉的标题

时间:2014-06-21 18:27:54

标签: android xml android-layout

我创建了一个导航抽屉,其中包含带有图标和文字的列表视图。现在,我想在它上面添加一个标题,在它下面添加另一个标题。下面是我用过的两个xml:

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

 <?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>

有关如何在此添加标题的任何想法吗?

1 个答案:

答案 0 :(得分:0)

实际上很简单: - )

您可以将ListView包裹在ListView中,而不是使用RelativeLayout作为抽屉,例如:

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

你可以把你喜欢的任何东西作为NavigationDrawer的布局 - 天空是极限: - )

另一种解决方案可能是在当前ListView添加页眉和页脚,但不会在顶部或底部修复。