【问题标题】:Using AppCompat Toolbar with FrameLayout将 AppCompat 工具栏与 FrameLayout 一起使用
【发布时间】:2014-10-29 16:15:56
【问题描述】:

所以我的 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 并且会占据屏幕的整个高度?有更好的方法吗?

或者我应该将工具栏放在我的FragementTransactions 填充FrameLayout 的布局旁边吗?这似乎效率不高,因为我有几个。

【问题讨论】:

    标签: android android-xml toolbar android-appcompat


    【解决方案1】:

    DrawerLayout 有两个子视图:第一个用于主要内容,第二个用于抽屉:两者都始终设置为match_parent。因此,您的 ToolbarFrameLayout 应包裹在垂直的 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>
    

    【讨论】:

    • 我不明白你为什么要走这条路,而不是将所有东西都包装在一个线性布局中,然后在抽屉布局上方放置工具栏
    • @tyczj 我一直认为DrawerLayout 必须将 xml 文件添加到书端。
    • @tyczj - 你读过我链接的帖子吗?如果您不将Toolbar 放在DrawerLayout 中,您将无法正确实现抽屉(在工具栏上方并根据material design guidelines 通过状态栏显示 - 请参阅导航抽屉部分)。
    • @KickingLettuce 不,我在我的几个项目中都将它包裹在 LinearLayout 中,并且效果很好
    • @ianhanniballake 材料指南也这样,如果您使用主页按钮作为向上导航来显示导航抽屉下方的工具栏
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 2015-01-06
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    相关资源
    最近更新 更多