Android中设置Menu菜单的文字颜色为白色
2016-11-07 11:42
423 查看
Android中设置Menu菜单的文字颜色为白色,一般情况下,Android中Menu菜单的title文字颜色为黑色,
如果在开发应用的过程中,自定义了ActionBar的颜色,比如一些比较鲜艳,清新的颜色,如青色,浅蓝色等
此时如果菜单的文字颜色仍未黑色,就会比较影响UI显示效果,可以通过修改Menu的文字颜色,达到较好的UI
显示效果,共有两种方式可以设置Menu文字颜色:
(一)
通过在style.xml文件里定义相关属性:
复制代码
这个方法对于一般手机而言都是可行的,不过由于各大手机厂商的系统不一样,这个方法并不是万能的,例如
在小米手机上,这个方法就不管用,如果这个方法不管用可以使用下面的第二个方法:
(二)
复制代码
这个方法对于一般的Activity都是可行的,从代码中可以看出,该方法不仅可以设置文字颜色,还可以设置更多相关属性
不过如果用的是FragmentActivity,上面这个方法就不管用了,不过对于FragmentActivity的更改方式与上面的代码
也很相似,下面是其实现:
public static void setFragmentActivityMenuColor(FragmentActivity context) {
final LayoutInflater layoutInflater = context.getLayoutInflater();
final LayoutInflater.Factory existingFactory = layoutInflater.getFactory();
try {
Field field = LayoutInflater.class.getDeclaredField(“mFactorySet”);
field.setAccessible(true);
field.setBoolean(layoutInflater, false);
context.getLayoutInflater().setFactory(new LayoutInflater.Factory() {
@Override
public View onCreateView(String name, final Context context, AttributeSet attrs) {
if (name.equalsIgnoreCase(“com.android.internal.view.menu.IconMenuItemView”)
|| name.equalsIgnoreCase(“com.android.internal.view.menu.ActionMenuItemView”)) {
View view = null;
// if a factory was already set, we use the returned view
if (existingFactory != null) {
view = existingFactory.onCreateView(name, context, attrs);
if (view == null) {
try {
view = layoutInflater.createView(name, null, attrs);
final View finalView = view;
if (view instanceof TextView) {
new Handler().post(new Runnable() {
public void run() {
((TextView) finalView).setTextColor(context.getResources().getColor(R.color.white));
}
});
}
return finalView;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
return view;
}
return null;
}
});
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
如果在开发应用的过程中,自定义了ActionBar的颜色,比如一些比较鲜艳,清新的颜色,如青色,浅蓝色等
此时如果菜单的文字颜色仍未黑色,就会比较影响UI显示效果,可以通过修改Menu的文字颜色,达到较好的UI
显示效果,共有两种方式可以设置Menu文字颜色:
(一)
通过在style.xml文件里定义相关属性:
<style name="AppTheme" parent="android:Theme.Holo.Light"> .... <item name="android:actionMenuTextColor">@android:color/white</item> </style>
复制代码
这个方法对于一般手机而言都是可行的,不过由于各大手机厂商的系统不一样,这个方法并不是万能的,例如
在小米手机上,这个方法就不管用,如果这个方法不管用可以使用下面的第二个方法:
(二)
public static void setActivityMenuColor(final Activity activity) { activity.getLayoutInflater().setFactory( new android.view.LayoutInflater.Factory() { public View onCreateView(String name, Context context, AttributeSet attrs) { // 指定自定义inflate的对象 if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView") || name.equalsIgnoreCase("com.android.internal.view.menu.ActionMenuItemView")) { try { LayoutInflater f = activity.getLayoutInflater(); final View view = f.createView(name, null, attrs); if (view instanceof TextView) { new Handler().post(new Runnable() { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) public void run() { // 设置背景图片 //view.setBackgroundResource(R.color.login_btn_normal); ((TextView) view).setTextColor(activity.getResources().getColor(R.color.white)); } }); } return view; } catch (InflateException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } return null; } } );
复制代码
这个方法对于一般的Activity都是可行的,从代码中可以看出,该方法不仅可以设置文字颜色,还可以设置更多相关属性
不过如果用的是FragmentActivity,上面这个方法就不管用了,不过对于FragmentActivity的更改方式与上面的代码
也很相似,下面是其实现:
public static void setFragmentActivityMenuColor(FragmentActivity context) {
final LayoutInflater layoutInflater = context.getLayoutInflater();
final LayoutInflater.Factory existingFactory = layoutInflater.getFactory();
try {
Field field = LayoutInflater.class.getDeclaredField(“mFactorySet”);
field.setAccessible(true);
field.setBoolean(layoutInflater, false);
context.getLayoutInflater().setFactory(new LayoutInflater.Factory() {
@Override
public View onCreateView(String name, final Context context, AttributeSet attrs) {
if (name.equalsIgnoreCase(“com.android.internal.view.menu.IconMenuItemView”)
|| name.equalsIgnoreCase(“com.android.internal.view.menu.ActionMenuItemView”)) {
View view = null;
// if a factory was already set, we use the returned view
if (existingFactory != null) {
view = existingFactory.onCreateView(name, context, attrs);
if (view == null) {
try {
view = layoutInflater.createView(name, null, attrs);
final View finalView = view;
if (view instanceof TextView) {
new Handler().post(new Runnable() {
public void run() {
((TextView) finalView).setTextColor(context.getResources().getColor(R.color.white));
}
});
}
return finalView;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
return view;
}
return null;
}
});
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
相关文章推荐
- Android MenuItem 设置文字颜色-TextColor的设置
- Android之自定义 ActionBar 上的菜单(Menu)文字颜色
- android 设置Spinner文字标题颜色 字体大小样式
- 最新历史版本 :android--设置TextView部分文字的颜色和背景(高亮显示)
- Android MenuItem 设置文字颜色-TextColor的设置
- Android 如何给Button上面的文字设置点击后变颜色
- Android-设置控件背景及文字颜色
- android 设置Spinner文字标题颜色 字体大小样式
- android--设置TextView部分文字的颜色和背景(高亮显示)
- iOS状态栏文字颜色设置白色
- Android 设置Spinner文字标题颜色 字体大小样式
- Android 如何给Button上面的文字设置点击后变颜色
- android--设置TextView部分文字的颜色和背景(高亮显示)
- 最新历史版本 :android--设置TextView部分文字的颜色和背景(高亮显示)
- android主题Theme.Holo.Light下如何设置程序的标题字体颜色为白色 分类: Android界面和组件 2014-06-21 14:02 327人阅读 评论(0) 收藏
- android 4.2修改设置菜单的背景颜色
- Android进阶篇-自定义Menu(设置Menu的背景及文字属性)
- android--设置TextView部分文字的颜色和背景(高亮显示)
- android 设置Spinner文字标题颜色 字体大小样式
- SpannableString设置AndroidTextView改变颜色,文字大小,图文混排等等