margin 设置为 textView 的第一行
我只想为 TextView 的第一行设置起始边距(不是每个段落的第一行)?我使用了以下代码:
SpannableString s = new SpannableString("this is a test"+"\n"+"this is a test");
s.setSpan(new android.text.style.LeadingMarginSpan.Standard(30, 0), 0, s.length(), 0);
textView .setText(s);
这里为该段落的所有行设置了起始边距。我不希望这样。我希望起始边距仅应用于第一行..请帮助..
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"+"\n"+"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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您实际上已经回答了自己的问题。
您所需要做的就是将跨度长度设置为
1
而不是s.length()
。这样,边距将仅应用于第一段。
You actually already answered your own question.
All you need to do is to set span lenght to
1
instead ofs.length()
.That way margin will only be applied to the first paragraph.
正如这里所建议的,我也认为最好的解决方案是在开头简单地添加一些空格。如果您只需要
TextView
第一行的边距空间,我不认为您需要做一些真正高级的事情。如果稍后需要更改或使用 TextView 中的文本,则可以使用
substring()
方法仅获取字符串的一部分,即s.substring( 2、s.length());
这不是一个完美的解决方案,但我认为它可以。
As it has been suggested here, I also think the best solution is to simply add a few spaces at the beginning. If you only need the margin space at the very first line of the
TextView
, I don't see any reason why you'd need to do something really advanced.If you need to change or use the text in the TextView at a later point, you can just use the
substring()
method to get only a part of the string, i.e.s.substring(2, s.length());
It's not a perfect solution, but I think it'll do.