Разобрался, как заставить висячие отступы работать на мой собственный проект. В основном вам нужно использовать android.text.style.LeadingMarginSpan и применить его к вашему тексту с помощью кода. LeadingMarginSpan.Standard принимает конструктор полного отступа (1 параметр) или висячий отступ (2 параметра), и вам нужно создать новый объект Span для каждой подстроки, к которой вы хотите применить стиль. Самому TextView также нужно будет установить для параметра BufferType значение SPANNABLE.
Если вам нужно сделать это несколько раз или вы хотите включить отступы в свой стиль, попробуйте создать подкласс TextView, который принимает пользовательский атрибут отступа и применяет диапазон автоматически. Я получил много пользы от этого учебника по пользовательским представлениям и атрибутам XML из блога со статической типизацией и вопроса SO Объявление пользовательского элемента пользовательского интерфейса Android с использованием 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);