每个 Android 4.0 应用程序都有一个名为ActionBar
的操作栏,它有一个标题。我需要将其大小缩小,但我不知道如何做到这一点,因为 ActionBar
没有提供公共方法来实现。 备注:我不想使用自定义视图。
每个 Android 4.0 应用程序都有一个名为ActionBar
的操作栏,它有一个标题。我需要将其大小缩小,但我不知道如何做到这一点,因为 ActionBar
没有提供公共方法来实现。 备注:我不想使用自定义视图。
实际上,您可以通过主题来自定义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>
parent="@android:style/TextAppearance.Large"
继承。这将为您提供标准的较大文字大小,无论您使用哪种设备。 - Gábor你可以总是做类似这样的事情:
ActionBar actionBar = getActionBar();
actionBar.setTitle(Html.fromHtml("<small>YOUR TITLE</small>"));
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
这是在 AndroidMainfest.xml 文件中指定的样式。我添加了
。<item name="android:textSize">15sp</item>
它起作用了,我宁愿这样做而不是使用自定义操作栏。
我已经按照以下方法跳过了创建自定义样式的步骤:
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);
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");