简体   繁体   English

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

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

I have created a navigation drawer having a listview with icon and text. 我创建了一个带有带有图标和文本的listview的导航抽屉。 Now, I want to add a title above it and also another below it. 现在,我想在其上方添加一个标题,并在其下方添加另一个标题。 Below are two xml I have used: 以下是我使用的两个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>

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: 您可以将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>

You can put whatever you like as the layout for the NavigationDrawer - the sky is the limit :-) 您可以随意放置任何内容作为NavigationDrawer的布局-天空是极限:-)

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. 另一种解决方案是在当前ListView添加页眉和页脚,但是这样就不会固定在顶部或底部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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