In version 26.0.1, the Support Library adds support for autosave in AppCompatTextView .
Developers can now let their text size expand or shrink automatically depending on the size and characteristics of the TextView, making it easier to optimize text size on different screens or dynamic content.
Grain
In Java:
Call the setAutoSizeTextTypeUniformWithConfiguration() method:
setAutoSizeTextTypeUniformWithConfiguration(int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit)
In XML:
Use the autoSizeMinTextSize, autoSizeMaxTextSize, and autoSizeStepGranularity attributes to set automatic calibration parameters in the layout XML file:
<android.support.v7.widget.AppCompatTextView android:id="@+id/autosizing_textview_presetsize" android:layout_width="wrap_content" android:layout_height="250dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" app:autoSizeMaxTextSize="100sp" app:autoSizeMinTextSize="12sp" app:autoSizeStepGranularity="2sp" app:autoSizeText="uniform" android:text="Hello World!" android:textSize="100sp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" />
Preset sizes
In Java:
Call the setAutoSizeTextTypeUniformWithPresetSizes() method:
setAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit)
In XML:
Use the autoSizePresetSizes attribute in the XML layout file:
<android.support.v7.widget.AppCompatTextView android:id="@+id/autosizing_textview_presetsize" android:layout_width="wrap_content" android:layout_height="250dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" app:autoSizeText="uniform" app:autoSizePresetSizes="@array/autosize_text_sizes" android:text="Hello World!" android:textSize="100sp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" />
To access the array as a resource, define the array in res / values /arrays.xml:
<array name="autosize_text_sizes"> <item>10sp</item> <item>12sp</item> <item>20sp</item> <item>40sp</item> <item>100sp</item> </array>
Darish
source share