TextView的自动调整功能不起作用(Android O)

时间:2020-12-05 20:55:34

I use new autosize feature added in support library 26. I read a documentation which can be found here : https://developer.android.com/preview/features/autosizing-textview.html

我在支持库26中添加了新的自动调整功能。我阅读了一个文档,可以在这里找到:https://developer.android.com/preview/features/autosizing-textview.html

I suppose that it should work this way: You can enable auto-sizing with this attribute: app:autoSizeTextType="uniform". I think that TextView should use all available space to display a whole text (not just a part - it shouldn't be cropped) and the textSize should be as big as possible. If you need to limit a maximum or minimum size of the text then you can use these two attributes:

我认为它应该以这种方式工作:您可以使用以下属性启用自动调整大小:app:autoSizeTextType =“uniform”。我认为TextView应该使用所有可用空间来显示整个文本(不仅仅是一个部分 - 它不应该被裁剪)并且textSize应该尽可能大。如果您需要限制文本的最大或最小大小,则可以使用以下两个属性:

app:autoSi*TextSize="XXsp" // (you can also use px or dp values.)

or

app:autoSizeMaxTextSize="XXsp"

So far so good. Let's say that I need a TextView with 56dp width. I have texts with a different length and I want to set these texts to this TextView. It should be automatically resized so it displays the whole text (all characters + not cropped) in the biggest possible textSize.

到现在为止还挺好。假设我需要一个宽度为56dp的TextView。我有不同长度的文本,我想将这些文本设置为此TextView。它应该自动调整大小,以便在最大可能的textSize中显示整个文本(所有字符+未裁剪)。

This is my TextView:

这是我的TextView:

<android.support.v7.widget.AppCompatTextView
        android:id="@+id/vName"
        style="@style/TextView.AutoSize"
        android:layout_width="56dp"
        android:gravity="bottom|center_horizontal"
        android:maxLines="1"
        app:autoSi*TextSize="1px"
        app:autoSizeTextType="uniform"
        app:layout_constraintBottom_toTopOf="@id/vGuideline"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

Unfortunately, the text view is cropped. I wanted to set this String as a text to the TextView above: "Groupa" but this is the result: TextView的自动调整功能不起作用(Android O)

不幸的是,文本视图被裁剪。我想将此String作为文本设置为上面的TextView:“Groupa”,但这是结果:

(TextView is inside ConstraintLayout with yellow circle background.)

(TextView位于ConstraintLayout内部,带有黄色圆圈背景。)

As you can see the textview is not resized at all. Do you have any idea what to do?

如您所见,textview根本没有调整大小。你知道该怎么办吗?

Thanks.

8 个解决方案

#1


27  

I have tested this for a few situations, and have the below conclusion:

我已经针对一些情况对此进行了测试,得出以下结论:

You must have bounded width and height. For example, if you set width to be match_parent but wrap_content for height, I think Android doesn't know that how high you want to stretch your text. In your example you don't have a specific height, so I think that's why it doesn't work.

你必须有有限的宽度和高度。例如,如果您将width设置为match_parent但将wrap_content设置为height,我认为Android不知道您想要拉伸文本的高度。在你的例子中,你没有特定的高度,所以我认为这就是为什么它不起作用。

For example:

TextView的自动调整功能不起作用(Android O)

TextView的自动调整功能不起作用(Android O)

I don't know why Android official document would use wrap_content as an example...

我不知道为什么Android官方文档会使用wrap_content作为例子......

And as you can see I didn't use other attributes in my example, so it probably is not the problem of incorrect attributes.

正如您所看到的,我在示例中没有使用其他属性,因此可能不是属性不正确的问题。

And, yes, the TextView I am using is android.support.v7.widget.AppCompatTextView.

而且,是的,我使用的TextView是android.support.v7.widget.AppCompatTextView。

And as long as you are using support library 26.0.0 or above it is good enough.

只要您使用支持库26.0.0或更高版本就足够了。

EDIT:

As for ConstraintLayout, the principal is the same. You should have both bounded width and height, which means either one of below for each dimension:

对于ConstraintLayout,主体是相同的。您应该同时具有有界宽度和高度,这意味着每个维度的下方之一:

  1. You have specified an absolute value for that dimension (width or height)

    您已为该尺寸指定了绝对值(宽度或高度)

  2. You have set Constraint to both directions

    您已将Constraint设置为两个方向

For example:

TextView的自动调整功能不起作用(Android O)TextView的自动调整功能不起作用(Android O)TextView的自动调整功能不起作用(Android O)

UPDATE: (2017-09-21)

I have tested that unfortunately it seems it does not support custom typeface yet, which is a function published together in support library v26...

我测试过,遗憾的是它似乎还不支持自定义字体,这是一个在支持库v26中一起发布的函数...

#2


9  

Additional to the other correct answers I found another point which prevents autosizing to work.

除了其他正确的答案,我发现另一点阻止自动调整工作。

Do not use android:singleLine="true" together with autosizing. Use the newer android:maxLines="1" instead.

不要将android:singleLine =“true”与自动调整一起使用。使用较新的android:maxLines =“1”代替。

#3


6  

I had the same issue. I solved it by changing two lines in my gradle: compile 'com.android.support:support-v4:26.0.1' and compile 'com.android.support:appcompat-v7:26.0.1' To fit longer texts you have to add all four options, like this:

我遇到过同样的问题。我通过改变我的gradle中的两行来解决它:编译'com.android.support:support-v4:26.0.1'并编译'com.android.support:appcompat-v7:26.0.1'为了适应更长的文本你有添加所有四个选项,如下所示:

<android.support.v7.widget.AppCompatTextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/your_string"
        app:autoSizeTextType="uniform"
        app:autoSizeMaxTextSize="13sp"
        app:autoSi*TextSize="5sp"
        app:autoSizeStepGranularity="1sp"/>

#4


5  

Which value did you set to android:layout_height attribute ?

您设置为android:layout_height属性的值是多少?

From the document: "If you set autosizing in an XML file, it is not recommended to use the value "wrap_content" for the layout_width or layout_height attributes of a TextView. It may produce unexpected results."

从文档中:“如果在XML文件中设置自动调整大小,则不建议对TextView的layout_width或layout_height属性使用值”wrap_content“。它可能会产生意外结果。”

#5


3  

Config your TextView like this

像这样配置TextView

<TextView
    android:id="@+id/vName"
    android:layout_width="56dp"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:text="Groupa"
    app:autoSi*TextSize="12sp"
    app:autoSizeMaxTextSize="20sp"
    app:autoSizeTextType="uniform"
    />

Work well on Android 22, 23, 26
TextView的自动调整功能不起作用(Android O)

在Android 22,23,26上运行良好

#6


2  

Have you tried setting all of the four attributes described in the link you posted?

您是否尝试过设置所发布链接中描述的所有四个属性?

E.g.

app:autoSizeTextType="uniform"
app:autoSizeMaxTextSize="13sp"
app:autoSi*TextSize="5sp"
app:autoSizeStepGranularity="1sp"

You can also try setting both width and height to wrap_content, and setting minWidth and maxWidth to 56dp.

您还可以尝试将width和height设置为wrap_content,并将minWidth和maxWidth设置为56dp。

As poss also mentioned in the comments, maxlines seems to cause problems (for me as well), so try removing that (The autosizing should probably take care of this, by reducing the textsize).

正如在评论中也提到的那样,maxlines似乎也会引起问题(对我来说也是如此),所以尝试删除它(自动调整可能应该通过减少文本大小来处理这个问题)。

#7


2  

In my case something very stupid was the issue: While autosizing always worked fine for me, on exactly one TextView I used the android: namespace instead of app:! I was totally oblivious to my mistake and kept wondering why it didn't work. So when using an AppCompat theme always make sure to use the AppCompat attributes, not the native ones.

在我的情况下,一个非常愚蠢的问题是:虽然autosizing对我来说总是很好,但在一个TextView上我使用了android:namespace而不是app :!我完全没有注意到我的错误,并一直在想为什么它不起作用。因此,在使用AppCompat主题时,请始终确保使用AppCompat属性,而不是本机属性。

#8


1  

if you do not know size of textview. e.g. you put them in linearlayout and set height or width to 0dp. then I got a solution. you need to setAutoSizeTextTypeWithDefaults in OnSizeChanged event.

如果你不知道textview的大小。例如你把它们放在linearlayout中并将高度或宽度设置为0dp。然后我找到了解决方案。你需要在OnSizeChanged事件中设置setAutoSizeTextTypeWithDefaults。

@Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        this.setAutoSizeTextTypeWithDefaults(AUTO_SIZE_TEXT_TYPE_UNIFORM);

    }

#1


27  

I have tested this for a few situations, and have the below conclusion:

我已经针对一些情况对此进行了测试,得出以下结论:

You must have bounded width and height. For example, if you set width to be match_parent but wrap_content for height, I think Android doesn't know that how high you want to stretch your text. In your example you don't have a specific height, so I think that's why it doesn't work.

你必须有有限的宽度和高度。例如,如果您将width设置为match_parent但将wrap_content设置为height,我认为Android不知道您想要拉伸文本的高度。在你的例子中,你没有特定的高度,所以我认为这就是为什么它不起作用。

For example:

TextView的自动调整功能不起作用(Android O)

TextView的自动调整功能不起作用(Android O)

I don't know why Android official document would use wrap_content as an example...

我不知道为什么Android官方文档会使用wrap_content作为例子......

And as you can see I didn't use other attributes in my example, so it probably is not the problem of incorrect attributes.

正如您所看到的,我在示例中没有使用其他属性,因此可能不是属性不正确的问题。

And, yes, the TextView I am using is android.support.v7.widget.AppCompatTextView.

而且,是的,我使用的TextView是android.support.v7.widget.AppCompatTextView。

And as long as you are using support library 26.0.0 or above it is good enough.

只要您使用支持库26.0.0或更高版本就足够了。

EDIT:

As for ConstraintLayout, the principal is the same. You should have both bounded width and height, which means either one of below for each dimension:

对于ConstraintLayout,主体是相同的。您应该同时具有有界宽度和高度,这意味着每个维度的下方之一:

  1. You have specified an absolute value for that dimension (width or height)

    您已为该尺寸指定了绝对值(宽度或高度)

  2. You have set Constraint to both directions

    您已将Constraint设置为两个方向

For example:

TextView的自动调整功能不起作用(Android O)TextView的自动调整功能不起作用(Android O)TextView的自动调整功能不起作用(Android O)

UPDATE: (2017-09-21)

I have tested that unfortunately it seems it does not support custom typeface yet, which is a function published together in support library v26...

我测试过,遗憾的是它似乎还不支持自定义字体,这是一个在支持库v26中一起发布的函数...

#2


9  

Additional to the other correct answers I found another point which prevents autosizing to work.

除了其他正确的答案,我发现另一点阻止自动调整工作。

Do not use android:singleLine="true" together with autosizing. Use the newer android:maxLines="1" instead.

不要将android:singleLine =“true”与自动调整一起使用。使用较新的android:maxLines =“1”代替。

#3


6  

I had the same issue. I solved it by changing two lines in my gradle: compile 'com.android.support:support-v4:26.0.1' and compile 'com.android.support:appcompat-v7:26.0.1' To fit longer texts you have to add all four options, like this:

我遇到过同样的问题。我通过改变我的gradle中的两行来解决它:编译'com.android.support:support-v4:26.0.1'并编译'com.android.support:appcompat-v7:26.0.1'为了适应更长的文本你有添加所有四个选项,如下所示:

<android.support.v7.widget.AppCompatTextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/your_string"
        app:autoSizeTextType="uniform"
        app:autoSizeMaxTextSize="13sp"
        app:autoSi*TextSize="5sp"
        app:autoSizeStepGranularity="1sp"/>

#4


5  

Which value did you set to android:layout_height attribute ?

您设置为android:layout_height属性的值是多少?

From the document: "If you set autosizing in an XML file, it is not recommended to use the value "wrap_content" for the layout_width or layout_height attributes of a TextView. It may produce unexpected results."

从文档中:“如果在XML文件中设置自动调整大小,则不建议对TextView的layout_width或layout_height属性使用值”wrap_content“。它可能会产生意外结果。”

#5


3  

Config your TextView like this

像这样配置TextView

<TextView
    android:id="@+id/vName"
    android:layout_width="56dp"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:text="Groupa"
    app:autoSi*TextSize="12sp"
    app:autoSizeMaxTextSize="20sp"
    app:autoSizeTextType="uniform"
    />

Work well on Android 22, 23, 26
TextView的自动调整功能不起作用(Android O)

在Android 22,23,26上运行良好

#6


2  

Have you tried setting all of the four attributes described in the link you posted?

您是否尝试过设置所发布链接中描述的所有四个属性?

E.g.

app:autoSizeTextType="uniform"
app:autoSizeMaxTextSize="13sp"
app:autoSi*TextSize="5sp"
app:autoSizeStepGranularity="1sp"

You can also try setting both width and height to wrap_content, and setting minWidth and maxWidth to 56dp.

您还可以尝试将width和height设置为wrap_content,并将minWidth和maxWidth设置为56dp。

As poss also mentioned in the comments, maxlines seems to cause problems (for me as well), so try removing that (The autosizing should probably take care of this, by reducing the textsize).

正如在评论中也提到的那样,maxlines似乎也会引起问题(对我来说也是如此),所以尝试删除它(自动调整可能应该通过减少文本大小来处理这个问题)。

#7


2  

In my case something very stupid was the issue: While autosizing always worked fine for me, on exactly one TextView I used the android: namespace instead of app:! I was totally oblivious to my mistake and kept wondering why it didn't work. So when using an AppCompat theme always make sure to use the AppCompat attributes, not the native ones.

在我的情况下,一个非常愚蠢的问题是:虽然autosizing对我来说总是很好,但在一个TextView上我使用了android:namespace而不是app :!我完全没有注意到我的错误,并一直在想为什么它不起作用。因此,在使用AppCompat主题时,请始终确保使用AppCompat属性,而不是本机属性。

#8


1  

if you do not know size of textview. e.g. you put them in linearlayout and set height or width to 0dp. then I got a solution. you need to setAutoSizeTextTypeWithDefaults in OnSizeChanged event.

如果你不知道textview的大小。例如你把它们放在linearlayout中并将高度或宽度设置为0dp。然后我找到了解决方案。你需要在OnSizeChanged事件中设置setAutoSizeTextTypeWithDefaults。

@Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        this.setAutoSizeTextTypeWithDefaults(AUTO_SIZE_TEXT_TYPE_UNIFORM);

    }