【问题标题】:How to change size of title's text on Action Bar?如何更改操作栏上标题文本的大小?
【发布时间】:2012-10-15 13:51:55
【问题描述】:

每个 Android 4.0 应用程序上都有一个ActionBar,它有一个标题。我需要减小这个尺寸。但我不明白我该怎么做,因为ActionBar 没有为它提供公共方法。 备注:我不想使用自定义视图。

【问题讨论】:

标签: java android android-actionbar text-size


【解决方案1】:

其实你可以做你想做的自定义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>

【讨论】:

  • 感谢 Aurélien!您应该考虑使用 dp 的 sp inshead 来确定文本大小;-)
  • 您可以考虑从parent="@android:style/TextAppearance.Large" 继承而不是硬编码任何大小。无论您使用什么设备,这都会为您提供标准更大的文本大小。
  • 干得好,我将这种样式方式用于操作栏并更改标题的颜色、大小和外观
【解决方案2】:

你总是可以这样做:

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

【讨论】:

  • 为我工作,使用 Sherlock
  • 使用 html 来设置文本对于 hbad 性能使用风格非常没用 :)
【解决方案3】:

在我的系统上,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>

它有效,我宁愿这样做而不是自定义操作栏。

【讨论】:

    【解决方案4】:

    我已经按照这个方法跳过了自定义样式的创建:

    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>
    

    在这里设置你需要的 textSize。

    2) MainActivity.java

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

    希望这会有所帮助。

    【讨论】:

      【解决方案5】:

      Android ActionBar 中的文本大小是标准

      自 ICS 以来,Android 现在开发了一个标准,以便应用程序(希望)遵循相同的 UI 准则和设计原则。

      http://developer.android.com/guide/practices/ui_guidelines/index.html

      这就是为什么你不能改变它。

      但是,如果您仍想这样做,您可以使用自定义主题或实现 ActionBar(一个分支)并修改其来源:https://github.com/johannilsson/android-actionbar 以获得最大控制。

      【讨论】:

      • 你可以。但你不应该。 Android 的 chrome 由 UI 专业人士设计,他们研究用户如何与设备交互,并优化所有 UI 元素的外观和位置以促进良好的交互。修改 chrome 会降低应用的可用性并让用户感到沮丧。
      【解决方案6】:

      你可以使用

      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");
      

      【讨论】:

      • 解决方案在哪里?
      猜你喜欢
      • 1970-01-01
      • 2013-01-30
      • 2014-07-23
      • 1970-01-01
      • 2015-12-13
      • 2014-12-24
      • 2015-05-08
      • 2011-03-27
      • 2020-11-13
      相关资源
      最近更新 更多