如何以编程方式更改底部导航视图的图标?
How to programmatically change Bottom Navigation View's Icons?
我想知道如何在用户基本上选择底部导航视图时更改它的图标,然后在用户选择不同选项时再次将其替换为以前的图标。
下面是我的开关案例片段。
切换 (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;
如果您想以编程方式执行此操作,请在 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;
}
或者
您可以通过编辑 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>
和你的菜单文件
菜单/(菜单名称在这里)。xml
<item
android:id="@+id/navigation_home"
android:icon="@drawable/homeIconSelector"
android:title="@string/title_child" />
这将从任何地方(例如 onResume)更改单个菜单项的图标和文本颜色。下面的代码通过(至少)Pie 在 4.4.2 上运行良好。这是来自这里和其他类似线程的点点滴滴。一些注意事项:
- 菜单项的图标是一个名为 "icon" 的可绘制对象,但是像我更改文本颜色那样简单地更改它并不完全正确 - 有时 [=32] 中的图标(如果有的话) =] 出现。这似乎总是有效。
- 它被写成一个静态函数,所以它可以从多个片段中调用;您可以轻松地使其成为成员并删除 activity class.
中的 activity 参数
- drawable 取决于它是矢量还是位图 drawable for older APIs: bit map (and new API) code is a comment;较新的 API 不需要特定的矢量可绘制函数。
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);
}
像这样调用(从 Activity)到 select 菜单项 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);
让我们试试这个方法。
在您的可绘制文件夹中创建一个 xml 文件。例如,xml 文件名是 home_selector.xml 在 drawable 文件夹。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/child" android:state_checked="false"/>
<item android:drawable="@drawable/child_fill" android:state_checked="true"/>
</selector>
现在在 bottom_navigation_main.xml 的菜单项中添加 home_selector
喜欢:android:icon="@drawable/child_selector"
示例:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_child"
android:icon="@drawable/child_selector"
android:title="@string/title_child" />
</menu>
完成,试一试。
谢谢
如果你想改变菜单栏图标,试试这个代码
BottomNavigationView navigation = FindViewById<BottomNavigationView>Resource.Id.navigation); IMenu i=navigation.Menu;IMenuItem ii= i.FindItem(Resource.Id.navigation_home); ii.SetIcon(Resource.Drawable.not_connected);
如果您将底部导航与导航组件一起使用,那么它将帮助您..
我想知道如何在用户基本上选择底部导航视图时更改它的图标,然后在用户选择不同选项时再次将其替换为以前的图标。
切换 (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;
如果您想以编程方式执行此操作,请在 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;
}
或者 您可以通过编辑 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>
和你的菜单文件 菜单/(菜单名称在这里)。xml
<item
android:id="@+id/navigation_home"
android:icon="@drawable/homeIconSelector"
android:title="@string/title_child" />
这将从任何地方(例如 onResume)更改单个菜单项的图标和文本颜色。下面的代码通过(至少)Pie 在 4.4.2 上运行良好。这是来自这里和其他类似线程的点点滴滴。一些注意事项:
- 菜单项的图标是一个名为 "icon" 的可绘制对象,但是像我更改文本颜色那样简单地更改它并不完全正确 - 有时 [=32] 中的图标(如果有的话) =] 出现。这似乎总是有效。
- 它被写成一个静态函数,所以它可以从多个片段中调用;您可以轻松地使其成为成员并删除 activity class. 中的 activity 参数
- drawable 取决于它是矢量还是位图 drawable for older APIs: bit map (and new API) code is a comment;较新的 API 不需要特定的矢量可绘制函数。
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);
}
像这样调用(从 Activity)到 select 菜单项 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);
让我们试试这个方法。 在您的可绘制文件夹中创建一个 xml 文件。例如,xml 文件名是 home_selector.xml 在 drawable 文件夹。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/child" android:state_checked="false"/>
<item android:drawable="@drawable/child_fill" android:state_checked="true"/>
</selector>
现在在 bottom_navigation_main.xml 的菜单项中添加 home_selector 喜欢:android:icon="@drawable/child_selector" 示例:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_child"
android:icon="@drawable/child_selector"
android:title="@string/title_child" />
</menu>
完成,试一试。 谢谢
如果你想改变菜单栏图标,试试这个代码
BottomNavigationView navigation = FindViewById<BottomNavigationView>Resource.Id.navigation); IMenu i=navigation.Menu;IMenuItem ii= i.FindItem(Resource.Id.navigation_home); ii.SetIcon(Resource.Drawable.not_connected);
如果您将底部导航与导航组件一起使用,那么它将帮助您..