簡體   English   中英

如何在Android的導航抽屜中添加標題

[英]how to add title to navigation drawer in android

我創建了一個帶有帶有圖標和文本的listview的導航抽屜。 現在,我想在其上方添加一個標題,並在其下方添加另一個標題。 以下是我使用的兩個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>

關於如何在其中添加標題的任何想法?

實際上很簡單:-)

您可以將ListView包裹在RelativeLayout ,而不是將ListView用作抽屜,例如:

<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添加頁眉和頁腳,但是這樣就不會固定在頂部或底部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM