Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView lineSpacingMultiplier less than zero chops last line

I have a TextView where I want the line spacing to be less than 1.0, i.e. there will be less than the default amount of space between lines. I've done this:

  <TextView
    android:id="@+id/text_header"
    android:layout_width="@dimen/text_header_width"
    android:layout_height="wrap_content"
    android:text="@string/header_text"
    android:textSize="@dimen/text_header_text_size"
    android:typeface="helvetica"
    android:lineSpacingMultiplier="0.95" />

But in the text on the bottom line, the descenders (e.g. y's, g's, and p's) are being slightly chopped off. In other words, there are about 2 or 3 pixels worth of the bottom of those letters missing.

I've tried playing with margins and padding with no success. I seem to be able to fix the issue by using a specified height (with a little extra room) instead of wrap_content, but I'd like to avoid that if possible.

like image 317
parkerfath Avatar asked Jul 20 '12 08:07

parkerfath


1 Answers

From my testing it looks like an issue with android versions below api 21. I solved it like so :

In my case I use a TextView that has a maximum of 2 lines with ellipsize at the end.

Style for my TextView before api 21 (values/style.xml)

<style name="MyTextViewStyle" parent="Base.TextAppearance.AppCompat">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:gravity">left</item>
    <item name="android:textColor">@color/blue_dark</item>
    <item name="android:textSize">16sp</item>
    <item name="android:maxLines">2</item>
    <item name="android:ellipsize">end</item>
    <item name="android:lineSpacingExtra">-4dp</item>
    <item name="android:paddingTop">0dp</item>
    <item name="android:paddingBottom">4dp</item>
    <item name="android:includeFontPadding">false</item>
</style>

Style for my TextView for api 21 and after (values-v21/style.xml)

<style name="MyTextViewStyle" parent="Base.TextAppearance.AppCompat">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:gravity">left</item>
    <item name="android:textColor">@color/blue_dark</item>
    <item name="android:textSize">16sp</item>
    <item name="android:maxLines">2</item>
    <item name="android:ellipsize">end</item>
    <item name="android:lineSpacingExtra">-4dp</item>
    <item name="android:paddingTop">0dp</item>
    <item name="android:paddingBottom">0dp</item>
    <item name="android:includeFontPadding">true</item>
</style>

The secret here is mainly android:paddingBottom that is the opposite of android:lineSpacingExtra before api 21. On my devices it corrects the bug of the last line chopped off in the bottom.

NOTE : <item name="android:lineSpacingExtra">-4dp</item> looks like it is the equivalent to <item name="android:lineSpacingMultiplier">0.8</item>.

like image 157
Quentin G. Avatar answered Oct 21 '22 15:10

Quentin G.