本文介绍了使android-textview的特定部分向右对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个TextView
.它的某些部分应该与左侧对齐,而某些部分应与右侧对齐.我将如何用Java做到这一点?
I have this TextView
. Some parts of it is supposed to be aligned to the left and some parts to the right. How would I do this in Java?
基本上,我希望第一行向左对齐,第二行向右对齐,依此类推.
Basicly I want the first line to align to the left, and the next line to the right and so on.
有人知道吗?
编辑
我尝试使用HTML,然后在不起作用的情况下尝试了span.
I have tried to use HTML and then when that did not work I tried spans.
html尝试
textView.setText(Html.fromHtml("<p align=\"right\">THIS IS TO THE RIGHT</p>"));
这是跨度尝试
String LeftText = "LEFT";
String RightText = "RIGHT";
final String resultText = LeftText + " " + RightText;
final SpannableString styledResultText = new SpannableString(resultText);
styledResultText.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE), LeftText.length() + 1, LeftText.length() + 2 +RightText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(styledResultText);
但是它们似乎都不起作用.
But none of them seems to work.
推荐答案
TextView resultTextView = new TextView(this);
final String resultText = LeftText + " " + RightText;
final SpannableString styledResultText = new SpannableString(resultText);
styledResultText.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE)
, LeftText.length() + 2
, LeftText.length() + 2 + RightText.length()
, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
resultTextView.setText(styledResultText);
Alignment.ALIGN_OPPOSITE is the equivalent for right side.
Alignment.ALIGN_NORMAL is the equivalent for left side.
这篇关于使android-textview的特定部分向右对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!