所以我的XML是这样的:
<?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,工具栏的作用就像match_parent一样,并且会占据屏幕的整个高度?有没有更好的方法呢?
或者我应该将工具栏放在我的FragementTransactions填充FrameLayout的布局中吗?这似乎没有效率,因为我有几个.
解决方法:
DrawerLayout有两个子视图:第一个用于主要内容,第二个用于抽屉:两者始终设置为match_parent.因此,您的工具栏和FrameLayout应该包装在一个垂直的LinearLayout中,根据AppCompat制造商的canonical example设置为match_parent:
<!-- 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>