如何设置Bottom Navigation Bar默认选中Home图标
我正在使用Java开发一款Android应用,实现了Bottom Navigation Bar。进入包含该底部导航栏的Activity时,页面已显示Home Fragment,但导航栏图标仍默认选中Message,请问如何设置默认选中Home图标?
我的Activity代码
package com.example.a1221_test; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import android.os.Bundle; import com.example.a1221_test.databinding.ActivitySecondBinding; import com.google.android.material.bottomnavigation.BottomNavigationView; public class SecondActivity extends AppCompatActivity { ActivitySecondBinding binding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = ActivitySecondBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); replaceFragment(new c_HomeFragment()); binding.bottomNavigationView2.setOnItemSelectedListener(item -> { switch (item.getItemId()){ case R.id.home: replaceFragment(new c_HomeFragment()); break; case R.id.message: replaceFragment(new a_MessageFragment()); break; case R.id.food: replaceFragment(new b_FoodFragment()); break; case R.id.closet: replaceFragment(new d_ClosetFragment()); break; case R.id.question: replaceFragment(new e_QuestionFragment()); break; } return true; }); } private void replaceFragment(Fragment fragment){ FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.frame_layout,fragment); fragmentTransaction.commit(); } }
我的布局文件
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SecondActivity"> <FrameLayout android:id="@+id/frame_layout" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> </FrameLayout> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottomNavigationView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#e8cfa6" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:itemIconTint="@drawable/bottom_navigation_colors" app:itemTextColor="@drawable/bottom_navigation_colors" app:itemTextAppearanceActive="@style/BottomNavigationViewTextStyle" app:itemTextAppearanceInactive="@style/BottomNavigationViewTextStyle" app:menu="@menu/bottom_nav_menu" /> </androidx.constraintlayout.widget.ConstraintLayout>
解决方法
方法一:通过代码主动设置默认选中项
在onCreate方法中,加载完Home Fragment后,调用BottomNavigationView的setSelectedItemId方法,直接指定选中Home对应的ID:
修改后的onCreate代码片段:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = ActivitySecondBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); replaceFragment(new c_HomeFragment()); // 添加此行,设置默认选中Home binding.bottomNavigationView2.setSelectedItemId(R.id.home); binding.bottomNavigationView2.setOnItemSelectedListener(item -> { switch (item.getItemId()){ case R.id.home: replaceFragment(new c_HomeFragment()); break; case R.id.message: replaceFragment(new a_MessageFragment()); break; case R.id.food: replaceFragment(new b_FoodFragment()); break; case R.id.closet: replaceFragment(new d_ClosetFragment()); break; case R.id.question: replaceFragment(new e_QuestionFragment()); break; } return true; }); }
方法二:通过菜单文件调整默认选中行为
BottomNavigationView默认会选中菜单列表中的第一个菜单项,如果你的bottom_nav_menu.xml中Message项排在Home前面,就会默认选中Message。只需将Home项移到菜单的最顶部即可:
示例bottom_nav_menu.xml结构:
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 将Home放在第一个位置 --> <item android:id="@+id/home" android:icon="@drawable/ic_home" android:title="Home"/> <item android:id="@+id/message" android:icon="@drawable/ic_message" android:title="Message"/> <item android:id="@+id/food" android:icon="@drawable/ic_food" android:title="Food"/> <item android:id="@+id/closet" android:icon="@drawable/ic_closet" android:title="Closet"/> <item android:id="@+id/question" android:icon="@drawable/ic_question" android:title="Question"/> </menu>
两种方法都能让导航栏默认选中Home图标,与已加载的Home Fragment状态保持一致。
内容的提问来源于stack exchange,提问作者Xiayan
相关产品推荐
相关产品推荐

