如何更改菜单项的颜色和放大器;尺寸编程? [英] How to change Menu Item Color & Size programmatically?

查看:170
本文介绍了如何更改菜单项的颜色和放大器;尺寸编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了很多在谷歌和计算器中的一个环节是如何使用的风格和主题,以更改文本的颜色,但我不知道如何在code使用。

I searched a lot in google and found in stackoverflow one link how to change color of text using styles and themes but I dont know how to use in code.

<style name="TextAppearance.Widget.IconMenu.Item" parent="@android:style/TextAppearance.Small"> 
        <item name="android:textColor">#ff0000</item> 
    </style> 

请给我片段为更好的理解。我知道如何使用Factory.In,我们可以发现视图对象来更改菜单项的背景。是否有任何工具来获取菜单项。然后我们就可以改变菜单项的颜色?

Please give me snippet for better understanding. I know how to change the background of menuItem by using Factory.In that we can find the View object. Is there any facility to get menu Item. Then we can change the color of menuItem?

推荐答案

试试这个code键更改背景和文本颜色....

try this code to change background and text color....

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    getLayoutInflater().setFactory(new Factory() {
    @Override
    public View onCreateView(String name, Context context,
                    AttributeSet attrs) {
                if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
                try {
                    LayoutInflater f = getLayoutInflater();
                    final View view = f.createView(name, null, attrs);
                    new Handler().post(new Runnable() {
                            public void run() {
                            // set the background drawable
                                view.setBackgroundResource(R.drawable.my_ac_menu_background);

                            // set the text color
                                ((TextView) view).setTextColor(Color.WHITE);
                            }
                        });
                        return view;
                    } catch (InflateException e) {
                    } catch (ClassNotFoundException e) {
                    }
                }
                return null;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

这篇关于如何更改菜单项的颜色和放大器;尺寸编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆