2017-07-18 175 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> 
相關問題