使用TableLayout

来源:互联网 发布:社交网络观看 编辑:程序博客网 时间:2024/04/27 23:55

最近有一个需求是显示横向显示两个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