3

我认为我的问题不需要太多解释,我只需要更改上下文菜单项的字体及其大小。我怎么做?

这是我的代码:

@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
      super.onCreateContextMenu(menu, v, menuInfo);
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.listmenu, menu);

    }

这就是我获取android的默认上下文菜单的方式。但我想定制它。

4

3 回答 3

2

您可以创建自己的布局来膨胀而不是 R.menu.listmenu,然后在其上设置自定义字体吗?

或者,您可以尝试拦截用户触摸并弹出一个完全自定义的菜单,但这可能会让期望某种字体/菜单的用户感到不安。

于 2012-07-19T13:52:38.987 回答
2

尝试这个

@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);
}
于 2012-07-19T14:08:41.497 回答
0

经过测试并像魅力一样工作:)

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_feedback_filter, menu);

    for (int i = 0; i < menu.size(); i++) {
        MenuItem mi = menu.getItem(i);
        //for aapplying a font to subMenu ...
        SubMenu subMenu = mi.getSubMenu();
        if (subMenu != null && subMenu.size() > 0) {
            for (int j = 0; j < subMenu.size(); j++) {
                MenuItem subMenuItem = subMenu.getItem(j);
                applyFontToMenuItem(subMenuItem, typeface);
            }
        }
        //the method we have create in activity
        applyFontToMenuItem(mi, typeface);
    }

    return super.onCreateOptionsMenu(menu);
}



private void applyFontToMenuItem(MenuItem mi, Typeface font) {
    SpannableString mNewTitle = new SpannableString(mi.getTitle());
    mNewTitle.setSpan(new CustomTypefaceSpan("", font), 0, mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    mi.setTitle(mNewTitle);
}

自定义跨度类

import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;

public class CustomTypefaceSpan extends TypefaceSpan {

    private final Typeface newType;

    public CustomTypefaceSpan(String family, Typeface type) {
        super(family);
        newType = type;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        applyCustomTypeFace(ds, newType);
    }

    @Override
    public void updateMeasureState(TextPaint paint) {
        applyCustomTypeFace(paint, newType);
    }

    private static void applyCustomTypeFace(Paint paint, Typeface tf) {
        int oldStyle;
        Typeface old = paint.getTypeface();
        if (old == null) {
            oldStyle = 0;
        } else {
            oldStyle = old.getStyle();
        }

        int fake = oldStyle & ~tf.getStyle();
        if ((fake & Typeface.BOLD) != 0) {
            paint.setFakeBoldText(true);
        }

        if ((fake & Typeface.ITALIC) != 0) {
            paint.setTextSkewX(-0.25f);
        }

        paint.setTypeface(tf);
    }
}
于 2017-03-01T13:57:06.123 回答