Kau*_*hal 6 icons android bottomnavigationview
我想知道如何在用户基本上选择底部导航视图的图标时更改底部导航视图的图标,然后在用户选择不同的选项时再次将其替换为以前的图标。
开关(menuItem.getItemId()){
case R.id.ic_home:
selectedFragment = new HomeFragment();
//menuItem.setIcon(R.drawable.like_colored);
break;
case R.id.ic_connect:
selectedFragment = new ConnectionFragment();
break;
case R.id.ic_add:
selectedFragment = new AddPostFragment();
break;
case R.id.ic_noti:
selectedFragment = new NotificationFragment();
break;
case R.id.ic_profile:
selectedFragment = new ProfileFragment();
break;
Run Code Online (Sandbox Code Playgroud)
San*_*uri 13
如果您想以编程方式执行此操作,请在 switch 语句之前将所有菜单项设置为默认图标。
navigation.getMenu().getItem(0).setIcon(R.drawable.defaultIcon1);
navigation.getMenu().getItem(1).setIcon(R.drawable.defaultIcon2);
navigation.getMenu().getItem(2).setIcon(R.drawable.defaultIcon3);
navigation.getMenu().getItem(3).setIcon(R.drawable.defaultIcon4);
navigation.getMenu().getItem(4).setIcon(R.drawable.defaultIcon5);
switch (menuItem.getItemId()) {
case R.id.ic_home:
selectedFragment = new HomeFragment();
menuItem.setIcon(R.drawable.icon1);
break;
case R.id.ic_connect:
selectedFragment = new ConnectionFragment();
menuItem.setIcon(R.drawable.icon2);
break;
case R.id.ic_add:
selectedFragment = new AddPostFragment();
menuItem.setIcon(R.drawable.icon3);
break;
case R.id.ic_noti:
selectedFragment = new NotificationFragment();
menuItem.setIcon(R.drawable.icon4);
break;
case R.id.ic_profile:
selectedFragment = new ProfileFragment();
menuItem.setIcon(R.drawable.icon5);
break;
}
Run Code Online (Sandbox Code Playgroud)
或者您可以通过编辑 XML 文件而不是以编程方式进行。
drawable/homeIconSelector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/homeNormalIcon" android:state_checked="false"/>
<item android:drawable="@drawable/homeSelectedIcon" android:state_checked="true"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
和你的菜单文件 menu/(menunamehere).xml
<item
android:id="@+id/navigation_home"
android:icon="@drawable/homeIconSelector"
android:title="@string/title_child" />
Run Code Online (Sandbox Code Playgroud)
小智 2
这将从任何地方(例如 onResume)更改单个菜单项的图标和文本颜色。下面的代码在 4.4.2 到(至少)Pie 上运行良好。这是来自这里和其他类似线程的零碎内容。一些注意事项:
static public void setMenuItemProperties(AppCompatActivity activity,
MenuItem item,
int resIconDrawable, int resColor) {
int id = item.getItemId();
BottomNavigationItemView m = activity.findViewById(id);
TextView t1 = m.findViewById(R.id.smallLabel);
TextView t2 = m.findViewById(R.id.largeLabel);
t1.setTextColor(activity.getResources().getColor(resColor));
t2.setTextColor(activity.getResources().getColor(resColor));
Drawable d = VectorDrawableCompat.create(activity.getResources(), resIconDrawable, null);
//Drawable d = activity.getResources().getDrawable(resIconDrawable);
item.setIcon(d);
}
Run Code Online (Sandbox Code Playgroud)
像这样调用(从 Activity)以在菜单项 3 的两个图标和文本颜色之间进行选择。(navigation是 BottomNavigationView。)
setMenuItemProperties(this, navigation.getMenu().getItem(3),
enabled ? R.drawable.ic_settings_red_24dp : R.drawable.ic_settings_redish_24dp,
enabled ? android.R.color.white : R.color.medium_dark_grey);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8590 次 |
| 最近记录: |