你如何得到一个TextView的文本是正当的(与文本flush在左边和右手边)?
我在这里找到了一个可能的解决方案,但它不起作用(即使您将vertical-center更改为center_vertical等)。
你如何得到一个TextView的文本是正当的(与文本flush在左边和右手边)?
我在这里找到了一个可能的解决方案,但它不起作用(即使您将vertical-center更改为center_vertical等)。
当前回答
TextView在Android O提供了充分的理由(新的排版对齐)本身。 你只需要这样做:
科特林
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
textView.justificationMode = JUSTIFICATION_MODE_INTER_WORD
}
Java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
textView.setJustificationMode(JUSTIFICATION_MODE_INTER_WORD);
}
XML
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:justificationMode="inter_word" />
默认值是jusication_mode_none (xml中的none)。
其他回答
非常简单的 我们可以在xml文件中这样做
<TextView
android:justificationMode="inter_word"
/>
TextView在Android O提供了充分的理由(新的排版对齐)本身。 你只需要这样做:
科特林
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
textView.justificationMode = JUSTIFICATION_MODE_INTER_WORD
}
Java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
textView.setJustificationMode(JUSTIFICATION_MODE_INTER_WORD);
}
XML
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:justificationMode="inter_word" />
默认值是jusication_mode_none (xml中的none)。
我不认为Android支持完全的理由。
更新2018-01-01:Android 8.0+支持TextView对齐模式。
你可以在xml中使用rationationmode作为inter_word。您必须记住,此属性可用于api级别26及更高级别。为此,你可以将targetApi赋值为o。完整的代码如下所示
<com.google.android.material.textview.MaterialTextView
...
android:justificationMode="inter_word"
tools:targetApi="o" />
我认为有两种选择:
使用像Pango这样的东西,通过NDK专门处理这一点,并将文本渲染到OpenGL或其他表面。 使用Paint.measureText()和friends来获取单词的长度,并在自定义视图的Canvas上手动布局它们。