Android:向选项菜单添加文本颜色、背景和字体大小

发布于 2024-11-14 04:43:16 字数 1048 浏览 6 评论 0原文

我尝试了很多次为我的选项菜单添加字体大小文本颜色背景,但无法能够解决,如何对以下代码执行此操作?始终感谢帮助......!,谢谢。

 @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);

        menu.add("Share In Heart Club!");
        menu.add("Play");
        menu.add("Check Your Heart Type?");
        menu.add("Cancel");


        // Return true so that the menu gets displayed.
        return true;
    }

     @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            // Close the menu after a period of time.
            // Note that this STARTS the timer when the options menu is being
            // prepared, NOT when the menu is made visible.
            Timer timing = new Timer();
            timing.schedule(new TimerTask() {

                @Override
                public void run() {
                    closeOptionsMenu();
                }
            }, 10000);
            return super.onPrepareOptionsMenu(menu);
        }

I tried a lot to add the font size,text color and background for my options menu but couldn't able to solve, how to do this for the following code?Help is always appreciated.....!, Thanks.

 @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);

        menu.add("Share In Heart Club!");
        menu.add("Play");
        menu.add("Check Your Heart Type?");
        menu.add("Cancel");


        // Return true so that the menu gets displayed.
        return true;
    }

     @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            // Close the menu after a period of time.
            // Note that this STARTS the timer when the options menu is being
            // prepared, NOT when the menu is made visible.
            Timer timing = new Timer();
            timing.schedule(new TimerTask() {

                @Override
                public void run() {
                    closeOptionsMenu();
                }
            }, 10000);
            return super.onPrepareOptionsMenu(menu);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

|煩躁 2024-11-21 04:43:16

尝试下面的代码

@Override
        public boolean onCreateOptionsMenu(android.view.Menu menu) {
            // MenuInflater inflater = getMenuInflater();
            // inflater.inflate(R.menu.menu, menu);
            // setContentView(R.layout.menu);
            menu.clear();
            setMenuBackground();
            menu.add(0, MobilePagesConstant.MenuConstant.MENU_ABOUT, 0,
                    R.string.menu_about).setIcon(R.drawable.ico_about);

            menu.add(0, MobilePagesConstant.MenuConstant.MENU_INDEX, 1,
                    R.string.menu_index).setIcon(R.drawable.ico_help);

            // if (HNHConstant.IS_LAST_LIST_ON == 1) {
            menu.add(0, MobilePagesConstant.MenuConstant.MENU_CUSTOMER_SUPPORT, 2,
                    R.string.menu_customer_support).setIcon(
                    R.drawable.ico_customer_support);

            menu.add(0, MobilePagesConstant.MenuConstant.MENU_LEGAL, 3,
                    R.string.menu_legal).setIcon(R.drawable.ico_moderate);
            menu.add(0, MobilePagesConstant.MenuConstant.MENU_GETTING_STARTED, 0,
                    R.string.menu_getting_started).setIcon(
                    R.drawable.ico_gettingstarted);

            return true;

        }





 private void setMenuBackground() {
            // TODO Auto-generated method stub
            getLayoutInflater().setFactory(new Factory() {

                @Override
                public View onCreateView(final String name, final Context context,
                        final AttributeSet attrs) {

                    if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {

                        try { // Ask our inflater to create the view
                            final LayoutInflater f = getLayoutInflater();
                            final View[] view = new View[1];
                            try {
                                view[0] = f.createView(name, null, attrs);
                            } catch (InflateException e) {
                                chaneDeafultMenu(name, attrs, f, view);
                            }
                            // Kind of apply our own background
                            new Handler().post(new Runnable() {
                                @Override
                                public void run() {

                                    view[0].setBackgroundResource(R.drawable.background);
                                    ((TextView) view[0]).setTextColor(Color.BLACK);
                                    ((TextView) view[0])
                                            .setTypeface(Typeface.SANS_SERIF);
                                }
                            });
                            return view[0];
                        } catch (InflateException e) {
                        } catch (ClassNotFoundException e) {

                        }
                    }
                    return null;
                }
            });
        }

        static void chaneDeafultMenu(final String name,
                final android.util.AttributeSet attrs, final LayoutInflater f,
                final View[] view) {
            // mConstructorArgs[0] is only non-null during a running call to
            // inflate()
            // so we make a call to inflate() and inside that call our dully
            // XmlPullParser get's called
            // and inside that it will work to call
            // "f.createView( name, null, attrs );"!
            try {
                f.inflate(new XmlPullParser() {
                    @Override
                    public int next() throws XmlPullParserException, IOException {
                        try {
                            view[0] = f.createView(name, null, attrs);
                        } catch (InflateException e) {
                        } catch (ClassNotFoundException e) {
                        }
                        throw new XmlPullParserException("exit");
                    }

                    @Override
                    public void defineEntityReplacementText(String arg0, String arg1)
                            throws XmlPullParserException {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public int getAttributeCount() {
                        // TODO Auto-generated method stub
                        return 0;
                    }

                    @Override
                    public String getAttributeName(int arg0) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getAttributeNamespace(int arg0) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getAttributePrefix(int arg0) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getAttributeType(int arg0) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getAttributeValue(int arg0) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getAttributeValue(String arg0, String arg1) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public int getColumnNumber() {
                        // TODO Auto-generated method stub
                        return 0;
                    }

                    @Override
                    public int getDepth() {
                        // TODO Auto-generated method stub
                        return 0;
                    }

                    @Override
                    public int getEventType() throws XmlPullParserException {
                        // TODO Auto-generated method stub
                        return 0;
                    }

                    @Override
                    public boolean getFeature(String arg0) {
                        // TODO Auto-generated method stub
                        return false;
                    }

                    @Override
                    public String getInputEncoding() {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public int getLineNumber() {
                        // TODO Auto-generated method stub
                        return 0;
                    }

                    @Override
                    public String getName() {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getNamespace() {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getNamespace(String arg0) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public int getNamespaceCount(int arg0)
                            throws XmlPullParserException {
                        // TODO Auto-generated method stub
                        return 0;
                    }

                    @Override
                    public String getNamespacePrefix(int arg0)
                            throws XmlPullParserException {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getNamespaceUri(int arg0)
                            throws XmlPullParserException {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getPositionDescription() {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getPrefix() {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public Object getProperty(String arg0) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getText() {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public char[] getTextCharacters(int[] arg0) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public boolean isAttributeDefault(int arg0) {
                        // TODO Auto-generated method stub
                        return false;
                    }

                    @Override
                    public boolean isEmptyElementTag()
                            throws XmlPullParserException {
                        // TODO Auto-generated method stub
                        return false;
                    }

                    @Override
                    public boolean isWhitespace() throws XmlPullParserException {
                        // TODO Auto-generated method stub
                        return false;
                    }

                    @Override
                    public int nextTag() throws XmlPullParserException, IOException {
                        // TODO Auto-generated method stub
                        return 0;
                    }

                    @Override
                    public String nextText() throws XmlPullParserException,
                            IOException {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public int nextToken() throws XmlPullParserException,
                            IOException {
                        // TODO Auto-generated method stub
                        return 0;
                    }

                    @Override
                    public void require(int arg0, String arg1, String arg2)
                            throws XmlPullParserException, IOException {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void setFeature(String arg0, boolean arg1)
                            throws XmlPullParserException {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void setInput(Reader arg0) throws XmlPullParserException {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void setInput(InputStream arg0, String arg1)
                            throws XmlPullParserException {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void setProperty(String arg0, Object arg1)
                            throws XmlPullParserException {
                        // TODO Auto-generated method stub

                    }
                }, null, false);
            } catch (InflateException e1) {
                // "exit" ignored
            }
        }

Try the following code

@Override
        public boolean onCreateOptionsMenu(android.view.Menu menu) {
            // MenuInflater inflater = getMenuInflater();
            // inflater.inflate(R.menu.menu, menu);
            // setContentView(R.layout.menu);
            menu.clear();
            setMenuBackground();
            menu.add(0, MobilePagesConstant.MenuConstant.MENU_ABOUT, 0,
                    R.string.menu_about).setIcon(R.drawable.ico_about);

            menu.add(0, MobilePagesConstant.MenuConstant.MENU_INDEX, 1,
                    R.string.menu_index).setIcon(R.drawable.ico_help);

            // if (HNHConstant.IS_LAST_LIST_ON == 1) {
            menu.add(0, MobilePagesConstant.MenuConstant.MENU_CUSTOMER_SUPPORT, 2,
                    R.string.menu_customer_support).setIcon(
                    R.drawable.ico_customer_support);

            menu.add(0, MobilePagesConstant.MenuConstant.MENU_LEGAL, 3,
                    R.string.menu_legal).setIcon(R.drawable.ico_moderate);
            menu.add(0, MobilePagesConstant.MenuConstant.MENU_GETTING_STARTED, 0,
                    R.string.menu_getting_started).setIcon(
                    R.drawable.ico_gettingstarted);

            return true;

        }





 private void setMenuBackground() {
            // TODO Auto-generated method stub
            getLayoutInflater().setFactory(new Factory() {

                @Override
                public View onCreateView(final String name, final Context context,
                        final AttributeSet attrs) {

                    if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {

                        try { // Ask our inflater to create the view
                            final LayoutInflater f = getLayoutInflater();
                            final View[] view = new View[1];
                            try {
                                view[0] = f.createView(name, null, attrs);
                            } catch (InflateException e) {
                                chaneDeafultMenu(name, attrs, f, view);
                            }
                            // Kind of apply our own background
                            new Handler().post(new Runnable() {
                                @Override
                                public void run() {

                                    view[0].setBackgroundResource(R.drawable.background);
                                    ((TextView) view[0]).setTextColor(Color.BLACK);
                                    ((TextView) view[0])
                                            .setTypeface(Typeface.SANS_SERIF);
                                }
                            });
                            return view[0];
                        } catch (InflateException e) {
                        } catch (ClassNotFoundException e) {

                        }
                    }
                    return null;
                }
            });
        }

        static void chaneDeafultMenu(final String name,
                final android.util.AttributeSet attrs, final LayoutInflater f,
                final View[] view) {
            // mConstructorArgs[0] is only non-null during a running call to
            // inflate()
            // so we make a call to inflate() and inside that call our dully
            // XmlPullParser get's called
            // and inside that it will work to call
            // "f.createView( name, null, attrs );"!
            try {
                f.inflate(new XmlPullParser() {
                    @Override
                    public int next() throws XmlPullParserException, IOException {
                        try {
                            view[0] = f.createView(name, null, attrs);
                        } catch (InflateException e) {
                        } catch (ClassNotFoundException e) {
                        }
                        throw new XmlPullParserException("exit");
                    }

                    @Override
                    public void defineEntityReplacementText(String arg0, String arg1)
                            throws XmlPullParserException {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public int getAttributeCount() {
                        // TODO Auto-generated method stub
                        return 0;
                    }

                    @Override
                    public String getAttributeName(int arg0) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getAttributeNamespace(int arg0) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getAttributePrefix(int arg0) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getAttributeType(int arg0) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getAttributeValue(int arg0) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getAttributeValue(String arg0, String arg1) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public int getColumnNumber() {
                        // TODO Auto-generated method stub
                        return 0;
                    }

                    @Override
                    public int getDepth() {
                        // TODO Auto-generated method stub
                        return 0;
                    }

                    @Override
                    public int getEventType() throws XmlPullParserException {
                        // TODO Auto-generated method stub
                        return 0;
                    }

                    @Override
                    public boolean getFeature(String arg0) {
                        // TODO Auto-generated method stub
                        return false;
                    }

                    @Override
                    public String getInputEncoding() {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public int getLineNumber() {
                        // TODO Auto-generated method stub
                        return 0;
                    }

                    @Override
                    public String getName() {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getNamespace() {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getNamespace(String arg0) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public int getNamespaceCount(int arg0)
                            throws XmlPullParserException {
                        // TODO Auto-generated method stub
                        return 0;
                    }

                    @Override
                    public String getNamespacePrefix(int arg0)
                            throws XmlPullParserException {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getNamespaceUri(int arg0)
                            throws XmlPullParserException {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getPositionDescription() {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getPrefix() {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public Object getProperty(String arg0) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public String getText() {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public char[] getTextCharacters(int[] arg0) {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public boolean isAttributeDefault(int arg0) {
                        // TODO Auto-generated method stub
                        return false;
                    }

                    @Override
                    public boolean isEmptyElementTag()
                            throws XmlPullParserException {
                        // TODO Auto-generated method stub
                        return false;
                    }

                    @Override
                    public boolean isWhitespace() throws XmlPullParserException {
                        // TODO Auto-generated method stub
                        return false;
                    }

                    @Override
                    public int nextTag() throws XmlPullParserException, IOException {
                        // TODO Auto-generated method stub
                        return 0;
                    }

                    @Override
                    public String nextText() throws XmlPullParserException,
                            IOException {
                        // TODO Auto-generated method stub
                        return null;
                    }

                    @Override
                    public int nextToken() throws XmlPullParserException,
                            IOException {
                        // TODO Auto-generated method stub
                        return 0;
                    }

                    @Override
                    public void require(int arg0, String arg1, String arg2)
                            throws XmlPullParserException, IOException {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void setFeature(String arg0, boolean arg1)
                            throws XmlPullParserException {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void setInput(Reader arg0) throws XmlPullParserException {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void setInput(InputStream arg0, String arg1)
                            throws XmlPullParserException {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void setProperty(String arg0, Object arg1)
                            throws XmlPullParserException {
                        // TODO Auto-generated method stub

                    }
                }, null, false);
            } catch (InflateException e1) {
                // "exit" ignored
            }
        }
Bonjour°[大白 2024-11-21 04:43:16

http://www.droidnova.com/how-to -创建选项菜单,427.html
如果您觉得有用,请检查链接并投票

http://www.droidnova.com/how-to-create-an-option-menu,427.html
Please check the link and vote up if u find it useful

若相惜即相离 2024-11-21 04:43:16

在 Android 中,将样式/主题应用到选项菜单并不容易。不过,有一篇很棒的文章向您展示了如何实现您自己的。

设置背景颜色是可以的,但是在 2.3 上有很多 bug。请参阅如何更改选项菜单的背景颜色? 了解更多信息。

Applying styles / themes to the options menu is not easy in Android. However, there is a great article which shows you how to implement your own.

Setting the background colour is possible, but it's quite buggy on 2.3. See How to change the background color of the options menu? for more information.

<逆流佳人身旁 2024-11-21 04:43:16

使用您最喜欢的图标和颜色创建自定义 xml 视图。使用 menuinflator 将它们扩展到选项菜单。

Create a custom xml view with your fav icons and colors. Use menuinflator to inflate them to options menu.

绝不服输 2024-11-21 04:43:16
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    // getMenuInflater().inflate(R.menu.splash_screen, menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.payby_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);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    // getMenuInflater().inflate(R.menu.splash_screen, menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.payby_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);
}
属性 2024-11-21 04:43:16

1:您只需书写即可简单地为文本添加颜色
安卓:textColor =“#ff00ff”
在布局 xml 中的 textview 标签

或第二种方法中,

您可以使用以下方法添加颜色
value->string.xml 并向字符串添加颜色资源

1: you can simply add color to text by just writing
android:textColor="#ff00ff"
in your layout xml in textview tag

or second method is

you can add color by using
values->string.xml and adding a color resource to your string

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文