TextView autoSizeTextType not working in Compat

前端 未结 4 701
一整个雨季
一整个雨季 2021-01-11 12:26

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

相关标签:
4条回答
  • 2021-01-11 12:55

    I wanted to update this thread since android.support.v7 has been migrated to androidX

    I was having problems getting the text to display at all and what fixed it for me was this note about singleLine in one of the comments:

    Side note, make sure not to set android:singleLine="true", then the auto-shrink will not work.

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/tempText"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:maxLines="1"
            android:textColor="@color/white"
            android:textSize="60dp"
            app:autoSizeMinTextSize="20dp"
            app:autoSizeTextType="uniform"
            tools:text="99&#xb0;" />
    
    0 讨论(0)
  • 2021-01-11 13:10

    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"  />
    
    0 讨论(0)
  • 2021-01-11 13:21

    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'
    }
    
    0 讨论(0)
  • 2021-01-11 13:21

    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>
    
    0 讨论(0)
提交回复
热议问题