android - 将固定/静态 header 添加到抽屉导航布局

标签 android listview navigation-drawer drawerlayout

像许多应用程序一样,我使用 DrawerLayout 代码如下:

<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="fill_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        tools:context=".MainActivity">
    <!-- The main content view -->
    <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    <!-- The navigation drawer -->



    <ListView
            android:id="@+id/left_drawer"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#FFFFFF" />

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

我在 ListView 上方有一些用户信息,所以我使用 addHeaderView()。但是,这会随列表滚动。我希望列表显示为在其下方滚动并且标题固定在顶部。

这可能吗?我尝试将 ListView 包装在 LinearLayout 中,并将标题代码也放在该布局中。这完全破坏了应用程序。还有其他方法吗?我实际上已经看到谷歌至少在他们的一个应用程序 (youtube) 中这样做了。

最佳答案

以某种布局包装您的列表是可行的方法。也许你只是错过了什么。试试这个:

<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="fill_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    tools:context=".MainActivity">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <!-- The navigation drawer -->


    <LinearLayout
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_gravity="right">

        <!--SOME HEADER-->
        <View
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

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

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

关于android - 将固定/静态 header 添加到抽屉导航布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30086139/

相关文章:

java - NavigationDrawer 打开新 Activity

android - 如何为支持平板电脑和手机的应用程序提供导航

"WHERE"附近的 Android 数据库更新 : syntax error

Android: android:layout_width=wrap_content 但不能小于X dp?

java - Java 中的 TotallyLazy 函数代码

listview - 用列/行小部件包裹小部件后屏幕消失

java - 处理 onClick 事件时 getView 出现空指针异常

android - 尝试使用 runQueryOnBackgroundThread 过滤 ListView 但没有任何反应 - 我错过了什么?

Android:带有抽屉导航 fragment 的偏好 fragment

java - 从 android 2.1 使用 ado.net 数据服务