2017-07-18 176 views
0

我遇到了我的NavigationView中的布局问题。目标是通过适配器提供标题和列表视图。首先,我有这个代码:LinearLayout中的ListView不可见

<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的唯一孩子,则抽屉保持空白。

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

+0

制作'NestedScrollView'高度'match_parent',LinearLayout中'wrap_content'和ListView'wrap_content' – Piyush

+0

@Piyush对不起,没有效果... –

+0

我也建议使用'RecyclerView'而不是'ListView'。 – Piyush

回答

1

尝试加入定位到的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> 
相关问题