简体   繁体   English

平衡的多行文本视图

[英]Balanced Multiline TextView

I am starting with this:我从这个开始:

<TextView
    android:text="Here is what it looks like"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:gravity="center"/>

Which renders like this:呈现如下:

| Here is what it looks |
|         like          |

But I want it to render like this:但我希望它呈现如下:

|      Here is what     |
|     it looks like     |

Is there a way to do this without customizing the built in TextView ?有没有办法在不自定义内置TextView情况下做到这一点? If not, how can TextView be customized to do this?如果没有,如何自定义TextView以执行此操作?

Note that the android:text value is actually filled in programmatically and can vary in content/length.请注意, android:text值实际上是以编程方式填充的,并且内容/长度可能会有所不同。 I am looking for a general solution that works for any text value on any screen width.我正在寻找适用于任何屏幕宽度上的任何文本值的通用解决方案。

Starting in API 23, TextView has break strategy and hyphenation frequency settings that can be used to achieve this.从 API 23 开始, TextView具有可用于实现此目的的中断策略字频率设置。

<TextView
    android:text="Here is what it looks like"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:hyphenationFrequency="none"
    android:breakStrategy="balanced"
    android:gravity="center"/>

However, I don't see any way to do this for API <23.但是,对于 API <23,我没有看到任何方法可以做到这一点。 AppCompatTextView in version 23.1.0 of the support library doesn't seem to support these.支持库 23.1.0 版中的AppCompatTextView似乎不支持这些。

NOTE: Android Studio 1.4.1 does not show the desired wrapping behavior in the preview window (even when API 23 is selected for the preview), but it can be seen in an emulated API 23 device.注意:Android Studio 1.4.1 不会在预览窗口中显示所需的包装行为(即使为预览选择了 API 23),但可以在模拟的 API 23 设备中看到。

或者你可以只 layout_alignparent_left 与重心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM