TextView autoSizeTextType not working in Compat

扶醉桌前 提交于 2019-12-08 17:03:38

问题


I tried using autoSizeTextType. My minSdk is 24, all the tools are 26 and compat ist 26-beta2 as well.

Making them adjustable was tried through code:

dialogWeight.touchables.filterIsInstance<Button>().forEach {
TextViewCompat.setAutoSizeTextTypeWithDefaults(it, 
TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM) }

And xml:

<android.support.v7.widget.AppCompatTextView
android:layout_height="match_parent"
android:text="7"
android:autoSizeTextType="uniform"/>

Any ideas ? I'm starting to believe that it's currently bugged


回答1:


Okay, so the combination of settings that worked :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.v7.widget.AppCompatTextView
android:text="7"
app:autoSizeTextType="uniform"/>

You also need the appcompat-v7 library as a dependency in your module build.gradle file.

dependencies {
    implementation 'com.android.support:appcompat-v7:27.1.1'
}



回答2:


The key thing to understand is to use app:autoSizeTextType, as opposed to android:autoSizeTextType

Per the documentation:

To define the default setting in XML through the support library, use the app namespace and set the autoSizeTextType attribute to none or uniform.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <TextView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:autoSizeTextType="uniform" />

</LinearLayout>



回答3:


I solved it programmatically.

TextView number1 = findViewById(R.id.number_one);
TextViewCompat.setAutoSizeTextTypeWithDefaults(number1, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);

and the XML:

  <TextView
        android:id="@+id/number_one"
        android:autoSizeTextType="uniform"
        android:gravity="center"
        android:text="1"  />


来源:https://stackoverflow.com/questions/45059411/textview-autosizetexttype-not-working-in-compat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!