简体   繁体   中英

AppCompatTextView autoSizeTextType not working

In my activity.xml , I have two AppCompatTextView that looks like this:

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/user_full_name_text_view"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="8dp"
    android:fontFamily="@font/roboto_bold"
    android:maxLines="1"
    android:text="@string/full_name"
    android:textColor="@android:color/white"
    app:autoSizeTextType="uniform"
    app:layout_constraintBottom_toTopOf="@+id/guideline"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toEndOf="@+id/profile_image"
    app:layout_constraintTop_toTopOf="parent" />

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/user_name_text_view"
    android:layout_width="0dp"
    android:layout_height="20dp"
    android:layout_marginEnd="8dp"
    android:fontFamily="@font/roboto"
    android:maxLines="1"
    android:text="@string/username"
    android:textColor="@android:color/white"
    app:autoSizeTextType="uniform"
    app:layout_constraintBottom_toTopOf="@+id/guideline"
    app:layout_constraintEnd_toEndOf="@+id/user_view"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="@+id/user_full_name_text_view"
    app:layout_constraintTop_toBottomOf="@+id/user_full_name_text_view"
    app:layout_constraintVertical_bias="0.0" />

In my design tab, this part of the activity looks like this:

Android 设计选项卡

Now that looks correct as the text has correctly auto sized to fill the height of the first AppCompatTextView . However, when I run my app on mobile, it looks like this:

安卓手机设计

Why is the text not auto sizing? How do I fix this so that way the text fills the entire width and height of the AppCompatTextView ?

Auto sizing doesn't play well with wrap_content , probably because it needs to know its size before it can say how big its content will be. Do you have any of that going on anywhere in the hierarchy, like in the layout your AppCompatTextViews are in?

Right now your widths depend on how wide parent is, but if that's wrap_content (or its parent is, etc etc) then eventually that text view is gonna get asked how wide it wants to be, and it gets confused

I guess the problem is that adding app:autoSizeTextType="uniform" is not enough. When you are using uniform type, you should specify step granularity as well. Try to use such attributes below:

<androidx.appcompat.widget.AppCompatTextView
    ...
    app:autoSizeTextType="uniform"
    app:autoSizeMinTextSize="12sp"
    app:autoSizeMaxTextSize="24sp"
    app:autoSizeStepGranularity="1sp"
    ... />

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