android menuitem 字体颜色,android
android-如何设置MenuItem的图标颜色?
我定义了一个具有ShareActionProvider并共享白色图标的菜单项,如下所示:
android:icon="@drawable/ic_share_white_24dp"
android:id="@+id/action_share"
android:title="@string/action_share"
android:orderInCategory="200"
app:showAsAction="ifRoom"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
但是,当我启动该应用程序时,我得到了另一个黑色共享图标。 如何将共享图标设置为白色?
这是我的结果
Dimitri asked 2020-06-21T14:03:13Z
7个解决方案
66 votes
该图标实际上是由ShareActionProvider提供的,您无法对其进行更改。 但是,您可以通过在styles.xml中设置textColorPrimary来自定义颜色:
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="@style/MyActionBarTheme"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
#fa0
对于任何自定义图标,您都必须自己为它们着色。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
for(int i = 0; i < menu.size(); i++){
Drawable drawable = menu.getItem(i).getIcon();
if(drawable != null) {
drawable.mutate();
drawable.setColorFilter(getResources().getColor(R.color.textColorPrimary), PorterDuff.Mode.SRC_ATOP);
}
}
return true;
}
tachyonflux answered 2020-06-21T14:05:01Z
39 votes
这是一个主题问题。 根据您当前的主题,您需要设置正确的ActionBar叠加主题。 动作提供者读取主题中的值(指示主题是深色还是浅色)以确定图标的颜色。
如果主主题是浅色,而ActionBar是黑暗,则ActionBar /工具栏必须使用主题ThemeOverlay.AppCompat.Dark.ActionBar。
BladeCoder answered 2020-06-21T14:04:36Z
32 votes
试试这个 :
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.MENU, menu);
// change color for icon 0
Drawable yourdrawable = menu.getItem(0).getIcon(); // change 0 with 1,2 ...
yourdrawable.mutate();
yourdrawable.setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_IN);
return true;
}
druci answered 2020-06-21T14:05:20Z
25 votes
简短而甜蜜的答案-> app:iconTint =“ @ color / yourcolor
在MenuItem中添加app:iconTint="@color/yourcolor",以更改图标颜色。
android:icon="@drawable/ic_share_white_24dp"
android:id="@+id/action_share"
android:title="@string/action_share"
android:orderInCategory="200"
app:iconTint="@color/yourcolor"
app:showAsAction="ifRoom"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
Nikunj Paradva answered 2020-06-21T14:05:48Z
3 votes
简短的答案->使用app:iconTint="?android:textColorPrimary"如果您希望图标颜色为白色,请输入:android:theme = "@style/ThemeOverlay.MaterialComponents.Dark.ActionBar"否则,如果要黑色,请输入:android:theme="@style/ThemeOverlay.MaterialComponents.Light"到您的工具栏
answered 2020-06-21T14:06:08Z
1 votes
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
menuInflater.inflate(R.menu.menu_confirm, menu);
MenuItem action_done = menu.findItem(R.id.action_done);
action_done.setIcon(R.drawable.ic_filter);
Utils.menuIconColor(action_done, Color.WHITE);
super.onCreateOptionsMenu(menu, menuInflater);
}
public static void menuIconColor(MenuItem menuItem, int color) {
Drawable drawable = menuItem.getIcon();
if (drawable != null) {
drawable.mutate();
drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
}
}
Ahamadullah Saikat answered 2020-06-21T14:06:23Z
0 votes
这种现象是预期的,因为ShareActionProvider是
负责创建实现数据共享的视图,并负责 如果放置了托管项目,则显示带有共享活动的子菜单 在溢出菜单上。
根据文档。
这意味着使用视图时,您无法控制其自定义。
Robert Estivill answered 2020-06-21T14:06:58Z
发布评论