【问题标题】:How can I add a Header in Drawer如何在抽屉中添加页眉
【发布时间】:2015-03-25 19:54:53
【问题描述】:
<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 或带有名称、电子邮件、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: 致命异常: main 进程: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)

它继续..

【问题讨论】:

    标签: android


    【解决方案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);

    【讨论】:

    • 您可以将自己的布局文件定义为 header.xml,然后对其进行扩展,上面的代码会将其作为标题添加到您的列表视图中
    【解决方案2】:

    您甚至不必使用 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 替换为您想要的任何复杂布局和子元素。

    【讨论】:

    • 你能提供更多关于什么不工作的信息吗?
    • 它可以编译,但是应用程序崩溃了。
    • Activity 布局的顶级元素是 android.support.v4.widget.DrawerLayout 吗?具有导航抽屉的活动必须将其作为根节点。然后你有一个正常的布局部分,我上面引用的 LinearLayout 成为导航抽屉部分。
    • 在上面的堆栈跟踪中,看起来您可能正在设置一个布局 xml,其中有一个片段作为顶部节点作为活动布局。您的活动布局需要像 RelativeLayout 这样的顶级布局。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    相关资源
    最近更新 更多