做一个Android的TextView中的某一部分右对齐(Make a certain part o

2019-07-18 17:14发布

我有这样TextView 。 它的某些部分是应该左对齐和某些部分的权利。 我将如何在Java中做到这一点?

Basicly我想的第一行对齐到左边,下一行的权利等。

任何人都得到了线索?

编辑

我曾尝试使用HTML,然后当没有工作,我试图跨越。

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);

但他们都不似乎工作。

Answer 1:

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.



Answer 2:

下面是与Spannable工作,与cavate,如果左,右太宽,他们将在同一行重叠的解决方案。 既然做了左/右招用spannable需要左和右之间的换行,我的解决办法是增加,减少行高度为零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);

而我们的小程序来处理重叠的线路:

    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;
        }
    }
}


Answer 3:

谢谢你的提示,@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;
    }
}

使用这样的:

CharSequence text = return new Truss()
        .append("LEFT")
        .pushSpan(LineOverlapSpan())
        .append("\n")
        .popSpan()
        .pushSpan(AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE))
        .append("RIGHT")
        .build()

其中Truss

一个SpannableStringBuilder包装,其API并不让我想刺了我的眼睛。

  • 杰克沃顿, https://gist.github.com/JakeWharton/11274467


Answer 4:

两个解决方案:

1)具有用于每行不同的文本视图,并设置其严重性。

2)使用HTML,而加载数据设置在HTML对准它REF:如何对显示HTML功能于TextView中 这是行不通的 对齐不支持标签编辑:

3:将使用范围。



Answer 5:

在TextView中Simple.Adjust XML部分。 使用layouts.If你想TextView的是在左

机器人:alignParentLeft =“真”更多对齐详细看这个。

http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

这也是不同的布局的例子。

http://www.androidhive.info/2011/07/android-layouts-linear-layout-relative-layout-and-table-layout/



文章来源: Make a certain part of a android-textview align to the right