简体   繁体   中英

Set alignment to a single line in multiline edittext android

Is it possible to set alignment on a single line or some lines in multiline edittext android? If yes, then how, if no, then why not? I get this answer from another question posted under the similar topic

"You can't apply multiple alignments within the same editText control. You'd have to solve your problem with some kind of manual padding of the text content"

So how can manual padding be inserted for selected lines?

you can use Alignment span to set alignment for a single line and multiple lines in multiline EditText provided you know the start and end index for the line. For example:

SpannableStringBuilder s = new SpannableStringBuilder("some text ");
AlignmentSpan span = new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE);
s.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
t.setText(s);

You can apply alignment to multiple lines at once by covering multiple line between start and end index.

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