2014-10-29 45 views
3

所以我的XML是這樣的:使用程序兼容性工具欄用的FrameLayout

<?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" > 

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

    <FrameLayout 
     android:id="@+id/main_frag" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <!-- ListView here --> 

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

發生了什麼,即使我把我的高度明確爲56dp,該toolbar行事像match_parent和遺囑屏幕的整個高度?有沒有更好的方法來做到這一點?

或者我應該把工具欄放在我的FragementTransaction s填充FrameLayout的佈局中?這似乎並不高效,因爲我有其中幾個。

回答

7

DrawerLayout需要兩個孩子的意見:第一個爲主要內容,第二個爲抽屜:兩者始終設置爲match_parent。因此,你的ToolbarFrameLayout應該被包裹在被設置爲match_parent爲每canonical example從程序兼容性的製造商垂直LinearLayout

<!-- The important thing to note here is the added fitSystemWindows --> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/my_drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <!-- Your normal content view --> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <!-- We use a Toolbar so that our drawer can be displayed 
      in front of the action bar --> 
     <android.support.v7.widget.Toolbar 
      android:id="@+id/my_toolbar" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:minHeight="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" /> 

     <FrameLayout 
      android:id="@+id/main_frag" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 

    </LinearLayout> 

    <!-- Your drawer view. This can be any view, FrameLayout 
     is just an example. As we have set fitSystemWindows=true 
     this will be displayed under the status bar. --> 
    <FrameLayout 
     android:layout_width="304dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="left|start" 
     android:fitsSystemWindows="true"> 

     <!-- ListView here --> 

    </FrameLayout> 

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

我不明白你爲什麼會走這條路,而不是在一個LinearLayout中包裹的一切,然後在抽屜佈局上方的工具欄 – tyczj 2014-10-29 16:27:45

+0

@tyczj我一直認爲'DrawerLayout'必須支持xml文件。 – KickingLettuce 2014-10-29 16:30:21

+1

@tyczj - 你看過我鏈接的帖子吧?您將無法正確執行抽屜(在工具欄上方,並按照[材料設計準則](http://android-developers.blogspot.com/2014/10/material-design- on-android-checklist.html) - 如果你不把'Toolbar'放在'DrawerLayout'中,請參閱Navigation Drawer部分。 – ianhanniballake 2014-10-29 16:31:19