Android中TableLayout中的布局


1.在TableLayout下的代码及效果


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<TableRow>
        <TextView android:text="tmacsky" />
        <TextView android:text="tmacsky" />
        <TextView android:text="tmacsky" />
    </TableRow>
    <TableRow>
        <Button android:text="tmacsky" />
        <Button android:text="tmacsky" />
        <Button android:text="tmacsky" android:layout_weight="1" />
    </TableRow>
    <TableRow>
        <EditText />
        <EditText />
        <EditText android:layout_weight="1"/>
</TableRow>

1.png

单个列布局中只能用layout_weight来拉宽,如果布局很多就有点繁琐了,所以得用到下面的stretchColumns伸缩列。


2. Android:stretchColumns 伸缩列

代码和效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="0,1,2" >  //(此处表示第1,2,3列都拉宽屏幕)
    <TableRow>
        <TextView android:text="tmacsky1" />
        <TextView android:text="tmacsky2" />
        <TextView android:text="tmacsky3" />
    </TableRow>
    <TableRow>
        <Button android:text="tmacsky1" />
        <Button android:text="tmacsky2" />
        <Button android:text="tmacsky3" />
    </TableRow>
    <TableRow>
        <EditText />
        <EditText />
        <EditText />
    </TableRow>
</TableLayout>


2.png


由此可以想象伸缩列其实就是用weight来封装的。


3. android:collapseColumns缩进列


将第2个例子里的Android:stretchColumns缩进下加一行代码:

android:collapseColumns="0"

意思就是缩进第一列效果:


3.png



4,如果按钮里文字过多,则会出现下面这种情况:

4.png


超出屏幕了;这个时候如果想让按钮里的文字换行不超出屏幕则可以:

在第二个例子的Android:stretchColumns添加一行代码:

android:shrinkColumns="2"


5.png



5.实现下面的效果

6.png

在第二个例子里的EditText代码改成这样:

<TableRow>

        <EditText android:layout_span="2"/>

        <EditText android:layout_column="2"/>

</TableRow>

Layout_span=”2” 伸展2倍

Layout_column=”2”  位置固定到第三列



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值