Android中设置Menu菜单的文字颜色为白色

Android中设置Menu菜单的文字颜色为白色,一般情况下,Android中Menu菜单的title文字颜色为黑色,
如果在开发应用的过程中,自定义了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();

}

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值