java - Android工具栏的文字大小和样式

标签 java android xml

我正在开发支持多屏幕的应用程序。我已经根据设备大小完成了所有剩余布局,但我无法增加相同的操作栏/工具栏大小。它在 10 英寸平板电脑中看起来很小。如何增加工具栏文本的大小?

我的样式 XML 如下所示

<resources>

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">

</style>


<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.FullScreen" parent="noActionBar">
<item name="android:windowFullscreen">true</item>
</style>
<style name="noActionBar" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
<style name="OptionDialog" parent="Theme.AppCompat.Dialog.Alert">

    <item name="colorAccent">#C5CAE9</item>
    <item name="android:background">#3F51B5</item>
    <item name="android:textColor">?android:attr/textColorPrimaryInverseDisableOnly</item>
</style>


<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="SelectableItemTheme">
    <item name="colorControlHighlight">#888888</item>
</style>

<style name="SelectableItemBackground">
    <item name="android:theme">@style/SelectableItemTheme</item>
    <item name="android:background">?attr/selectableItemBackground</item>
</style>


<style name="HomeTabTextStyle" 
    parent="TextAppearance.Design.Tab"> 
    <item name="android:textSize">35sp</item> 
    <item name="android:textStyle">bold</item> 
    </style>

<!-- Action Bar Style -->

我的包含工具栏的布局 xml 如下所示

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="110dp"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        app:tabTextAppearance="@style/HomeTabTextStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</android.support.design.widget.AppBarLayout>

我的 Java 代码如下所示

setContentView(R.layout.activity_home);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    setTitle("My App Name");

最佳答案

工具栏就像任何其他布局一样;这意味着您可以像这样在其中添加元素:

     <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/AppTheme.PopupOverlay">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:textColor="@color/colorText"
            android:gravity="center|center_horizontal"
            android:text="@string/title_activity_my_activity"/>

    </android.support.v7.widget.Toolbar>

这将更改工具栏标题的标题大小;如你所愿。

我希望这可以帮助您实现目标!祝你好运。

更新

如果你想手动设置 Activity 标题,你可以像这样为 TextView 设置一个id:

<TextView
      android:id="@+id/title_text"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:textSize="25sp"
      android:textColor="@color/colorText"
      android:gravity="center|center_horizontal"
      android:text="@string/title_activity_my_activity"/>

然后像往常一样,

TextView title = (TextView) toolbar.findViewById(R.id.title_text);
title.setText("New Title");

希望对您有所帮助!

关于java - Android工具栏的文字大小和样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38380785/

相关文章:

java - 检查两个字符串是否相互排列?

java - 如何将第二个锁与 Collections.synchronizedMap 关联

java - 如何在ArrayList集合中循环

android - GeckoView 发送事件或启动函数 (Android)

java - findPreference() 返回 null

c# - 如何使用 Linq 反序列化 xml?

java - 使用 AspectJ 监控 Spring Boot 应用程序中 REST 调用的运行时间

android - 通过 SHA1 指纹字符串模拟 PackageInfo.signatures

php - 通过属性使用 Xpath 解析 xml

java - 如何从 xml 生成 pojo 类(在 jvm 中)?