2014-10-29 42 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