简体   繁体   中英

How do you set the action bar and title window text size for multiple screen sizes?

I want to set the text size of the action bar based on the screen size and resolution such as 4" 480 x 800 hdpi. I'm at the end of my app development so now I'm just making sure the app layout fits in all the multiple android screen sizes. The problem is that I can't set the title size per layout such as layout-normal-hdpi and layout-normal-ldpi so in some device layouts the text I place in the title text runs out of space since the text is too big when test it on an emulator. Here's the styles.xml code.

<style name="ActionBar" parent="android:style/Widget.Holo.ActionBar">
<item name="android:background">@color/title_background</item>
<item name="android:titleTextStyle">@style/TextMedium</item>
<item name="android:displayOptions">showTitle</item>
</style>

<style name="TextMedium">
    <item name="android:textSize">16sp</item>
</style>

I think the best aborgem for this problem type, and the value in dimension

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="font_size">16sp</dimen>
</resources>

//change to this

<style name="TextMedium">
    <item name="android:textSize">@dimen/font_size</item>
</style>

Edited:

see more here : https://developer.android.com/training/basics/actionbar/styling.html

<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="@style/Theme.AppCompat.Light.DarkActionBar">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

    <!-- the theme applied to the application or activity -->
    <style name="AppThemeApp" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@color/background_actionbar</item>
        <item name="background">@color/background_actionbar</item>
        <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>

        <!-- Support library compatibility -->
        <item name="titleTextStyle">@style/MyActionBarTitleText</item>
    </style>

    <!-- ActionBar title text -->
    <style name="MyActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/actionbar_text</item>
        <item name="android:textSize">@dimen/font_size</item>
        <!-- The textColor property is backward compatible with the Support Library -->
    </style>
</resources>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM