android - 半透明叠加在不同的视图上显示不同的效果
问题描述
我有一个 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>
解决方案
对同级视图使用海拔属性会导致视图根据您给它们的 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>
推荐阅读
- c++ - Eigen 版本 3.2.0 和 3.3.4 之间的数值差异
- java - 如何在 Junit 中模拟模板(.ftl 文件)
- javafx - SceneBuilder 和 settings.xml
- ssl - Certbot(300 多项选择)和 Nginx 和 DNS 的身份验证问题
- angular - Angular 6 ng-serve 在重新启动之前不显示动态图像
- node.js - NPM 错误 TS2304:找不到 ThreeJS 的名称“VRDisplay”
- json - 检查 SQL Server 中的唯一 JSON 键
- photoshop - 使用 Photoshop 批量替换智能对象
- excel - Excel/VBA 匹配 2 个条件,提取序列中的最后一个匹配项和中断序列后的第一个匹配项
- html - 如何区分页内链接和页外链接