android:lineSpacingMultiplier doesn't work in android:textAppearance
Asked Answered
P

3

7

I'm trying to add android:lineSpacingMultiplier in my textAppearance style (android:textAppearance), and it's not working. Am I doing something wrong?

TextAppearance style:

<style name="TextAppearance.Body1" parent="TextAppearance.AppCompat.Body1">
      <item name="android:lineSpacingMultiplier">1.25</item>
</style> 

Use of style:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="This is body 1\nThis is body 1"
    android:textAppearance="TextAppearance.Body1"
    />
Phyto answered 13/2, 2017 at 22:20 Comment(0)
P
9

For whatever reason android:lineSpacingMultiplier doesn't work as an item within your textAppearance. You'll either have to set it directly as a TextView attribute (using android:lineSpacingMultiplier), or create a regular style which "wraps" setting the textAppearance and lineSpacingMultiplier

Create a style

<style name="TextBody1">
    <item name="android:textAppearance">@style/TextAppearance.AppCompat.Body1</item>
    <item name="android:lineSpacingMultiplier">1.25</item>
</style>

or

<style name="TextBody1" parent="TextAppearance.AppCompat.Body1">
    <item name="android:lineSpacingMultiplier">1.25</item>
</style>

and then apply via style instead of android:textAppearance

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="This is body 1\nThis is body 1"
    style="@style/TextBody1"
    />
Phyto answered 13/2, 2017 at 22:20 Comment(0)
P
1

lineSpacingMultiplier as well as lineSpacingExtra you can apply only in style.

But as an alternative, you can use lineHeight attribute, which can be applied to the MaterialTextView using textAppearance.

Interesting note, that if you use a fully Material theme (not Bridge) all your TextView will auto-inflate to MaterialTextView, otherwise, you will need to specify <com.google.android.material.textview in your xml.

Promissory answered 18/10, 2020 at 14:39 Comment(0)
G
0

I was able to solve the issue by simply applying the style to the TextView itself instead of the textAppearance (similar to the accepted answer, but with a bit less code).

Here is a sample:

Style:

<style name="TextBody1" parent="TextAppearance.AppCompat.Body1">
  <item name="android:lineSpacingMultiplier">1.25</item>
</style> 

or simpler if you don't care about the parent:

<style name="TextBody1">
      <item name="android:lineSpacingMultiplier">1.25</item>
</style> 

Then in your View:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="This is body 1\nThis is body 1"
    style="@style/TextBody1"
    />

I am not sure why applying android:textAppearance to a TextView with a given style that defines android:lineSpacingMultiplier does not work (I would speculate that it may be due to the fact that the line spacing is on the style View itself instead of the textAppearance of the View) but this is a bit simpler than the accepted answer if you don't care about the parent.

Guildsman answered 3/5, 2018 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.