2016-05-29 61 views
-1

我做了一個活動,其中包含一個ListView和一個片段(帶有DrawerLayout)。片段上的DrawerLayout隱藏ListView

Activity.xml:

<?xml version="1.0" encoding="utf-8"?> 
<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:background="#ddffff" 
> 

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/lista" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentTop="true" /> 

<fragment 
    android:id="@+id/fragment1" 
    android:name=".Fragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

</RelativeLayout> 

ListView中不會出現。我認爲listview是隱藏的,因爲這個片段。我不知道爲什麼。

+0

全屏顯示listview和fragment。該片段位於頂部,因此它覆蓋了列表視圖。你想如何將listview和fragment定位在屏幕上? – Francesc

+0

我想讓listview放在drawerlayout後面。像這樣:http://blog.teamtreehouse.com/wp-content/uploads/2015/02/nav_drawer_final.gif – tomss

回答

0

要創建DrawerLayout的佈局,佈局中的頂層元素必須是DrawerLayout類型。嘗試像這樣

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <!-- 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="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:background="#111"/> 
</android.support.v4.widget.DrawerLayout> 

請參閱this作爲參考。

0

這是很自然的片段覆蓋整個看法,因爲你設置其屬性爲

android:layout_width="match_parent" 
android:layout_height="match_parent" 

這意味着該片段將始終堅持「匹配父」的意思總是覆蓋的整個分辨率父佈局(在這種情況下,RelativeLayout的),你可以把它改成一個數值來這樣做,例如停止它,如果我想我的片段總是有30DP的高度我會做:

android:layout_width="match_parent" 
android:layout_height="30dp" 

或者,您可以設置輔助佈局與一個設置的大小,並有片段填充,而不是。

+0

它返回一個錯誤:DrawerLayout必須使用MeasureSpec.EXACTLY進行測量。 – tomss

+0

如果您使用的是DrawerLayout,您應該遵循Francesc的說法並創建一個包裝所有內容的DrawerLayout,然後創建一個包含該片段的frameLayout,然後創建一個listview本身。 – Ilias