我正在用Java编写一个Android应用程序,我参考这个视频制作底部导航栏。
输入图片描述
如图所示,当我切换到包含底部导航栏的activity时,fragment显示在home,但是图标仍然显示在message中。我如何使我的图标默认在家里?
这是我的活动,包括底部导航栏:
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>
命名为replaceFragment
replaceFragment(new c_HomeFragment());
binding.bottomNavigationView2.getMenu().findItem(R.id.home).setChecked(true);