如何在抽屉中添加标题

时间:2015-03-25 19:54:53

标签: android

<ListView 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" 
android:choiceMode="singleChoice"
android:divider="@android:color/transparent" 
android:dividerHeight="0dp"
android:background="#cccc" 
tools:context=".NavigationDrawerFragment" />

我将它作为fragment_navigation_drawer.xml,但我希望有一个标题,只是一个简单的ImageView或带有Name,Email,CircleView的复杂事物。

我怎样才能改变这个?


此文件:fagment_main.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" 
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
   tools:context=".MainActivity$PlaceholderFragment">

<TextView android:id="@+id/section_label" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>





  This file: fragment_navigation_drawer.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NavigationDrawerFragment">

<ImageView
    android:id="@+id/my_header_image_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/simple_image_ok" />

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#cccc" />
</LinearLayout>


        Thisfile: activity_main.xml

   <android.support.v4.widget.DrawerLayout         
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"       
   android:id="@+id/drawer_layout"
   android:layout_width="match_parent" android:layout_height="match_parent"
   tools:context=".MainActivity">

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



    <fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width"        
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="t9200.laurentiu.com.dti200t9.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

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

      Those are my xml files.. i hope you can help me :D

03-26 18:34:02.674 5431-5431 / t9200.laurentiu.com.dti200t9 E / AndroidRuntime:致命异常:主要     处理:t9200.laurentiu.com.dti200t9,PID:5431     java.lang.RuntimeException:无法启动活动ComponentInfo {t9200.laurentiu.com.dti200t9 / t9200.laurentiu.com.dti200t9.MainActivity}:android.view.InflateException:二进制XML文件行#19:错误膨胀类片段             在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2395)             在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2453)             在android.app.ActivityThread.access $ 900(ActivityThread.java:173)

它继续......

2 个答案:

答案 0 :(得分:1)

Android列表视图允许您向ListView添加全局标头。

参考 Using ListView : How to add a header view?

LayoutInflater inflater = getLayoutInflater(); ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, myListView, false); myListView.addHeaderView(header, null, false);

答案 1 :(得分:0)

您甚至不必使用ListView的标题功能。您可以在导航抽屉中使用任何复杂的布局,因此我甚至建议您使用LinearLayout作为顶部元素。然后,您可以放入ImageView并在其下面放置ListView。像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NavigationDrawerFragment">

    <ImageView
        android:id="@+id/my_header_image_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="{Your Drawable Here}" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#cccc" />
</LinearLayout>

您还可以轻松地将ImageView替换为任何复杂的布局和放大器。你想要的子元素。