所以我的 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,toolbar
的行为类似于 match_parent
并且会占用屏幕的整个高度?有更好的方法吗?
或者我应该将工具栏放在我的 FragementTransaction
填充 FrameLayout
的布局中吗?这似乎效率不高,因为我有几个。
最佳答案
DrawerLayout
有两个 subview :第一个用于主要内容,第二个用于抽屉:两者始终设置为 match_parent
。因此,您的 Toolbar
和 FrameLayout
应该包裹在一个垂直的 LinearLayout
中,根据 canonical example 设置为 match_parent
AppCompat 的制造商:
<!-- 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>
关于android - 将 AppCompat 工具栏与 FrameLayout 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26635485/