lineSpacingExtra 不适用于 TextView

And*_*ndy 2 android

我有一个改变行与行之间空间的TextView习惯:TextAppearance

<style name="CustomAppearance">
    <item name="android:lineSpacingExtra">30sp</item>
</style>

<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Lorem ipsum lorem ipsum lorem ipsum lorem ipsum..."
  android:textAppearance="@style/CustomAppearance" />
Run Code Online (Sandbox Code Playgroud)

问题是lineSpacingExtra应用程序启动时该值被忽略。(虽然如果我使用CustomAppearancethroughstyle而不是textAppearance属性,它会起作用)

And*_*ndy 7

它不起作用,因为TextAppearancelineSpacingExtra内部不支持:

未包含的一些常见 TextView 属性包括 lineHeight[Multiplier|Extra]、lines、breakStrategy 和 hyphenationFrequency。TextAppearance 在字符级别工作,而不是段落级别,因此不支持影响整个布局的属性。Nick Butcher 设计的 TextView 样式

我的解决方案是使用MaterialTextViewlineHeight属性:

<com.google.android.material.textview.MaterialTextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Lorem ipsum lorem ipsum lorem ipsum lorem ipsum..."
          android:textAppearance="@style/CustomAppearance" />

<style name="CustomAppearance">
        <item name="lineHeight">30sp</item>
</style>
Run Code Online (Sandbox Code Playgroud)