Android布局

  此文章适合于初级学习Android布局的学员,如果有不恰当的地方请指点

  我介绍四种布局分别是

1:LinearLayout线性布局

LinearLayout线性布局类似于swing中的流式布局

LinearLayout线性布局有两种排布1.横向排布,2.竖向排布

决定性属性是必须有的

android:orientation:vertical(垂直方向)或者是horizontal(水平方向)

常用的属性有

android:gravity(设置的是控件自身上的位置)

android:layout——gravity(由父类决定控件的位置在哪里)

android:layout_weight(权重(给控件分配剩余空间))

我给大家举个例子


这是一个用LinearLayout线性布局做好的图

我们把它分成3步来操作

1.把左上和左下作为一个小父类,左上和左下分别嵌套在小父类中,并且父类控制他们的方向是垂直vertical,

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="左上" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:text="左下"></Button>
    </LinearLayout>
</LinearLayout>

2,‘’中间按钮‘’是一个小父类

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中间按钮" />
</LinearLayout>

3,右上按钮和右下按钮的原理和左上左下一样,就不重复讲了




2:第二种布局是TableLayout表格布局

常见方法:

android:shrinkcolumns(设置可收缩的列,内容过多则收缩,扩展到第二行)

android:stretchcolumns(设置可伸展的列)

android:collapsecolumns(设置可隐藏的列)

内部控件属性

android:layout_column(单元格在第几列显示)

android:layout_span(单元格占据列数)

举例子

我们简单的分析下,此表有三行三列

1,那么第一行是最简单的一步

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#00ff26">(背景颜色)

    <TextView
        android:layout_height="wrap_content"
        android:layout_weight="wrap_content"

        android:text="这是第一行"

        />

2,第二步一共有三列,

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#c4ce38">

    <TableRow>(类似于HTML中一个表格行)

        <TextView
            android:layout_height="wrap_content"
            android:layout_weight="wrap_content"
            android:background="#4d9750"
            android:text="第2行1列" />

        <TextView
            android:layout_height="wrap_content"
            android:layout_weight="wrap_content"
            android:background="#666b1c"
            android:text="第2行2列" />

        <TextView
            android:layout_height="wrap_content"
            android:layout_weight="wrap_content"
            android:background="#2f5848"
            android:text="第2行3列" />
    </TableRow>


</TableLayout>

3:其实第三行第二列被我隐藏掉了,并且可以看出我用了收缩的方法

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#b0a5c1"
    android:shrinkColumns="0,2"
    android:collapseColumns="1"
    >


    <TableRow>

        <TextView

            android:layout_height="wrap_content"
            android:layout_weight="wrap_content"
            android:background="#538958"
            android:shadowColor="0"
            android:text="第二个layouut第一行一列第二行一列" />

        <TextView

            android:layout_height="wrap_content"
            android:layout_weight="wrap_content"

            android:text="第3行2列" />

        <TextView
            android:layout_height="wrap_content"
            android:layout_weight="wrap_content"
            android:background="#ce8958"
            android:text="第二个layouut第一行一列第二行三列" />
    </TableRow>


</TableLayout>




3:第三种GridLayout网格布局

网格布局是4.0后添加的布局,跟表格布局有点像,但更加好用

方法

android:columnCount=""(设置几列)
android:rowCount=""(几行)




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值