如何在Android的TextView上去除顶部和底部的空白

66

当我把下面的 XML 包含到 layout 文件中时,我可以看到下面的图片。如果你看到了它,你会发现 TextView 有顶部和底部的空间。

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E1"
android:background="#ff00ff00"/>

enter image description here

我想要去掉这个空格。如何去掉它?这个叫什么? 如果有人知道,请告诉我。谢谢。


可能是重复的问题:Android:TextView:删除顶部和底部的间距和填充 - Mohammed Atif
14个回答

0

TopAlignedTextView代码的答案TopAlignedTextView@GitHub

通过布局使用:

<com.github.captain_miao.view.TopAlignedTextView
    android:id="@+id/text_a"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:text="@string/text_demo_a"
/>

enter image description here


0

在LinearLayout中,默认的padding可能会成为一个问题。尝试将其设置为0dp。这对我有用。


0

我的解决方法相当hacky,但我设法通过将文本视图的高度设置为静态并调整它,直到它刚好适合文本来使文本坐在我想要的位置。在我的情况下,我使用的字体样式的高度为64sp,因此我将我的文本视图的高度设置为50sp,这样就可以正常工作了。我还必须将foreground_gravity设置为底部。


-1
android:includeFontPadding="false"

你的回答可以通过添加更多关于代码的信息以及它如何帮助提问者来改进。 - Tyler2P

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接