如何更改Android菜单项的自定义字体?

ans*_*992 25 android menuitem

我有以下Android Java和XML代码.我想更改我的应用程序的菜单项的字体.我只知道我们可以使用setTypeface更改TextView的字体,但无论如何都无法找到菜单项.

JAVA代码 - :

@Override
public boolean onCreateOptionsMenu(Menu menu) {    
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}

@Override    
public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {     
        case R.id.action_refresh1:                                          
            Toast.makeText(this, "Item1 Selected", Toast.LENGTH_SHORT)
            .show();      
        break;

        case R.id.action_refresh2:                                          
            Toast.makeText(this, "Item2 Selected", Toast.LENGTH_SHORT)
            .show();      
            break;

        default:      
            break;
   }
}
Run Code Online (Sandbox Code Playgroud)

XML代码 - :

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/action_refresh1"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:title="Item1"/>

<item
    android:id="@+id/action_refresh2"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:title="Item2"/>
</menu>
Run Code Online (Sandbox Code Playgroud)

我想更改两个菜单项的字体,但不知道如何为菜单项集成settypface.

ara*_*aks 20

您必须为菜单项提供自定义布局.

首先,在菜单XML中设置以下之一

如果您使用支持库,则为CASE 1:

<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_save"
        android:title="@string/action_save"
        android:icon="@drawable/ic_action_save"
        android:orderInCategory="100"
        app:showAsAction="always"
        app:actionLayout="@layout/layout_menu_save"/>
</menu>
Run Code Online (Sandbox Code Playgroud)

如果您使用支持库,则为案例2:

<menu
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/action_save"
        android:title="@string/action_save"
        android:icon="@drawable/ic_action_save"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:actionLayout="@layout/layout_menu_save"/>
</menu>
Run Code Online (Sandbox Code Playgroud)

然后在actionLayout(我的例子中的layout_menu_save.xml)中写下以下内容:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@drawable/background_transparent_stateful">

    <com.example.YourCustomTypefaceTextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:gravity="center_vertical"
        android:text="@string/action_save"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:gravity="center_vertical"
        android:src="@drawable/ic_action_save"
        android:contentDescription="@string/action_save"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

背景drawable(background_transparent_stateful)对于在自定义布局上提供触摸反馈非常有用:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true"
        android:drawable="#20FFFFFF">
    </item>
    <item
        android:drawable="@android:color/transparent">
    </item>
</selector>
Run Code Online (Sandbox Code Playgroud)

这样,您将在左侧显示带有自定义字体的文本作为带有右侧图标的标签.

显然你可以根据自己的喜好自定义布局!


编辑:
如果您正在使用支持库并且您的活动主题扩展了AppCompatTheme,您可以获得典型的棒棒糖" 涟漪效应 ",以获得更好看的触摸反馈.只需将自定义背景替换为:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="?attr/selectableItemBackgroundBorderless">
    ...
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

  • 是的,使用支持库.看起来我刚修好了.只需将clickable ="true"和focusable ="true"添加到LinearLayout.然后两者,涟漪效应和点击工作正常.(不要添加focusableInTouchMode) (4认同)
  • 嗯.这种作品,但不是真的(至少在L上).您需要将item.setOnMenuItemClickListener()更改为item.getActionView().setOnClickListener(),并使您的LinearLayout可单击并可聚焦以注册点击.此外,在动画制作后,涟漪效应会卡住. (3认同)

Sum*_*man 13

在styles.xml中创建样式

 <style name="Style_TextView">
            <item name="fontFamily">@font/myriadproregular</item>
 </style>
Run Code Online (Sandbox Code Playgroud)

将以下代码添加到android.support.design.widget.NavigationView中

 app:itemTextAppearance="@style/Style_TextView"
Run Code Online (Sandbox Code Playgroud)

注意:它适用于android.support.design.widget.NavigationView


Sim*_*thi 9

Just add the font of your choice to your base application theme. Note that that makes the selected font the base font for any unspecified font attributes.

    <item name="android:fontFamily">@font/your_font</item>
Run Code Online (Sandbox Code Playgroud)

  • 请记住,如果您有values-21文件夹,那么您也可以在那里进行此更改 (2认同)

Nic*_*hek 8

XML

<?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

        <item
            android:id="@+id/action_edit"
            android:title="@string/edit"
            android:visible="true"
            app:showAsAction="always" />
    </menu>
Run Code Online (Sandbox Code Playgroud)

在活动或片段

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

            Typeface face = Typeface.createFromAsset(getActivity().getAssets(),"fonts/OpenSans-Regular.ttf");    //  THIS
            TypefaceSpan face = new TypefaceSpan("<REPLACE_WITH_FONT_NAME>"); // OR  THIS 
            SpannableStringBuilder title = new SpannableStringBuilder(getContext().getString(R.string.edit));
            title.setSpan(face, 0, title.length(), 0);
            menu.add(Menu.NONE, R.id.action_edit, 0, title); // THIS 
            MenuItem menuItem = menu.findItem(R.id.action_edit); // OR THIS 
            menuItem.setTitle(title);
        super.onCreateOptionsMenu(menu, inflater);
    }
Run Code Online (Sandbox Code Playgroud)