use*_*905 15 java android android-widget
我有这个TextView.它的某些部分应该与左侧对齐,而某些部分则与右侧对齐.我怎么用Java做这个?
基本上我希望第一行与左对齐,下一行与右对齐,依此类推.
有人知道吗?
编辑
我试图使用HTML,然后当它不起作用我尝试跨度.
HTML尝试
textView.setText(Html.fromHtml("<p align=\"right\">THIS IS TO THE RIGHT</p>"));
Run Code Online (Sandbox Code Playgroud)
继续尝试跨度
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);
Run Code Online (Sandbox Code Playgroud)
但它们似乎都不起作用.
Vik*_*tel 23
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);
Run Code Online (Sandbox Code Playgroud)
Alignment.ALIGN_OPPOSITE is the equivalent for right side.
Alignment.ALIGN_NORMAL is the equivalent for left side.
这是一个与Spannable一起使用的解决方案,如果右侧和左侧太宽,它们将在同一条线上重叠.由于使用spannable执行左/右技巧需要在Left和Right之间进行换行,我的修复是添加一个spannable,将一行换行的行高减少到零(即重叠行),然后恢复正常行高那.
String fullText = leftText + "\n " + rightText; // only works if linefeed between them! "\n ";
int fullTextLength = fullText.length();
int leftEnd = leftText.length();
int rightTextLength = rightText.length();
final SpannableString s = new SpannableString(fullText);
AlignmentSpan alignmentSpan = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE);
s.setSpan(alignmentSpan, leftEnd, fullTextLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
s.setSpan(new SetLineOverlap(true), 1, fullTextLength-2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
s.setSpan(new SetLineOverlap(false), fullTextLength-1, fullTextLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Run Code Online (Sandbox Code Playgroud)
我们有一个处理重叠线的小程序:
private static class SetLineOverlap implements LineHeightSpan {
private int originalBottom = 15; // init value ignored
private int originalDescent = 13; // init value ignored
private Boolean overlap; // saved state
private Boolean overlapSaved = false; // ensure saved values only happen once
SetLineOverlap(Boolean overlap) {
this.overlap = overlap;
}
@Override
public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v,
Paint.FontMetricsInt fm) {
if (overlap) {
if (!overlapSaved) {
originalBottom = fm.bottom;
originalDescent = fm.descent;
overlapSaved = true;
}
fm.bottom += fm.top;
fm.descent += fm.top;
} else {
// restore saved values
fm.bottom = originalBottom;
fm.descent = originalDescent;
overlapSaved = false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
感谢您的提示,@Frank,但这已经足够了:
public class LineOverlapSpan implements LineHeightSpan {
@Override
public void chooseHeight(final CharSequence text, final int start, final int end, final int spanstartv, final int v, final Paint.FontMetricsInt fm) {
fm.bottom += fm.top;
fm.descent += fm.top;
}
}
Run Code Online (Sandbox Code Playgroud)
像这样使用:
CharSequence text = return new Truss()
.append("LEFT")
.pushSpan(LineOverlapSpan())
.append("\n")
.popSpan()
.pushSpan(AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE))
.append("RIGHT")
.build()
Run Code Online (Sandbox Code Playgroud)
这里Truss是
一个
SpannableStringBuilder包装器,它的 API 不会让我想刺伤我的眼睛。
| 归档时间: |
|
| 查看次数: |
13871 次 |
| 最近记录: |