You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为TableRow中的两个TextView添加小间隔?

如何为TableRow中的两个TextView添加小间隔?

嘿,我来给你分享几个实用的办法,轻松搞定TableRow里两个TextView的间隔问题~

  • 给TextView设置外边距(Margin)
    这是最直接省事的方法,你只需要给其中一个TextView添加左右外边距就行。比如给第二个TextView加个左边距,这样两个控件之间就会拉开距离啦。修改后的代码大概是这样:
<TableRow
    android:layout_weight="1"
    android:id="@+id/tableRow4"
    android:layout_height="wrap_content"
    android:gravity="center">

    <TextView
        android:text="Temperature"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:gravity="center"/>

    <TextView
        android:text="Humidity"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_marginLeft="8dp"/> <!-- 这里添加了左边距,数值可以按需调整 -->
</TableRow>

注意用match_parent替代旧的fill_parent哦,后者已经被官方废弃啦。

  • 在两个TextView之间添加空白View
    如果需要更灵活控制间隔的样式(比如加个背景色当分隔线),或者担心margin在某些布局组合下有冲突,可以在两个TextView中间插一个空的View:
<TableRow
    android:layout_weight="1"
    android:id="@+id/tableRow4"
    android:layout_height="wrap_content"
    android:gravity="center">

    <TextView
        android:text="Temperature"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:gravity="center"/>

    <!-- 空白间隔View -->
    <View
        android:layout_width="8dp"
        android:layout_height="match_parent"/>

    <TextView
        android:text="Humidity"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:gravity="center"/>
</TableRow>

这个View的宽度就是你想要的间隔大小,还能给它加android:background设置分隔线颜色,实用性拉满~

  • 额外小提示
    如果你的TableRow是嵌套在TableLayout里的,记得确认TableLayout有没有设置android:stretchColumns或者android:shrinkColumns属性,避免间隔影响两个TextView的宽度分配哦。

备注:内容来源于stack exchange,提问作者vico

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 12:14:35