Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the relationship between the font size and the height of TextView in android?

Tags:

android

I occured a problem when using RadioButton. The RadioButton's layout_height must be 15dp, and the offical design guide says that 1sp is 1dp, so I set the font size to 1sp, but the TextView's layout_height is a little bigger than 15dp. It seems that when the font size is 15sp, the layout_height of the TextView is 18dp. So what's the relationship between the font size and the layout_height?

like image 239
Perqin Avatar asked Mar 13 '23 22:03

Perqin


1 Answers

First thing I wanted to mention is the font-padding, which is by default included into the TextView size. So to make TextView's size closer to the size of the text itself - specify android:includeFontPadding="false"

Now, if you compare View with layout_height equals to 20 sp and TextView with TextSize 20sp - they are going to be almost equally high. (There's small difference, as text might contain very high letter like "Å" or the letter like "y" which goes below the baseline)

About sp vs dp.

Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.

It depends on how big is your font (Settings->Accessibility->Enable/Disable Large Text). You can change it and see the effect.

Hope, it helps.

like image 62
Konstantin Loginov Avatar answered Apr 26 '23 04:04

Konstantin Loginov