简体   繁体   中英

How to align text on both left and right side of one TextView line?

I want to set text in a single line TextView on both left and right side of the only line. For this I use a SpannableStringBuilder.

Here is an example :

mSpannableStringBuilder.append("leftright");
mSpannableStringBuilder.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_NORMAL), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mSpannableStringBuilder.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE), 4, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mTextView.setText(mSpannableStringBuilder, BufferType.SPANNABLE);

But it doesn't work ... Any suggestions please ? Thanks you

我只是在xml文件中使用如下所示

<RelativeLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:gravity="center_horizontal" > <TextView android:text="AAA:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#3b5998" android:layout_marginRight="5px" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> <TextView android:id="@+id/maine" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Not Set" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#0040FF" android:layout_alignParentRight="true" android:layout_alignParentTop="true"
/> </RelativeLayout>

Check if your xml tag contains "android:textAlignment" or "android:gravity" properties. In positive case, remove it.

It can be done with the span, but requires a linefeed between the left and right string which sort of sucks. If there is no linefeed, it can only align the line left or right, but not split as left and right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM