如何更改PopupMenu项目字体

Sae*_* N. 11 android popupmenu

我想更改PopupMenu项目的默认字体,并使用我的自定义字体.

这是我用来创建PopupMenu的代码:

PopupMenu pm = new PopupMenu(this, v);
getMenuInflater().inflate(R.menu.main, pm.getMenu());
pm.show();
Run Code Online (Sandbox Code Playgroud)

菜单项:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/Setting"
        android:title="Setting"/>
    <item
        android:id="@+id/About"
        android:title="About"/>
    <item
        android:id="@+id/Help"
        android:title="Help"/>
</menu>
Run Code Online (Sandbox Code Playgroud)

如果你和我分享你的建议,我将非常感激:-)

问候

Raj*_*shi 12

我知道这是一个古老的问题,但仍然想回答,如果像我这样的人偶然发现这个特定的问题,正在寻找一个明确的答案。

不知道AppCompat,但正在玩MaterialComponents并发现了 api textAppearanceLargePopupMenu。举个例子,假设您想为应用PopupMenu程序中的所有s应用特定字体,然后定义 astyle并将其应用于您的theme

示例样式:

<style name="AppTheme.TextAppearance.Popup" parent="TextAppearance.MaterialComponents.Caption">
    <item name="fontFamily">@font/opensans_regular</item>
</style>
Run Code Online (Sandbox Code Playgroud)

示例主题:

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- All other attrs and styles -->
    <item name="textAppearanceLargePopupMenu">@style/AppTheme.TextAppearance.Popup</item>
</style>
Run Code Online (Sandbox Code Playgroud)


Gau*_*pta 10

检查我的解决方案是否存在同

弹出菜单Mehthod:

 private void showEditPopupWindow(Context mContext) {
        PopupMenu popupMenu = new PopupMenu(mContext, view);
        popupMenu.getMenuInflater().inflate(R.menu.YOUR_MENU, popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.delete) {
                  //  Do your stuffs;
                } else {
                  //  Do your stuffs
                }
                return true;
            }
        });

        Menu menu = popupMenu.getMenu();
        for (int i = 0; i < menu.size(); i++) {
            MenuItem mi = menu.getItem(i);
            applyFontToMenuItem(mi);
        }

    }
Run Code Online (Sandbox Code Playgroud)

在此方法中应用您的字体,您也可以更改字体的颜色:

private void applyFontToMenuItem(MenuItem mi) {
        Typeface font = Typeface.createFromAsset(mContext.getAssets(), "fonts/YOUR_FONT.ttf");
        SpannableString mNewTitle = new SpannableString(mi.getTitle());
        mNewTitle.setSpan(new CustomTypeFaceSpan("", font,Color.WHITE), 0, mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        mi.setTitle(mNewTitle);
    }
Run Code Online (Sandbox Code Playgroud)

CustomTypeFaceSpan类:

    public class CustomTypeFaceSpan extends TypefaceSpan {

    private final Typeface newType;
    private final int mColor;

    public CustomTypeFaceSpan(String family, Typeface type, @ColorInt int color) {

        super(family);
        newType = type;
        mColor = color;
    }

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

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

    @Override
    public int getSpanTypeId() {
        return super.getSpanTypeId();
    }

    @ColorInt
    public int getForegroundColor() {
        return mColor;
    }




    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);
    }
}
Run Code Online (Sandbox Code Playgroud)


QAr*_*rea -2

我认为这是不可能的。实际上,您可以使用 popupWindow 并根据需要像菜单一样自定义它。