android 代码缩进,Android缩进和悬挂缩进

弄清楚如何使悬挂缩进适用于我自己的项目.基本上你需要使用android.text.style.LeadingMarginSpan,并通过代码将它应用到你的文本. LeadingMarginSpan.Standard采用完整缩进(1个参数)或悬挂缩进(2个参数)构造函数,并且需要为要应用样式的每个子字符串创建新的Span对象. TextView本身也需要将其BufferType设置为SPANNABLE.

如果必须多次执行此操作,或者希望在样式中包含缩进,请尝试创建TextView的子类,该子类采用自定义缩进属性并自动应用跨度.我从Statically Typed博客和SO问题Declaring a custom android UI element using XML中获得了很多用途.

在TextView中:

// android.text.style.CharacterStyle is a basic interface, you can try the

// TextAppearanceSpan class to pull from an existing style/theme in XML

CharacterStyle style_char =

new TextAppearanceSpan (getContext(), styleId);

float textSize = style_char.getTextSize();

// indentF roughly corresponds to ems in dp after accounting for

// system/base font scaling, you'll need to tweak it

float indentF = 1.0f;

int indent = (int) indentF;

if (textSize > 0) {

indent = (int) indentF * textSize;

}

// android.text.style.ParagraphStyle is a basic interface, but

// LeadingMarginSpan handles indents/margins

// If you're API8+, there's also LeadingMarginSpan2, which lets you

// specify how many lines to count as "first line hanging"

ParagraphStyle style_para = new LeadingMarginSpan.Standard (indent);

String unstyledSource = this.getText();

// SpannableString has mutable markup, with fixed text

// SpannableStringBuilder has mutable markup and mutable text

SpannableString styledSource = new SpannableString (unstyledSource);

styledSource.setSpan (style_char, 0, styledSource.length(),

Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

styledSource.setSpan (style_para, 0, styledSource.length(),

Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

// *or* you can use Spanned.SPAN_PARAGRAPH for style_para, but check

// the docs for usage

this.setText (styledSource, BufferType.SPANNABLE);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值