【问题标题】:AppBarLayout not allowing other Views to match_parentAppBarLayout 不允许其他视图匹配父级
【发布时间】:2016-04-12 16:19:46
【问题描述】:

我正在使用一个带有 AppBarLayout 的布局,其中包含一个工具栏和一个 TabLayout、一个 ViewPager 和一个 FloatingActionMenu。

我的问题是,当使用 layout_height=match_parent 时,使用 AppBarLayout 会导致 CoordinatorLayout 内的其他视图与整个屏幕不匹配,而是与父级左侧的其他视图匹配,不包括 AppBarLayout。这种行为对我的 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 作为我的根视图或使用 LinearLayout 来保存我的 Toolbar 和 TabLayout 可以解决我的问题,但我需要使用 CoordinatorLayout 和 AppBarLayout 来添加滚动功能。

编辑:

在不同的设备上进行测试后,我发现这个问题只发生在较新的版本上,如下图所示。

在 API 19 上:

在 API 23 上:

【问题讨论】:

  • idk 你的代码似乎是正确的,但是我可以删除这一行以进行调试,因为它并不是真正需要的! android:orientation="vertical" 来自应用栏
  • @k0sh 谢谢,我忘记删除了,在我尝试使用 LinearLayout 而不是 AppBarLayout 时使用它。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多