我已经添加了BottomNavigationView,当我点击任何一个标签时,只有Fragment被替换,它没有显示新的选择的标签图标和标题(主标签总是显示为选择的)。当我点击任何一个标签页时,只有Fragment被替换,它不显示新的选定标签页图标和标题(Home标签页总是显示为选定),这是什么问题?
我的XML布局 - MainActivity.xml。
<RelativeLayout 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">
<FrameLayout
android:id="@+id/FragmentContainerFrameLayout"
android:layout_above="@id/bottomNavAppBar"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/bottomNavAppBar"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.bottomnavigation.BottomNavigationView
app:labelVisibilityMode="unlabeled"
android:id="@+id/bottomNavgView"
app:menu="@menu/bottom_nav"
android:background="?android:attr/windowBackground"
app:itemTextColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.appbar.AppBarLayout>
</RelativeLayout>
我的java代码 - MainActivity.java。
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
BottomNavigationView bottomNavigationView;
Fragment selectedFragment;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView=findViewById(R.id.bottomNavgView);
bottomNavigationView.setOnNavigationItemSelectedListener(this);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId())
{
case R.id.homeMenu:
selectedFragment=new HomeFragment();
break;
case R.id.searchMenu:
selectedFragment=new SearchFragment();
break;
case R.id.addPostMenu:
selectedFragment=null;
startActivity(new Intent(MainActivity.this,AddPostActivity.class));
break;
case R.id.likesMenu:
selectedFragment=new LikesFragment();
break;
case R.id.profileMenu:
selectedFragment=new ProfileFragment();
break;
}
if(selectedFragment!=null)
{
FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.FragmentContainerFrameLayout,selectedFragment);
transaction.commit();
}
return false;
}
}
颜色文件 - @menubottom_nav
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/black" android:state_checked="true"/>
<item android:color="@android:color/darker_gray"/>
</selector>
Gradle文件
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
菜单文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/homeMenu"
android:title="Home"
android:icon="@drawable/ic_home"></item>
<item
android:id="@+id/searchMenu"
android:title="Search"
android:icon="@drawable/ic_search"></item>
<item
android:id="@+id/addPostMenu"
android:title="Add Post"
android:icon="@drawable/ic_add_post"></item>
<item
android:id="@+id/likesMenu"
android:title="Likes"
android:icon="@drawable/ic_like"></item>
<item
android:id="@+id/profileMenu"
android:title="Profile"
android:icon="@drawable/ic_profile"></item>
</menu>
我没有进行过什么出格的事情,为什么会出现这种情况?请帮助我。
第一个菜单项的图标总是被选中,而不是改变。
适当的片段被替换,但不显示图标和标题。
你必须返回 true
在 onNavigationItemSelected
方法。
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
...
return true;
}
你可以检查 doc:
返回
boolean
true显示为选中的项目,false显示为不应该被选中的项目。. 考虑将不可选择的项目预先设置为禁用,使它们看起来不具有交互性。
此外,您还使用了 app:labelVisibilityMode="unlabeled"
. 这意味着,所有导航项的标签都是隐藏的 (LABEL_VISIBILITY_UNLABELED
)