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

问题描述 投票:1回答:1

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

但我希望显示这个项目填充BottomNavigationView,如下图: Image 2

我的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个回答
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>

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.