使用TableLayout

最近有一个需求是显示横向显示两个TextView,左边的TextView可以多行显示。

形式如下:

情况1
AAAAAAAAAAAA bbb
情况2
AAAAAAAAAAAA bbb
AAAAAAAA 

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left" >
        <TextView
            android:id="@+id/right_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="bbb"
            android:layout_alignParentRight="true" />

        <TextView
            android:id="@+id/left_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="AAAAAAAAAAAAAAAA"
            android:layout_toLeftOf="@id/right_text"
            android:layout_marginRight="10dp" />
    </RelativeLayout>
最开始就是用上面的RelativeLayout的方式写的,然后发现在一些手机上(我使用的api15以下都不能正常显示情况1,即android:gravity="left" 没起作用)。

然后就考虑使用其他的布局了,最后发现TableLayout比较方便实现这个需求。代码如下:

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="0" >
        <TableRow>
            <TextView
                android:id="@+id/left_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="AAAAAAAAAAAAAAAA"
                android:layout_marginRight="10dp" />
            <TextView
                android:id="@+id/right_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="bbb"
                android:layout_toLeftOf="@id/right_text" />
        </TableRow>
</TableLayout>

关于TableLayout的使用。
TableLayout属性: 
android:collapseColumns:将TableLayout里面指定的列隐藏,若有多列需要隐藏,请用逗号将需要隐藏的列序号隔开。            
android:stretchColumns:设置指定的列为可伸展的列,以填满剩下的多余空白空间,若有多列需要设置为可伸展,请用逗号将需要伸展的列序号隔开。     android:shrinkColumns:设置指定的列为可收缩的列。当可收缩的列太宽(内容过多)不会被挤出屏幕。当需要设置多列为可收缩时,将列序号用逗号隔开。
就是说可以在TableLayout中设置是否显示,是否可以伸展,是否可以收缩。我上面的例子中设置了android:shrinkColumns="0” ,即left_text是可以收缩的。 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值