How to fix the position of bottom navigation view

2019-07-24 18:46发布

问题:

I have a bottom navigation view in my home page, there are four menu items. On onCreate(), the four menu items are not in fixed position, the label is shown only when it is tapped on and it changes the position. I want them to remain fixed in position with their labels visible, not changing position upon touching. Here are my xml codes:

     <?xml version="1.0" encoding="utf-8"?>
       <menu xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       xmlns:design="http://schemas.android.com/tools">
       <item
        android:id="@+id/menu_home"
        android:title="Home"
        android:enabled="true"
        android:icon="@drawable/home_icon"
        app:showAsAction="always"/>
    <item
        android:id="@+id/menu_wallet"
        android:title="Wallet"
        android:enabled="true"
        android:icon="@drawable/wallet_icon"
        app:showAsAction="always"/>

    <item
        android:id="@+id/menu_toto"
        android:title="Toto"
        android:enabled="true"
        android:icon="@drawable/toto_icon"
        app:showAsAction="always"/>
    <item
        android:id="@+id/menu_settings"
        android:title="Settings"
        android:enabled="true"
        android:icon="@drawable/setting_icon"
        app:showAsAction="always"/>
</menu>

and the main xml is like this:

<android.support.design.widget.BottomNavigationView
   android:id="@+id/navigation"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   app:itemBackground="@color/homenavigationbottom"
   app:itemTextColor="@android:color/white"
   android:fitsSystemWindows="true"
   app:menu="@menu/bottom_navigation_menu"/>

回答1:

Add the following tag in your activity_main.xml file code:

android:layout_gravity="bottom"

More info: https://developer.android.com/reference/android/support/design/widget/BottomNavigationView.html