19

我有这个TextView。它的某些部分应该与左侧对齐,而某些部分应与右侧对齐。我将如何在 Java 中做到这一点?

基本上我希望第一行向左对齐,下一行向右对齐,依此类推。

有人有线索吗?

编辑

我曾尝试使用 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);

但它们似乎都不起作用。

4

6 回答 6

24
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.

于 2013-01-25T11:35:38.417 回答
8

这是一个适用于 Spannable 的解决方案,如果左右太宽,它们将在同一条线上重叠。由于使用 spannable 执行 Left/Right 技巧需要在 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);

我们有处理重叠线的小程序:

    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;
        }
    }
}
于 2016-04-07T22:42:02.527 回答
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;
    }
}

像这样使用:

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 不会让我想要刺痛我的眼睛。

于 2016-09-11T03:12:06.517 回答
2

两种解决方案:

1)每行有不同的文本视图并设置其重力。

2)在加载数据时使用 Html 为其设置 html 中的对齐方式 REF:how-to-display-html-in-textview 这将不起作用 align is not a supported tags 已 编辑:

3:将使用跨度。

于 2013-01-25T11:28:05.487 回答
0

简单。在 textview 中调整 xml 部分。使用布局。如果您希望文本视图位于左侧

android:alignParentLeft="true" 更多对齐细节看这里。

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/

于 2013-01-25T11:34:50.860 回答
0

启发了其他解决方案,但解决了多行文本问题和文本重叠问题

class LineOverlapSpan() : LineHeightSpan {
    var originalBottom: Int = 0
    var originalDescent: Int = 0
    var overlapSaved = false

    override fun chooseHeight(
        text: CharSequence?,
        start: Int,
        end: Int,
        spanstartv: Int,
        v: Int,
        fm: Paint.FontMetricsInt?
    ) {
        if (fm == null) {
            return
        }

        if (!overlapSaved) {
            originalBottom = fm.bottom
            originalDescent = fm.descent
            overlapSaved = true
        }

        if (text?.subSequence(start, end)?.endsWith("\n") == true) {
            fm.bottom += fm.top
            fm.descent += fm.top
        } else {
            fm.bottom = originalBottom
            fm.descent = originalDescent
        }
    }

}

用法:

fun keyValueSpan(key: CharSequence, value: CharSequence) = span {
    span {
        alignment = "normal"
        +key
        span {
            textColor = Color.TRANSPARENT
            +value
        }
        span {
            +"\n"
            addSpan(LineOverlapSpan())
        }
    }
    span {
        alignment = "opposite"
        +value
    }
}

Kotlin 解决方案是使用 Kpan 库https://github.com/2dxgujun/Kpan

于 2019-07-15T06:41:00.867 回答