Android布局管理器

Android中布局管理器本身也是一个界面控件,所有的布局管理器都是ViewGroup类的子类,都可以当作容器类来使用。因此,可以在一个布局管理器中嵌套其他布局管理器。Android中常用的布局管理器主要有:线性布局,表格布局,相对布局,层布局以及网格布局。

  • 线性布局:它用LinearLayout类表示,所谓的线性是指所有的控件沿着同一个方向进行排列,在Android中,只提供了水平和垂直两种方向,可通过android:orientation属性进行设置,默认为水平。

常见属性:

android:gravity:设置线性布局内控件的对齐方式。例如:bottom|center_horizental代表出现在屏幕底部,而且水平居中。

android:orientation:设置线性布局内控件的排列方向,只有vertical或horizontal两种。

android:layout_weight:设置线性布局内控件的宽度或高度所占剩余空间的权重。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="上"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:orientation="horizontal"
        >
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="左"
            />
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:text="中"
            />
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="右"
            />
    </LinearLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="下"
        />

</LinearLayout>
  • 表格布局:它用TableLayout类表示,但并未提供相关属性来设置包含几行几列,而是通过在TableLayout中添加TableRow来添加行。

常见属性:

android:collapseColumns:隐藏指定列,其值为列所在的序号,从0开始。

android:layout_column:指定控件在TableRow中所处的列。

android:layout_span:指定控件所跨越的列数。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="2"
    >
    <TableRow>
        <Button  android:text="按钮一"/>
        <Button  android:text="按钮三"
            android:layout_column="2"/>
    </TableRow>
    <TableRow>
        <Button android:text="按钮四"/>
        <Button android:text="按钮五"/>
        <Button android:text="按钮六"/>
    </TableRow>
    <TableRow>
        <Button android:text="按钮七"/>
        <Button android:text="按钮八" android:layout_span="2"/>
    </TableRow>

</TableLayout>

其他控件下次分享!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值