android - 即使视图位于其顶部,BottomNavigationView 也是可见的
问题描述
我有 3 个主要部分的屏幕
- 工具栏
- homeContaner(将托管BottomNavigationView的片段)
- mainContainer(主机启动画面或任何占满屏幕的屏幕)
当用启动BottomNavigationView替换mainContainer时, 我尝试向mainContainer添加背景颜色,发现BottomNavigationView在mainContainer的顶部
还尝试向mainContainer添加高程,但没有得到新结果
完整代码
<?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=".feature.home_Activity.HomeActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:titleTextColor="@android:color/white"/>
<FrameLayout
android:id="@+id/homeContainer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="@id/dummy"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar" />
<View
android:id="@+id/dummy"
android:layout_width="match_parent"
android:layout_height="@dimen/_10sdp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/navigation" />
<FrameLayout
android:id="@+id/mainContainer"
android:layout_width="0dp"
android:layout_height="0dp"
app:elevation="10dp"
android:background="#ffaaff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案
用LinearLayout包裹BottomNavigationView解决了问题
<?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=".feature.home_Activity.HomeActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:layout_constraintTop_toTopOf="parent"
app:titleTextColor="@android:color/white" />
<FrameLayout
android:id="@+id/homeContainer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="@id/dummy"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar" />
<View
android:id="@+id/dummy"
android:layout_width="match_parent"
android:layout_height="@dimen/_10sdp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/navigationContainer" />
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/navigationContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bn_bg"
app:elevation="1dp"
app:itemBackground="@drawable/bn_item__bg"
app:itemIconTint="@color/secondaryTextColor"
app:itemTextColor="@color/primaryTextColor"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/navigation" />
</androidx.appcompat.widget.LinearLayoutCompat>
<FrameLayout
android:id="@+id/mainContainer"
android:layout_width="0dp"
android:layout_height="0dp"
app:elevation="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
推荐阅读
- reactjs - 打字稿扩展类来定义属性类名
- mysql - mysql 关系数据库同步到弹性搜索的最佳实践
- spring - Spring Boot v2.2.6.RELEASE 和 Turbine - 无法聚合 metrix
- python - Python 将文件中的列表传递给 requests.get()
- r - R:eBay OAuth - 客户凭证授予请求
- java - 如何为存储在 ArrayList 中的特定属性设置值?
- java - 带有 BaseX 错误的 Java 连接:连接被拒绝:没有更多信息
- csv - 使用终端在 CSV 文件中搜索 2 个关键词并创建一个新的 csv 文件
- sql - 覆盖现有 JSON 对象的 SQL 查询
- c# - string.IndexOf(string) 的 StringComparison 方法是什么?