Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android lineHeight vs lineSpacingExtra

can I know what is the difference between lineHeight and lineSpacingExtra in android xml. I tried to compare both, but I got the different result (I need the lineHeight function, however it is only supported in API 28).

Here is part of my code:

left:

android:textSize="14sp"

android:lineSpacingExtra="6dp"

right:

android:textSize="14sp"

android:lineHeight="20dp"

Result:

enter image description here

Any solution for this? Thank you.

like image 670
hallz12 Avatar asked Jan 15 '19 11:01

hallz12


People also ask

What is lineSpacingExtra android?

The difference is that android:lineSpacingExtra add extra spacing between lines of text of TextView and android:lineSpacingMultiplier work as scale factor for height of line space. in other words, each line height will be height*multiplier + extra. Follow this answer to receive notifications.

How do I set line spacing in android?

Just we need to add an attribute with your TextView that give the spacing with lines. These are –android:lineSpacingExtra, android:lineSpacingMultiplier – use these two attributes with your TextView in XML file. or Programatically call setter method of that attribute textView. setLineSpacing().

What is line height in TextView?

For example, 1.2 . It means, the spacing is 120% of the text size. In example above, the line height is 19sp and the text size is 15sp. If we translate it into scale, it become. 19/15 = 1.26.

What is TextAppearance in android?

TextAppearance allows you to define text-specific styling while leaving a View 's style available for other uses. Note, however, that if you define any text attributes directly on the View or in a style, those values would override the TextAppearance values.


1 Answers

You mentioned you want to set lineHeight in pre-API 28, an alternative approach would be to just set a small lineSpacingExtra / lineSpacingMultiplier value (as you have shown). Otherwise, there are many solutions to setting a line height yourself in this related question.

I'll briefly cover the differences below, with my summary and the official documentation.

android:lineHeight is the total height of each line. This includes the text, as well as any padding on the top and bottom.

Explicit height between lines of text. If set, this will override the values set for lineSpacingExtra and lineSpacingMultiplier.

android:lineSpacingExtra is the additional spacing added after each line of text (except the last one).

Extra spacing between lines of text. The value will not be applied for the last line of text.

Finally, you may be interested in android:lineSpacingMultiplier. This is similar to lineSpacingExtra, but with a multiplier value of the current height (e.g. 1.2) instead of a fixed value.

Extra spacing between lines of text, as a multiplier. The value will not be applied for the last line of text.

Further information (besides the included quotas) is available in the TextView documentation.

like image 121
Jake Lee Avatar answered Sep 30 '22 18:09

Jake Lee