Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
581 views
in Technique[技术] by (71.8m points)

android - margin set to first line of textView

I want to set start margin only for the first line of TextView (Not first line of each paragraph)? I have used the following code:

SpannableString s = new SpannableString("this is a test"+"
"+"this is a test");
s.setSpan(new android.text.style.LeadingMarginSpan.Standard(30, 0), 0, s.length(), 0);
textView .setText(s);

Here start margin is getting set for all lines of the Paragraph.. I don't want that. I want start margin to be applied for first line only.. Pls help..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You actually already answered your own question.
All you need to do is to set span lenght to 1 instead of s.length().

if (s.lenght() > 0) {
    s.setSpan(new android.text.style.LeadingMarginSpan.Standard(30, 0), 0, 1, 0);
}

That way margin will only be applied to the first paragraph.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...