如何使 TextView
文字两端对齐(左右对齐)?
我在这里找到了一种可能的解决方案(链接),但它并不起作用(即使你将vertical-center更改为center_vertical等)。
如何使 TextView
文字两端对齐(左右对齐)?
我在这里找到了一种可能的解决方案(链接),但它并不起作用(即使你将vertical-center更改为center_vertical等)。
我不相信Android支持全文对齐。
2018-01-01更新:Android 8.0+支持TextView
的对齐模式。(参考链接)
Android O
中的 TextView
提供了全称 (新的排版对齐) 功能。
您只需要完成以下操作:
Kotlin
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" />
默认值为JUSTIFICATION_MODE_NONE
(在xml中为none
)。
<TextView
...
android:layout_gravity="center_vertical|end">
...
</TextView>
<TextView
...
android:gravity="end">
...
</TextView>
android:layout_gravity="center_horizontal|center"
和 android:gravity="center"
。 - Luis A. Florit为了在Android上调整文本的对齐方式,我使用了WebView。
setContentView(R.layout.main);
WebView view = new WebView(this);
view.setVerticalScrollBarEnabled(false);
((LinearLayout)findViewById(R.id.inset_web_view)).addView(view);
view.loadData(getString(R.string.hello), "text/html; charset=utf-8", "utf-8");
以及 HTML。
<string name="hello">
<![CDATA[
<html>
<head></head>
<body style="text-align:justify;color:gray;background-color:black;">
Lorem ipsum dolor sit amet, consectetur
adipiscing elit. Nunc pellentesque, urna
nec hendrerit pellentesque, risus massa
</body>
</html>
]]>
</string>
我目前还不能上传图片来证明,但是“对我来说它有效”。
view.loadData()
后跟着view.setBackgroundColor("#00000000")
来使背景透明。 - Paul Lammertsmaview.loadUrl()
加载它可以工作,而view.loadData()
则不行。我不知道为什么后者不行。 - Paul Lammertsmawebview.loadDataWithBaseURL(null, displayString, "text/html", "utf-8", "");
- Kirit Vaghela我们为此创建了一个简单的类。目前有两种方法可以实现您想要的效果。两种方法都不需要WEBVIEW,并且支持可扩展字符序列(SUPPORTS SPANNABLES)。
库:https://github.com/bluejamesbond/TextJustify-Android
支持:Android 2.0到5.X
设置
// Please visit Github for latest setup instructions.
截图
我基于本地文本视图编写了一个小部件来完成它。
我找到了解决这个问题的方法,但这可能不是非常优雅,但效果还不错。
它的原理是将每行的空格替换为固定宽度的ImageSpan(颜色是透明的)。
public static void justify(final TextView textView) {
final AtomicBoolean isJustify = new AtomicBoolean(false);
final String textString = textView.getText().toString();
final TextPaint textPaint = textView.getPaint();
final SpannableStringBuilder builder = new SpannableStringBuilder();
textView.post(new Runnable() {
@Override
public void run() {
if (!isJustify.get()) {
final int lineCount = textView.getLineCount();
final int textViewWidth = textView.getWidth();
for (int i = 0; i < lineCount; i++) {
int lineStart = textView.getLayout().getLineStart(i);
int lineEnd = textView.getLayout().getLineEnd(i);
String lineString = textString.substring(lineStart, lineEnd);
if (i == lineCount - 1) {
builder.append(new SpannableString(lineString));
break;
}
String trimSpaceText = lineString.trim();
String removeSpaceText = lineString.replaceAll(" ", "");
float removeSpaceWidth = textPaint.measureText(removeSpaceText);
float spaceCount = trimSpaceText.length() - removeSpaceText.length();
float eachSpaceWidth = (textViewWidth - removeSpaceWidth) / spaceCount;
SpannableString spannableString = new SpannableString(lineString);
for (int j = 0; j < trimSpaceText.length(); j++) {
char c = trimSpaceText.charAt(j);
if (c == ' ') {
Drawable drawable = new ColorDrawable(0x00ffffff);
drawable.setBounds(0, 0, (int) eachSpaceWidth, 0);
ImageSpan span = new ImageSpan(drawable);
spannableString.setSpan(span, j, j + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
builder.append(spannableString);
}
textView.setText(builder);
isJustify.set(true);
}
}
});
}
我把代码放到了GitHub上:
https://github.com/twiceyuan/TextJustification概述:
XML布局:声明WebView代替TextView
<WebView
android:id="@+id/textContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
WebView view = (WebView) findViewById(R.id.textContent);
String text;
text = "<html><body><p align=\"justify\">";
text+= "This is the text will be justified when displayed!!!";
text+= "</p></body></html>";
view.loadData(text, "text/html", "utf-8");
这可能解决你的问题。 对我来说完全有效。
非常简单 我们可以在xml文件中做到这一点
<TextView
android:justificationMode="inter_word"
/>