LinearLayout中的ListView不可见

时间:2017-07-18 08:42:25

标签: android android-layout

我在NavigationView遇到布局问题。目标是提供带适配器的标头和列表视图。首先,我有这个代码:

<android.support.v4.widget.DrawerLayout 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"
    xmlns:app="http://schemas.android.com/apk/lib/com.plaxxt.plaxxt1"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    android:paddingBottom="0dp"
    tools:context=".MainActivity"
    android:id="@+id/drawerLayout"
    android:fitsSystemWindows="true">

    <RelativeLayout
        android:layout_width = "match_parent"
        android:layout_height = "match_parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/my_awesome_toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary" />


        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="?attr/actionBarSize">
(...)
           </FrameLayout>
    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        android:fitsSystemWindows="true">

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="start">

                <include layout="@layout/drawer_header" />

                <ListView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/lst_menu_items" />

            </LinearLayout>

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

    </android.support.design.widget.NavigationView>

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

,结果如下(标题在那里,没有列表)

enter image description here

如果我用虚拟图像替换标题,则两者都出现:

<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        android:fitsSystemWindows="true">

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="start">

                <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingTop="@dimen/nav_header_vertical_spacing"
                    android:src="@android:drawable/sym_def_app_icon" />

                <ListView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/lst_menu_items" />

            </LinearLayout>

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

    </android.support.design.widget.NavigationView>

结果是:

enter image description here

因此我们看到使用我的适配器正确生成列表。

如果我删除图片并将ListView作为LinearLayout的唯一子项,则抽屉仍为空。

错误在哪里?请帮帮我...

1 个答案:

答案 0 :(得分:2)

尝试将Orientation添加到LinearLayout

 <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="start"  
                android:orientation="vertical">

                <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingTop="@dimen/nav_header_vertical_spacing"
                    android:src="@android:drawable/sym_def_app_icon" />

                <ListView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/lst_menu_items" />

            </LinearLayout>