如何更改动作子菜单标题的样式?

时间:2018-09-15 09:23:31

标签: android menu android-actionbar styles title

如何通过style.xml文件更改此标题的颜色和字体大小?

enter image description here

只知道,有一种简单的隐藏方法吗?

1 个答案:

答案 0 :(得分:-1)

在您的onCreateOptionsMenu

中尝试以下代码
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.your_menu, menu);

    int positionOfMenuItem = 0; // or whatever...
    MenuItem item = menu.getItem(positionOfMenuItem);
    SpannableString s = new SpannableString("Configuration");
    s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0); // changes color to red
    s.setSpan(new RelativeSizeSpan(1.5f), 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // changes font size
    item.setTitle(s);
}

或者如果您想更改整个菜单的样式

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater awesome = getMenuInflater();
    awesome.inflate(R.menu.menu_main, menu);
    for(int i = 0; i < menu.size(); i++) {
        MenuItem item = menu.getItem(i);
        SpannableString spanString = new SpannableString(menu.getItem(i).getTitle().toString());
        int end = spanString.length();
        spanString.setSpan(new RelativeSizeSpan(1.5f), 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        item.setTitle(spanString);
    }
    return true;
}