如何在样式文件中设置Android Support AutoSize TextView?
Great question! I've run into this exact issue before when trying to standardize auto-sizing across an app without repeating layout code. The key thing to remember is that while you use the app: prefix for these attributes in XML layouts, you don't need the prefix in styles—you can reference the raw attribute names defined by the AppCompat library directly.
Here's a step-by-step solution:
1. Ensure You're Using AppCompat Widgets
First, make sure all your TextViews are actually AppCompatTextView (or subclasses like AppCompatButton). The native Android TextView doesn't support the AppCompat auto-sizing features, so this is non-negotiable. You can either:
- Explicitly use
<android.support.v7.widget.AppCompatTextView>in your layouts, or - Let the AppCompat library inflate them automatically (if your app uses an AppCompat theme, this happens by default for most standard widgets).
2. Define Your Auto-Sizing Style
In your res/values/styles.xml, create a style that uses the AppCompat auto-sizing attributes without the app: prefix. These attributes are defined in the AppCompat library's attrs.xml, so they're available directly in styles:
<style name="AutoSizeTextStyle" parent="TextAppearance.AppCompat"> <!-- Enable uniform auto-sizing (supports "none" or "uniform") --> <item name="autoSizeTextType">uniform</item> <!-- Minimum text size --> <item name="autoSizeMinTextSize">12sp</item> <!-- Maximum text size --> <item name="autoSizeMaxTextSize">24sp</item> <!-- Step size for text size adjustments --> <item name="autoSizeStepGranularity">2sp</item> <!-- Optional: Use preset sizes instead of min/max/step --> <!-- <item name="autoSizePresetSizes">@array/autosize_text_sizes</item> --> </style>
If you prefer using preset sizes instead of a range, define an array in res/values/arrays.xml:
<array name="autosize_text_sizes"> <item>12sp</item> <item>16sp</item> <item>20sp</item> <item>24sp</item> </array>
3. Apply the Style
You can apply this style to individual widgets:
<android.support.v7.widget.AppCompatTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This text will auto-size!" style="@style/AutoSizeTextStyle" />
Or, to apply it globally to all text widgets in your app, add it to your app's theme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Other theme attributes --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <!-- Apply auto-sizing to all text views --> <item name="textAppearance">@style/AutoSizeTextStyle</item> <!-- For buttons specifically, use: --> <!-- <item name="buttonTextAppearance">@style/AutoSizeTextStyle</item> --> </style>
Key Notes
- Dependency Check: Make sure your
build.gradle(Module level) includes the AppCompat library (version 26.0.0 or higher, since auto-sizing was added then):dependencies { implementation 'com.android.support:appcompat-v7:28.0.0' // For AndroidX, use: implementation 'androidx.appcompat:appcompat:1.6.1' } - Avoid mixing fixed
android:textSizewith auto-sizing: If you set a fixed text size in your style, the auto-sizing system will still override it based on your min/max/step settings, but it's cleaner to omit fixed sizes when using auto-sizing.
内容的提问来源于stack exchange,提问作者Jose M Lechon

