您的位置:首页 > 移动开发 > Android开发

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

2016-11-07 11:42 423 查看
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();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android