Android中移除TextView上下的空间

我们知道在Android中,使用TextView的时候,文字的上下都会默认留一些空间。特别是TextView中添加一个背景颜色的时候,就会特别明显。如果你想移除这个空间,设置:

android:padding="0dp"

没有任何效果,甚至设置padding为负数,都不能减少其空间。正确的做法如下:

android:includeFontPadding="false"

TextView的android:includeFontPadding属性,官方文档的解释是:

Leave enough room for ascenders and descenders instead of using the font ascent and descent strictly. (Normally true).

大概的意思是:用来设置是否预留足够文字上下行距离的,而不是用字体的行间距,一般情况是true

参考StackOverflow上的答案。这里是官方文档