提问者:小点点

半透明叠加在不同的视图上显示不同的效果


我有一个AppBarLayout,LinearLayout和一个BottomNavigationView。 我想在它们上面添加一个半透明的白色覆盖,但是我不知道为什么我在AppBarLayout和LinearLayout上面得到了这个略带灰色的颜色。 这是否因为有不同的海拔高度呢? 有没有可能在我的另外两个布局上得到与我的BottomNavigationView相同的效果而不改变它们的高度?

无覆盖带覆盖

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity"
    android:background="@android:color/white">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:id="@+id/appBar"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@android:color/white">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Title"
                android:textColor="@android:color/black"
                android:textSize="20sp"
                android:layout_centerInParent="true"/>

            <ImageButton
                android:id="@+id/buttonMenu"
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:src="@drawable/ic_menu"
                android:layout_centerVertical="true"
                android:layout_margin="16dp"/>

        </RelativeLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/appBar"
        app:layout_constraintBottom_toTopOf="@id/bottomNavigation"
        android:id="@+id/linearLayoutContents">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Lorem ipsum"
            android:textSize="32sp"
            android:textStyle="bold"
            android:textColor="@android:color/black"
            android:layout_margin="24dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:layout_marginEnd="32dp"
            android:text="@string/content"
            android:textColor="@android:color/darker_gray"
            android:textSize="16sp"/>

    </LinearLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/bottom_nav_menu"
        android:id="@+id/bottomNavigation"
        android:background="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/overlay"
        android:background="#CCFFFFFF"
        android:elevation="8dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

共1个答案

匿名用户

对同级视图使用elevation属性会导致视图根据您给出的Z顺序相互隐藏。 为了克服这个问题,我将我的布局层次结构更改为非平面状态,考虑到ConstraintLayout目的(平面视图层次结构),这是一种肮脏的方法。 如果你找到了更好的方法,请随意分享。

因此,要做到这一点,请将您的覆盖移出其父级,并删除提升:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".MainActivity"
    android:background="@android:color/white">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        ...

    </androidx.constraintlayout.widget.ConstraintLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/overlay"
        android:background="#CCFFFFFF"/>

</FrameLayout>