Android TextView将文本左对齐和右对齐

时间:2018-02-08 06:37:46

标签: java android

我正在尝试TextView有两个文本,一个在TextView的左侧对齐,另一个在TextView的右侧。

我引用了这个[Android TextView Align text to Right and Left(@ daemontus)来设置文本。

但是,如果我在TextView的右侧TextView左侧输入public void setLeftRightText(TextView view, String left, String right,Enum keysel) { if(keysel == RSK_KEY) { SpannableString merged=new SpannableString(left + "\n" + right); merged.setSpan( new AlignmentSpan.Standard(Layout.Alignment.ALIGN_NORMAL), 0, left.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE ); merged.setSpan( new LineOverlapSpan(), left.length(), left.length() + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE ); merged.setSpan( new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE), left.length() + 1, left.length() + 1 + right.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE ); view.setText(merged); } else if (keysel == LSK_KEY){ final String resultText = right + " " + left; final SpannableString styledResultText = new SpannableString(resultText); styledResultText.setSpan((new AlignmentSpan.Standard(Layout.Alignment.ALIGN_OPPOSITE )), left.length() + 2, left.length() + 2 +right.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); styledResultText.setSpan((new AlignmentSpan.Standard(Layout.Alignment.ALIGN_NORMAL )), 0, left.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); view.setText(styledResultText); } }

左侧的文本,如何添加文字?

1)输入:输入按钮1以显示Text1

enter image description here

2)输出:输入按钮2以显示Text2和Text1

enter image description here

public class LineOverlapSpan implements LineHeightSpan {

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

LineOverlapSpan.java

FIELD_SIZE = 20
roster =
  students.map do |s|
    s.values.map do |f|
      f.to_s.ljust(FIELD_SIZE) # 1 row
    end.join('   ')            # join columns with spaces
  end.join($/)                 # join rows with OS-dependent CR/LF
titles =
  ['First Name', 'Last Name', 'Grade Level'].map do |t|
    t.to_s.ljust(FIELD_SIZE)
  end.join(' | ')              # join with bars

3 个答案:

答案 0 :(得分:0)

您需要使用两个单独的textview来满足您的要求。

请使用以下代码并检查。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@android:color/black"
    android:padding="10dp"
    android:weightSum="1">

    <TextView
        android:text="Left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:textSize="36sp"
        android:fontFamily="sans-serif-light"
        android:textColor="@android:color/white"
        android:background="@android:color/transparent"
        android:gravity="left"
        android:padding="5dp"/>

    <TextView
        android:text="Right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:textSize="36sp"
        android:fontFamily="sans-serif-light"
        android:textColor="@android:color/white"
        android:background="@android:color/transparent"
        android:gravity="right"
        android:padding="5dp"/>

</LinearLayout>

答案 1 :(得分:0)

您可以在单个文本视图中获得所需的结果。创建一个扩展文本视图并覆盖onDraw

的视图

我把一个小例子放在一起。希望它至少会给你一个想法

public class LeftRightTextView extends AppCompatTextView {

    private TextPaint mTextPaint;
    private String mLeftText, mRightText;

    public LeftRightTextView(Context context) {
        this(context, null);
    }

    public LeftRightTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public void setLeftText(String leftText) {
        mLeftText = leftText;
        setText(mLeftText);
    }

    public void setRightText(String rightText) {
        mRightText = rightText;
        invalidate();
    }

    public LeftRightTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LeftRightTextView, defStyleAttr, 0);
        mLeftText = typedArray.getString(R.styleable.LeftRightTextView_lr_left_text);
        mRightText = typedArray.getString(R.styleable.LeftRightTextView_lr_right_text);
        typedArray.recycle();
        setText(mLeftText);
        mTextPaint = new TextPaint();
        mTextPaint.setColor(getCurrentTextColor());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //let it draw the left text as usual
        super.onDraw(canvas);

        //Now draw the right text
        int rightEnd = getWidth() - getPaddingRight();
        float textWidth = mTextPaint.measureText(mRightText);
        canvas.drawText(mRightText, rightEnd - textWidth, mTextPaint.getFontMetrics().descent - mTextPaint.getFontMetrics().ascent, mTextPaint);

    }
}

属性

<declare-styleable name="LeftRightTextView">
    <attr name="lr_left_text" format="string"/>
    <attr name="lr_right_text" format="string"/>
</declare-styleable>

布局

<me.srs.myapplication.LeftRightTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:lr_left_text="Hello"
    app:lr_right_text="World"/>

这是结果

The result in layout preview

答案 2 :(得分:-1)

您需要创建自定义视图并覆盖onDraw方法,以实现所需的功能。