android - 将 AppCompat 工具栏与 FrameLayout 一起使用

标签 android android-xml toolbar android-appcompat

所以我的 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。因此,您的 ToolbarFrameLayout 应该包裹在一个垂直的 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/

相关文章:

android - 从 xml 文件中扩充 View 是什么意思?

android - 使用 volley 将数据从 recyclerview 存储在 mysql 中

android - 在 Android 的任何布局中使用 colors.xml

android - 媒体播放器在android中播放暂停

java - 滚动 RecyclerView 时出现滞后/错误的 y 坐标(OnscrollListener)

android - 升级gradle之后:程​​序类型已经存在:net.jcip.annotations.GuardedBy

android - 我如何为整个 snackbar 布局设置 Action ,而不仅仅是为文本设置 Action ?

android-studio - 不要在预览窗口创建线程 - 华为广告实现

javascript - 如何使用 Javascript 创建工具栏/书签?

eclipse - 如何显示Eclipse的隐藏工具栏