你如何得到一个TextView的文本是正当的(与文本flush在左边和右手边)?

我在这里找到了一个可能的解决方案,但它不起作用(即使您将vertical-center更改为center_vertical等)。


当前回答

FILL_HORIZONTAL等价于CENTER_HORIZONTAL。 你可以在textview的源代码中看到这个代码片段:

case Gravity.CENTER_HORIZONTAL:
case Gravity.FILL_HORIZONTAL:
    return (mLayout.getLineWidth(0) - ((mRight - mLeft) -
            getCompoundPaddingLeft() - getCompoundPaddingRight())) /
            getHorizontalFadingEdgeLength();

其他回答

我写了一个基于本地textview的小部件来做到这一点。

github

我认为有两种选择:

使用像Pango这样的东西,通过NDK专门处理这一点,并将文本渲染到OpenGL或其他表面。 使用Paint.measureText()和friends来获取单词的长度,并在自定义视图的Canvas上手动布局它们。

在xml文件中使用此属性即可

android:justificationMode="inter_word"

你可以在xml中使用rationationmode作为inter_word。您必须记住,此属性可用于api级别26及更高级别。为此,你可以将targetApi赋值为o。完整的代码如下所示

<com.google.android.material.textview.MaterialTextView
            ...
            android:justificationMode="inter_word"
            tools:targetApi="o" />

我不认为Android支持完全的理由。

更新2018-01-01:Android 8.0+支持TextView对齐模式。