无法自动调整TextView的大小(Android O) [英] Autosizing of TextView doesn't work (Android O)

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

问题描述

我使用了在支持库26中添加的新的自动调整大小功能.我阅读了可以在此处找到的文档: https://developer.android.com/preview/features/autosizing-textview.html

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

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

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

app:autoSizeMaxTextSize="XXsp"

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

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.

这是我的TextView:

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" />

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

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在ConstraintLayout中,带有黄色圆圈背景.)

(TextView is inside ConstraintLayout with yellow circle background.)

如您所见,textview根本没有调整大小.你有什么想法吗?

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

谢谢.

推荐答案

我已经在几种情况下对此进行了测试,并得出以下结论:

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

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

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.

例如:

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

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.

是的,是的,我正在使用的TextView是 android.support.v7.widget.AppCompatTextView .

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

只要您使用的是支持库26.0.0或更高版本,它就足够了.

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

ConstraintLayout一样,主体是相同的. 您应该同时设置宽度和高度的边界,这意味着每个尺寸都必须使用以下任一值:

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. 您已为该尺寸(宽度或高度)指定了一个绝对值

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

您已将约束"设置为两个方向

You have set Constraint to both directions

例如:

更新:(2017年9月21日)

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

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天全站免登陆