java - AppBarLayout 不允许其他 View 进行 match_parent

标签 java android android-coordinatorlayout android-appbarlayout coordinator-layout

我正在使用带有 AppBarLayout 的布局,其中包含 Toolbar 和 TabLayout、ViewPager 和 FloatingActionMenu。

我的问题是,使用 AppBarLayout 会导致 CoordinatorLayout 内的其他 View 在使用 layout_height=match_parent 时与整个屏幕不匹配,而是与除 AppBarLayout 之外的父级的剩余 View 相匹配。这种行为对于我的 ViewPager 来说是完美的,因为否则它的内容将被 AppBarLayout 覆盖,但我需要 FloatingActionMenu 来匹配整个屏幕,这样当我的菜单打开时我可以使屏幕变暗。

所以我的问题是:如何让我的 FloatingActionMenu 占据整个屏幕?

我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.Home">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppBaseTheme.ToolbarPopup"
        app:theme="@style/AppBaseTheme.Toolbar">

    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:tabTextAppearance="@style/AppBaseTheme.TabText"
        app:theme="@style/AppBaseTheme.Toolbar"/>

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/home_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<com.example.FloatingActionMenu
    android:id="@+id/fam"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom|end"
    android:padding="@dimen/fab_margin"
    app:menu_backgroundColor="#BB8b8b8b">

    <com.example.FloatingActionButton
        android:id="@+id/fab"
        style="@style/MenuButtonsStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</com.example.FloatingActionMenu>

使用RelativeLayout作为我的 Root View 或使用LinearLayout来容纳我的工具栏和TabLayout可以解决我的问题,但我需要使用CoordinatorLayout和AppBarLayout来添加滚动功能。

编辑:

在不同的设备上进行测试后,我发现这个问题只发生在较新的版本上,正如您在下面的照片中看到的那样。

在 API 19 上: Older version: API 19

在 API 23 上:

Newer version: API 23

最佳答案

问题是我没有为 FloatingActionMenu 指定 android:elevation,因此 AppBarLayout 出现在菜单顶部

关于java - AppBarLayout 不允许其他 View 进行 match_parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34669329/

相关文章:

java - 未封闭的字符类(正则表达式)

java - 为什么我不能在 Selenium/HtmlUnit 的字段中输入文本?

java - 错误:Could not run build action using Gradle distribution 'https://services.gradle.org/distributions/gradle-4.2.1-bin.zip'

android - Amazon IAP v2.0 不响应 android 实时测试

带有折叠工具栏的 RecyclerView 中的 Android 字母快速 ScrollView

android - 包裹在 CoordinatorLayout 中的 RecyclerView 在键盘打开时未调整大小

android - 禁用工具栏滚动

java - SerialVersionUID 未在 Servlet 中声明

android - 致命异常 : ThumbnailManager-1 while sharing images to Intent

android - 以编程方式禁用多窗口功能