[英]Why does BottomNavigationView not changing selected tab Icon?
我添加了底部导航视图。 当我点击任何选项卡时,只有 Fragment 被替换,它不显示新选择的选项卡图标和标题(主页选项卡始终显示为选中状态)有什么问题?
我的 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;
}
}
颜色文件 - @menu/bottom_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>
我还没有表演过世界上的东西,为什么会这样? 请帮我。
始终选择第一个菜单项图标,不改变
正确的片段被替换但不显示图标和标题。
您必须在onNavigationItemSelected
方法中返回true
:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
...
return true;
}
您可以查看文档:
退货
boolean
如果该项目不应该被选择,则将项目显示为已选项目,则为 true 。 考虑将不可选择的项目预先设置为禁用,以使它们看起来非交互。
您也在使用app:labelVisibilityMode="unlabeled"
。
这意味着 label 对所有导航项 ( LABEL_VISIBILITY_UNLABELED
) 都是隐藏的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.