【问题标题】:How to set fill items in BottomNavigationView on Android如何在Android的BottomNavigationView中设置填充项
【发布时间】: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_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/mainNavigationFragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        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_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        android:layout_alignParentBottom="true"
        app:menu="@menu/navigation"/>

</RelativeLayout>

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

【问题讨论】:

标签: java android xml fragment bottomnavigationview


【解决方案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_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        app:labelVisibilityMode="labeled"
        android:id="@+id/bottomNavigationView"
        app:menu="@menu/navigation_manager"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        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_width="0dp"
        android:layout_height="0dp"
        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 类似。
  • 是的,我的朋友,我试试你的代码并为我工作。感谢您的帮助
猜你喜欢
  • 2022-07-19
  • 2023-03-06
  • 2017-03-05
  • 2017-11-20
  • 1970-01-01
  • 2017-08-31
  • 2013-08-30
  • 2017-11-20
相关资源
最近更新 更多