使用AppCompat工具栏与FrameLayout

3

所以我的 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,并占据整个屏幕的高度?有没有更好的方法来解决这个问题?

或者我应该把toolbar放在我的FragmentTransaction填充FrameLayout的布局中吗?这似乎不太有效,因为我有几个这样的布局。

1个回答

10

DrawerLayout 需要两个子视图:第一个为主内容,第二个为抽屉,两者的大小都应设置为 match_parent。因此,您的 ToolbarFrameLayout 应该被放在一个竖直的 LinearLayout 中,并将其设置为 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>

1
我不明白为什么你要这样做,而不是将所有内容都放在LinearLayout中,然后将工具栏置于抽屉布局的上方。 - tyczj
@tyczj 我一直认为 DrawerLayout 必须作为 xml 文件的两端。 - TheLettuceMaster
1
@tyczj - 你有看到我链接的帖子吗?如果你不把Toolbar放在DrawerLayout里面,你就无法得到正确的抽屉实现(在工具栏上方并透过状态栏显示,如材料设计指南中的导航抽屉部分所示)。 - ianhanniballake
@KickingLettuce 不,我在我的几个项目中都将其包装在LinearLayout中,而且完美运作。 - tyczj
@ianhanniballake,有人最好告诉谷歌,因为他们的几个应用程序都这样做(包括Google Play商店),虽然我猜这并不令人惊讶。 - tyczj
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接