TextView 的自动调整大小不起作用 (Android O) [英] Autosizing of TextView doesn't work (Android O)

查看:105
本文介绍了TextView 的自动调整大小不起作用 (Android O)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了支持库 26 中添加的新自动调整功能.我阅读了一份文档,可以在这里找到:

(TextView 位于 ConstraintLayout 内,背景为黄色圆圈.)

如您所见,文本视图根本没有调整大小.你知道该怎么做吗?

谢谢.

解决方案

我测试了几种情况,得出以下结论:

您必须具有有限的宽度高度.例如,如果您将宽度设置为 match_parent 而将 wrap_content 设置为高度,我认为 Android 不知道您想要将文本拉伸多高.在您的示例中,您没有特定的高度,所以我认为这就是它不起作用的原因.

例如:

不知道为什么Android官方文档会以wrap_content为例...

正如你所看到的,我在我的例子中没有使用其他属性,所以这可能不是属性不正确的问题.

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

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

至于ConstraintLayout,原理是一样的.您应该同时具有有界宽度和高度,这意味着每个维度的以下任一项:

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

  2. 您已将约束设置为双向

例如:

更新:(2017-09-21)

我已经测试过,不幸的是它似乎还不支持自定义字体,这是一个在支持库 v26 中一起发布的功能...

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

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:autoSizeMinTextSize="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.

This is my 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:autoSizeMinTextSize="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 is inside ConstraintLayout with yellow circle background.)

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

Thanks.

解决方案

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.

For example:

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

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.

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

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:

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

  2. You have set Constraint to both directions

For example:

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...

这篇关于TextView 的自动调整大小不起作用 (Android O)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆