如何在Android的BottomNavigationView中设置填充项

Posted

技术标签:

【中文标题】如何在Android的BottomNavigationView中设置填充项【英文标题】:How to set fill items in BottomNavigationView on Android 【发布时间】:2019-08-28 03:16:21 【问题描述】:

在我的应用程序中,我想使用BottomNavigationView 视图来显示一些片段。我有4 个fragments,我将这个fragments 设置为BottomNavigationView。 我写了下面的代码,但在大型设备屏幕(例如表格)中,我在BottomNavigationView 的中心显示了这些项目。 如下图:

但我想显示此项目填写BottomNavigationView,如下图:

我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/container"
    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_
    android:layout_
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/mainNavigationFragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_
        android:layout_
        app:defaultNavHost="true"
        android:layout_above="@+id/bottomNavigationView"
        app:navGraph="@navigation/navigation_graph"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_
        android:layout_
        android:background="?android:attr/windowBackground"
        android:layout_alignParentBottom="true"
        app:menu="@menu/navigation"/>

</RelativeLayout>

如何更改我的显示项目代码,例如图片二

【问题讨论】:

你好,我想这是你的答案 :) how you fill your bottom navigation menus ? 【参考方案1】:

尝试使用ConstraintLayout,看看问题是否解决

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/container"
        android:layout_
        android:layout_
        tools:context=".MainActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        app:labelVisibilityMode="labeled"
        android:id="@+id/bottomNavigationView"
        app:menu="@menu/navigation_manager"
        android:layout_
        android:layout_
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_
        android:layout_
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:menu="@menu/navigation_graph"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintVertical_bias="0.0"/>

</android.support.constraint.ConstraintLayout>

【讨论】:

谢谢亲爱的,但是您的代码只是为 BottomNavigationView 设置了全屏!我想设置BottomNavigationView的全屏项目! 我的回答与图 2 类似。 是的,我的朋友,我试试你的代码并为我工作。感谢您的帮助

以上是关于如何在Android的BottomNavigationView中设置填充项的主要内容,如果未能解决你的问题,请参考以下文章

记录一下《猜歌猜谜》应用主要使用的开源库

如何在Android的tableview中左对齐文本

Android Dialog如何显示在空间的下面

如何在android原生中加载RN页面

Android File Transfer Mac: 如何在 macOS 和 Android 系统之间移动数据

如何在 Android 上将 TextView 居中?