将android-textview的某个部分对齐到右边

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.

  • 对我来说,我只能使用换行符"\n"而不是两个空格,并将setSpan的第二个参数设置为LeftText.length()而不使用"+2".我试过很多组合试图摆脱我不想要的换行,但还没有找到一个令人满意的解决方案. (5认同)
  • 这只适用于我,如果我使用整个线与跨度,但不是一个部分.:( (4认同)
  • 嗯.它没有与右边对齐.textview中的引力是否重要? (2认同)
  • @WonderCsabo```AlignmentSpan.Standard```是一个段落样式,所以你需要在```LeftText```(即\n)中添加一个新的行字符,以使```RightText````` (我没有测试过上面的特定例子) (2认同)
  • 为什么它作为答案呈现,而它不起作用且无法起作用?它是一个基于段落的跨度,所以如果我们将其设置为两个字符串(带有 \n),那么它会起作用,但如果没有跨度,它可以轻松解决,而对于单个字符串,它是无用的:( (2认同)

Fra*_*ank 7

这是一个与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)


Eug*_*nec 5

感谢您的提示,@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 不会让我想刺伤我的眼睛。