Android autosizing TextViews split up words before decreasing font size

ぃ、小莉子 提交于 2019-12-22 04:35:31

问题


Is there a way to avoid it?

If my text is "Blablabla" and it doesn't fit in a square, I don't want to have something like:

Blabla- bla

I want "Blablabla" with a smaller font size. Is there a way to control whether the autosizing TextView will split up words or not?

Here's an example of the issue:

The minimum text size there is 1sp, so it's pretty clear the word could fit with a decreased text size.

It seems this has been reported: https://issuetracker.google.com/issues/38468964


回答1:


I've actually reported about this issue in the past (here and here, one you've already found out about), but it seems there is a solution, or at least it seems to work for me.

I think the tip could help:

  • https://youtu.be/x-FcOX6ErdI?t=218

  • https://developer.android.com/reference/android/widget/TextView#setBreakStrategy(int)

This means you can choose the break strategy for the textView. Example:

        <TextView
            android:layout_width="250dp" android:layout_height="wrap_content" android:background="#f00"
            android:breakStrategy="balanced" android:hyphenationFrequency="none"
            android:text="This is an example text" android:textSize="30dp" app:autoSizeTextType="uniform"/>

Sadly, this doesn't always seem to work nicely as it should. I've noticed that on Hebrew, it sometimes doesn't get spread well:

https://issuetracker.google.com/issues/79936443

But at least it won't put part-words for no reason.

The major disadvantage of using this solution, is that it's available only from API 23 (at least 62% of the devices, according to Android dashboard statistics). Someone has asked it to be on the support library (here), but was told it's impossible.




回答2:


I had the same problem and found a "precarious" work around. For me it works because the text I needed to put in the textView was only one or two words... Anyway, I hope it helps or, at least, it gives you some inspiration. Here's what i did:

in XML I added android:maxLines="2" to the AppCompatTextView.

in code I checked if the text was only ONE word and, if so, set programmatically textView.setMaxLines(1);

I understand it not a perfect nor a universal solution but was good enough for me. Anyway... if you only need to scale "Blablabla" properly using android:maxLines="1" should do the trick.



来源:https://stackoverflow.com/questions/46976525/android-autosizing-textviews-split-up-words-before-decreasing-font-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!