Android TextView 文字空白解决方案

这里写图片描述

*Android的TextView控件会在文字上下加上默认的padding值,该值随着文字大小成正比变化,只要为TextView设置android:includeFontPadding=”false”;就可以减少大部分上下空白,而且如果文字sp值小,也就看不出空白了。ps.在Eclipse编辑器上可能看不出效果,在真机上就有效果了,一下是xml演示代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tv_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff00ff"
        android:text="平"
        android:textSize="56sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/tv_one"
        android:background="#ff00ff"
        android:includeFontPadding="false"
        android:text="平"
        android:textSize="56sp" />

    <TextView
        android:id="@+id/tv_three"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_one"
        android:layout_marginTop="20dp"
        android:background="#ff00ff"
        android:text="平"
        android:textSize="14sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/tv_three"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/tv_one"
        android:background="#ff00ff"
        android:includeFontPadding="false"
        android:text="平"
        android:textSize="14sp" />

</RelativeLayout>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解决 AndroidTextView 上下空白较大的方法有以下几种: 1. 设置行高(line height):可以通过在 XML 中设置 TextView 的行高来调整上下空白的大小。例如: ``` <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:lineHeight="20dp" android:text="Hello World!" /> ``` 2. 设置 padding 值:可以通过在 XML 中设置 TextView 的 padding 值来调整上下空白的大小。例如: ``` <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="5dp" android:paddingBottom="5dp" android:text="Hello World!" /> ``` 3. 调整字体大小(text size):可以通过在 XML 中设置 TextView 的字体大小来调整上下空白的大小。例如: ``` <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:text="Hello World!" /> ``` 4. 调整布局属性(layout attributes):可以通过在 XML 中设置 TextView 的布局属性来调整上下空白的大小。例如,如果 TextView 是在 LinearLayout 中,可以设置 layout_gravity 属性来调整上下位置。例如: ``` <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:layout_gravity="center_vertical" /> </LinearLayout> ``` 希望这些方法可以帮助你解决问题!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值