如何在Android中的菜单项中创建带有字体的自定义PopupMenu

时间:2017-05-15 10:37:07

标签: android menuitem android-fonts

我想更改弹出菜单项的默认字体,并使用我的自定义字体。有可能吗? 这是我用于创建弹出菜单的代码:

<item
    android:id="@+id/Setting"
    android:title="Setting"/>
<item
    android:id="@+id/About"
    android:title="About"/>
<item
    android:id="@+id/Help"
    android:title="Help"/>

我想使用自定义字体(ttf)文件更改项目的字体。

1 个答案:

答案 0 :(得分:3)

你可以为此弹出PopupWindow,检查以下代码:

pop_layout.xml

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity"
            android:orientation="vertical"
            android:padding="5dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginTop="22dp"
                android:text="Item 1"
                android:id="@+id/one"
                android:textAppearance="?android:attr/textAppearanceMedium" />
            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:id="@+id/two"
                android:layout_marginTop="22dp"
                android:text="Item 2"
                android:textAppearance="?android:attr/textAppearanceMedium" />
            <TextView
                android:id="@+id/three"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginTop="22dp"
                android:text="Item 3"
                android:textAppearance="?android:attr/textAppearanceMedium" />
            <TextView
                android:id="@+id/four"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginTop="22dp"
                android:text="Item 4"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </LinearLayout>

PopupWindow:

                    LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    final View popupView = layoutInflater.inflate(R.layout.pop_layout, null);


                    TextView one = (TextView)popupView.findViewById(R.id.one);
                    TextView two = (TextView)popupView.findViewById(R.id.two);
                    TextView three = (TextView)popupView.findViewById(R.id.three);
                    TextView four = (TextView)popupView.findViewById(R.id.four);
                    // Do your customised stuff

                    PopupWindow  popupWindow = new PopupWindow(
                            popupView,
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);

                    popupWindow.setBackgroundDrawable(new BitmapDrawable());
                    popupWindow.setOutsideTouchable(true);
                    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                        @Override
                        public void onDismiss() {
                            //TODO do sth here on dismiss
                        }
                    });
                    popupWindow.showAsDropDown(view);// your view instance in which click you want to show menu