Android MenuItem 设置文字颜色-TextColor的设置


前面一直在找 MenuItem的文字颜色的设置。我发现API中只有背景颜色的设置。。。所以找到下面的方法。在OverFlow上看到的。

在onCreateOptionsMenu中覆写一下。使MenuItem产生的ItemView去修改文字颜色


@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		  MenuInflater inflater = getMenuInflater();

		  getLayoutInflater().setFactory(new Factory()
		  {

			@Override
            public View onCreateView(String name, Context context,
                    AttributeSet attrs)
            {
				System.out.println(name);
				if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")
					      || name.equalsIgnoreCase("com.android.internal.view.menu.ActionMenuItemView"))
					    {
					     try
					     {
					      LayoutInflater f = getLayoutInflater();
					      final View view = f.createView(name, null, attrs);
					      System.out.println((view instanceof TextView));
					      if(view instanceof TextView){
					       ((TextView)view).setTextColor(Color.GREEN);
					      }
					      return view;
					     } catch (InflateException e)
					     {
					    	 e.printStackTrace();
					     } catch (ClassNotFoundException e)
					     {
					    	 e.printStackTrace();
					     }
					    }
					    return null;
            }
			  
		  });
		  
		  inflater.inflate(R.menu.main, menu);
		  return super.onCreateOptionsMenu(menu);
	}

可以看到MenuItem的颜色成功改变。


<!------ 补充  这是最近发现的。上面的 有些机型不能使用---------------->
看到:http://stackoverflow.com/questions/18015010/action-bar-menu-item-text-color        stackOverFlow上有相关的答案。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过在Toolbar的menu布局文件中设置每个MenuItem的actionLayout属性为一个自定义的单选按钮布局文件,然后在该布局文件中设置单选按钮的颜色。下面是一个示例代码: 首先,在menu布局文件中设置actionLayout属性: ``` <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/my_menu_item" android:title="My Menu Item" app:showAsAction="always" app:actionLayout="@layout/menu_item_radio_group"/> </menu> ``` 然后,创建一个名为menu_item_radio_group.xml的布局文件,其中包含一个RadioGroup和两个RadioButton: ``` <?xml version="1.0" encoding="utf-8"?> <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/radio_button_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 1" android:checked="true" android:textColor="@color/white"/> <RadioButton android:id="@+id/radio_button_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 2" android:textColor="@color/white"/> </RadioGroup> ``` 最后,在Activity的onCreateOptionsMenu方法中获取MenuItem设置它的点击事件: ``` @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.my_menu, menu); MenuItem menuItem = menu.findItem(R.id.my_menu_item); RadioGroup radioGroup = (RadioGroup) menuItem.getActionView(); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // Handle radio button click events here } }); return true; } ``` 注意,这里通过menuItem.getActionView()方法获取了RadioGroup布局,而不是直接调用findViewById方法。这是因为actionLayout属性指定的布局文件不会自动加载到MenuItem中,需要手动获取并添加到MenuItem的ActionView中。 另外,还需要在colors.xml文件中定义单选按钮的颜色: ``` <resources> <color name="white">#FFFFFF</color> </resources> ``` 这样就可以通过设置单选按钮的textColor属性来改变它的颜色了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值