工具栏菜单项文本大小更改

6

以下代码用于更改工具栏的通用标题大小,但我需要更改菜单项文本大小。我尝试了很多问题答案,但它并没有直接影响菜单项大小。我想在项目 app:showAsAction="ifRoom" 时进行更改。你有什么想法吗?

菜单项 (我需要更改这些的大小)

    android:id="@+id/try"
    app:showAsAction="ifRoom"
    android:title="0"
    />

Styles Xml

<style name="ActionBar.nameText" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textSize">50sp</item>
        <item name="android:textStyle">bold</item>
    </style>

Toolbar

android.support.v7.widget.Toolbar

        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
android:titleTextAppearance="@style/ActionBar.nameText"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

        />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:id="@+id/header">


        <Button
            android:id="@+id/level_text"
            android:background="@drawable/levelstar"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="15"
            android:textSize="15sp"
            android:layout_centerHorizontal="true"/>

        />

    </RelativeLayout>

Image

enter image description here


2
可能是如何更改工具栏文本大小?的重复问题。 - basic
谢谢,虽然我之前试过那个方法,但只有标题的大小在工具栏内发生了变化,我需要改变菜单项的文本大小,而不影响菜单项的大小。 - Doğanay Şahin
1个回答

7

您有两种方法可以做到这一点,具体取决于您希望所有内容的外观/功能如何。

选项1:

为整个弹出菜单创建自定义样式。 唯一的缺点是它会改变所有菜单项而不仅仅是一个。

在Styles.xml中创建样式

<style name="MyMenu">
    <item name="android:textSize">17sp</item> //size desired.
    <item name="android:textColor">@color/colorAccent</item> //font color
    <item name="android:background">#FFFFFF</item> //background
</style>

然后将这个样式应用到你的工具栏:

<android.support.v7.widget.Toolbar
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="?attr/actionBarSize"
  android:background="?attr/colorPrimary"
  app:popupTheme="@style/MyMenu" //Important line
/>

结果如下:

结果如下:

enter image description here

选项2

您可以通过编程方式设置单个菜单项的自定义字体和样式。要做到这一点,您只需要知道您想要操作的任何菜单项的索引。

在您的onCreateOptionsMenu中,您需要添加类似于以下内容:

  @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    MenuItem item = menu.getItem(0); //here we are getting our menu item.
    SpannableString s = new SpannableString(item.getTitle()); //get text from our menu item.
    s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0); //here I am just setting a custom color to the menu item. leave this out if you want it left at black.
    s.setSpan(new RelativeSizeSpan(.7f),0,s.length(),0); //here is where we are actually setting the size with a float (proportion).
    item.setTitle(s); //Then just set the menu item to our SpannableString.

    return true;
}

现在它看起来应该是这样的:

这里输入图片描述


1
感谢您的基本解释。我尝试了一下,是可以工作的,但是如果我使用app:showAsAction="never",但是我尝试使用app:showAsAction="ifRoom",这个好像不起作用,至少在屏幕上看起来不是这样,因为当我尝试toast时它是可以工作的。 - Doğanay Şahin
如果您使用ifRoom,您能添加一个截图展示发生了什么吗? - basic
你可能需要让你的主题继承一个合适的父主题。例如:<style name="MyMenu" parent="ThemeOverlay.AppCompat.Light">。否则,一些默认行为将会丢失,比如菜单高亮点击时的涟漪效果。 - Mr-IDE

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接