Android Indents and Indents

I am interested in having a series of TextViews, ultimately with a padding indent. The standard way to do this with CSS is to set the border to X pixels, and then set the text indent to -X pixels. Obviously, I can make the first option with "android: layout_marginLeft =" Xdp ", but I'm not sure how to overlay -X pixels on the TextView. Any ideas or workarounds? I appreciate any suggestions.

+7
source share
1 answer

It turned out how to make hanging padding work for my own project. Basically you need to use android.text.style.LeadingMarginSpan and apply it to the text through code. LeadingMarginSpan.Standard accepts either the full indent constructor (1 param) or the indent constructor (2 params), and you need to create a new Span object for each substring that you want to apply the style to. TextView itself must also have its own BufferType for SPANNABLE.

If you need to do this several times or want to include indentation in your style, try creating a subclass of TextView that uses a special indent attribute and automatically applies span. I got a lot of benefit from this Custom Views and XML attributes from the Statically Typed blog, and the SO question is declaring an Android user interface user element using XML .

In 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 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); 
+12
source

All Articles