如何在Action Bar上更改标题文本的大小?

mal*_*uri 38 java android text-size android-actionbar

有一个ActionBar在每个Android 4.0的应用程序,它得到了一个称号.我需要减小这个尺寸.但我不明白我是怎么做到的,因为ActionBar它没有为它提供公共方法.备注:我不想使用自定义视图.

Aur*_*ard 49

实际上,您可以执行自定义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>
Run Code Online (Sandbox Code Playgroud)

  • 您可以考虑继承`parent ="@ android:style/TextAppearance.Large"而不是硬编码任何大小.无论您使用什么设备,这都会为您提供标准的更大文字尺寸. (8认同)
  • 谢谢Aurélien!您应该考虑使用dp的sp inshead作为文本大小;-) (2认同)

Mat*_*t W 20

你总是可以这样做:

ActionBar actionBar = getActionBar();
actionBar.setTitle(Html.fromHtml("<small>YOUR TITLE</small>"));
Run Code Online (Sandbox Code Playgroud)


ste*_*ith 7

在我的系统上,res/styles AppTheme有一个说明

<!-- All customizations that are NOT specific to a particular API-level can go here. -->
Run Code Online (Sandbox Code Playgroud)

这是AndroidMainfest.xml中指示的样式.我补充道

<item name="android:textSize">15sp</item>
Run Code Online (Sandbox Code Playgroud)

并且它工作,我宁愿做这个比自定义操作栏.


Ste*_*raj 5

我已遵循此方法跳过自定义样式的创建:

  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>
Run Code Online (Sandbox Code Playgroud)

在此设置所需的textSize。

2)MainActivity.java

    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); //bellow setSupportActionBar(toolbar);
    getSupportActionBar().setCustomView(R.layout.titlebar);
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。


shk*_*der 3

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 元素的外观和位置以促进良好的交互。修改镶边会降低应用程序的可用性并使用户感到沮丧。 (4认同)