如何在操作栏上更改标题文本的大小?

42

每个 Android 4.0 应用程序都有一个名为ActionBar的操作栏,它有一个标题。我需要将其大小缩小,但我不知道如何做到这一点,因为 ActionBar 没有提供公共方法来实现。 备注:我不想使用自定义视图。


1
这里有一个漂亮、干净的解决方案:https://dev59.com/Ql4b5IYBdhLWcg3w9Fyr#29266326 - ban-geoengineering
6个回答

51

实际上,您可以通过主题来自定义ActionBar,以达到您想要的效果。您可以像这样做:

<style name="Theme.YourTheme" parent="android:style/Theme">
    <item name="android:actionBarStyle">@style/Theme.YourTheme.Styled.ActionBar</item>
</style>

<style name="Theme.YourTheme.Styled.ActionBar">
    <item name="android:titleTextStyle">@style/Theme.Viadeo.Styled.YourTheme.TitleTextStyle</item>
</style>

<style name="Theme.YourTheme.Styled.ActionBar.TitleTextStyle" parent="@android:style/Widget.TextView">
    <item name="android:textSize">12sp</item>
</style>

2
谢谢Aurélien!你应该考虑使用sp而不是dp来设置文本大小;-) - gingo
8
不要硬编码任何大小,可以考虑从parent="@android:style/TextAppearance.Large"继承。这将为您提供标准的较大文字大小,无论您使用哪种设备。 - Gábor
干得好!我使用这种样式来处理操作栏,改变标题的颜色、大小和外观。 - Milaaaad

23

你可以总是做类似这样的事情:

ActionBar actionBar = getActionBar();
actionBar.setTitle(Html.fromHtml("<small>YOUR TITLE</small>"));

对我来说有效,使用Sherlock。 - Elhanan Mishraky
使用HTML设置文本非常无用,性能也很差,建议使用样式 :) - Hugo Mallet

7
在我的系统上,res/styles AppTheme有一条注释说:
<!-- All customizations that are NOT specific to a particular API-level can go here. -->

这是在 AndroidMainfest.xml 文件中指定的样式。我添加了

<item name="android:textSize">15sp</item>

它起作用了,我宁愿这样做而不是使用自定义操作栏。


6

我已经按照以下方法跳过了创建自定义样式的步骤:

  1. 创建一个带有所需文本大小的自定义布局。
  2. 将其应用于您的操作栏。

1)创建一个布局文件(titlebar.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/action_bar_title"
        android:text="@string/app_name"
        android:textColor="@color/colorPrimary"
        android:textSize="24sp" />
</LinearLayout>

在这里设置所需的文本大小。

2) MainActivity.java

    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); //bellow setSupportActionBar(toolbar);
    getSupportActionBar().setCustomView(R.layout.titlebar);

希望这能有所帮助。

3

4
可以这样做,但不应该。Android的Chrome浏览器是由专业的用户界面设计师设计的,他们研究用户如何与设备交互,并优化所有用户界面元素的外观和位置以促进良好的交互体验。修改Chrome浏览器将使应用程序更难使用并使用户感到沮丧。 - 323go

0

你可以使用

subtitle:"exsample"

XML

 <androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="?attr/actionBarTheme"
    app:subtitle="subTitle"
    app:title = "Title"
    android:tag="dff"/>

JAVA

getSupportActionBar.setSubtitle("Sub Title");

解决方案在哪里? - Fer

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