java - 底部导航视图中选定选项卡的颜色保持不变
问题描述
我正在向项目中添加一个 BottomNavigationView,并且我希望为选定的选项卡设置不同的文本(和图标色调)颜色(以实现使非选定选项卡变灰的效果)。我使用了一个带有 android:state_checked="true" 的颜色选择器资源文件,它工作正常,直到我将 BottomNavigationView.OnNavigationItemSelectedListener 实现到我的 Fragment 类之一。添加该侦听器后,默认选择的选项卡仍然突出显示,即使我选择了不同的选项卡。我尝试了状态选择器的不同属性,但没有任何帮助。
这是我将视图添加到布局的方法:
<android.support.design.widget.BottomNavigationView
android:id="@+id/profileNavBar"
android:layout_alignParentTop="true"
android:background="@color/colorLavander"
app:itemTextColor="@color/nav_item_color"
app:itemIconTint="@color/nav_item_color"
app:menu="@menu/profile_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.BottomNavigationView>
这是我的颜色选择器 .xml 文件:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorWhite" android:state_checked="true" />
<item android:color="@color/colorInactive" />
</selector>
这是我的片段类:
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import org.home.com.gvwrev.R;
public class HomeFragment extends Fragment implements BottomNavigationView.OnNavigationItemSelectedListener {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.home_fragment, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
BottomNavigationView bottomNavigationView = view.findViewById(R.id.profileNavBar);
bottomNavigationView.setOnNavigationItemSelectedListener(this);
displayRevFragment(new RecordRevFragment());
}
public void displayRevFragment(Fragment fragment) {
getFragmentManager()
.beginTransaction()
.replace(R.id.revenueContainer, fragment)
.commit();
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.itemRecord:
fragment = new RecordRevFragment();
break;
case R.id.itemModify:
fragment = new ModifyRevFragment();
break;
case R.id.itemView:
break;
}
if (fragment != null) {
displayRevFragment(fragment);
}
return false;
}
}
请帮忙。
解决方案
问题是你应该return true
在onNavigationItemSelected
方法中选择一个项目时。尝试这个:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
case R.id.itemRecord:
fragment = new RecordRevFragment();
break;
case R.id.itemModify:
fragment = new ModifyRevFragment();
break;
case R.id.itemView:
break;
}
if (fragment != null) {
displayRevFragment(fragment);
return true;
}
return false;
}
推荐阅读
- spring - Spring REST Docs中没有字符串参数构造函数/工厂方法从字符串值('OK')反序列化
- excel - 帮助列摆脱数组公式
- c# - 为什么不能从由相同泛型定义的参数推断出泛型类型作为委托方法的一部分?
- python - 在烧瓶模板jinja2中显示循环
- c# - 如果 X 和 Y 是列表,如何在 LiveCharts 中使用 .AddRange?
- sql - 在 SQL Server 中,如何将字符串值的一部分替换为空白值并以正确的顺序打印列?
- bash - 使用 ps、awk 和 sysctl 计算 CPU 利用率
- postgresql - 查询执行时间过长
- c# - 有没有办法在 OpenApi v3 规范属性中明确设置 nullable : false ?
- php - 自定义或 ApiPlatform 过滤嵌套资源在具有复合主键的实体中不起作用