android - 使用 BottomNavigationView 时如何仅显示片段区域?
问题描述
我目前正在开发一个使用 android-studio 和 Kotlin 的应用程序。
这个应用程序有一个 BottomNavigationView 和 4 个片段。
我的问题是如何使用 BottomNavigationView 仅显示片段区域?
例如)当我使用两个 LinearLayouts 划分片段区域时,每个 LinearLaytout 具有不同的高度,如下图所示: 并且我想将每个 LinearLaytout 划分为相同的高度。
以下是代码:
片段.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.HowToUseFragment" android:orientation="vertical">
<LinearLayout
android:id="@+id/viewBag"
android:visibility="visible"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/holo_blue_bright">
</LinearLayout>
<LinearLayout
android:id="@+id/viewReceipt"
android:visibility="visible"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/holo_blue_dark">
</LinearLayout>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/bottom_navigation_menu"/>
<fragment
android:id="@+id/nav_host_fragment"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/navigation_graph"/>
</FrameLayout>
安卓工作室:3.3.2
解决方案
尝试LinearLayout
代替FrameLayout
:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/navigation_graph"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/bottom_navigation_menu"/>
</LinearLayout>
或添加android:layout_marginBottom="56dp"
到您的<fragment/>
推荐阅读
- alignment - Latex 错误缺少 { 已插入。\结束{对齐}?
- javascript - 角材料输入模式添加空白
- grails - 如何在 micronaut 中执行序列化
- regex - .htaccess 复杂的重写规则
- docker - 在 docker 容器中,我是 root 但 apt-get update 不起作用
- google-colaboratory - 如何在 Google Colab 上使用示例数据文件?
- c# - 是否可以在运行时限制 SQL WITHIN 查询/连接的资源?
- linux - 通过 ansible 获取 yum 更新列表失败
- javascript - 我什么时候应该使用 React 钩子?
- python - 如何在本地 Web 服务器上托管烧瓶 api?