Android开发布局之帧布局-表格布局-网格布局

3. 帧布局 FrameLayout

帧布局的特点为从父容器的左上角开始绘制布局,因此所有位于帧布局之内的子控件或者容器之间可以互相覆盖。

1. 标签 <FrameLayout />

2. 基本属性

  1. id :表示当前布局或控件的唯一标识,构建后自动在R.java文件中生成一串标识符,可作为查找和引用控件的参考。
  2. layout_width:表示当前布局的宽度,可以使用 match_parentwrap_content 来表示当前的布局大小,或直接使用dp值固定大小。
  3. layout_height :表示当前布局的高度,可以使用 match_parentwrap_content 来表示当前的布局大小,或直接使用dp值固定大小。
  4. background :表示当前布局的背景,可以用十六进制的RGB、ARGBRRGGBB、AARRGGBB来表示,也可以调用 drawble 文件夹当中的图片作为背景。
  5. padding : 表示当前布局内部空间的填充值,一般使用dp来作为衡量单位,线性布局从左上角开始,也可以选择方向paddingTop,paddingLeft,paddingRight,paddingBottom
  6. margin : 表示当前控件或布局距离周围空间布局或父布局的距离,可选方向为margin_left,margin_top,margin_bottom,margin_right
  7. forground:表示当前帧布局中控件的前景,可以是十六进制颜色或图片,永远显示在当前控件的最上层。
  8. forground_gravity:表示当前控件中前景的布局位置,取值可以是 top\right\bottom等。
  9. layout_gravity: 用来处理当前帧布局中子控件位于帧布局当中的位置,因此此属性一定处于子控件的标签中。

3. 代码实例

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FrameLayout"
    android:foreground="@drawable/ic_launcher_background"
    android:foregroundGravity="center">

   <TextView
       android:layout_width="300dp"
       android:layout_height="300dp"
       android:background="#f00"
       ></TextView>

    <TextView
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:background="#ff453216"
        ></TextView>

    <TextView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#f66565"
        ></TextView>


</FrameLayout>

4. 表格布局 TableLayout

表格布局的特点是控件会直接在布局中占用一行的大小,但和线性布局有差别,使用一个button控件在表格布局中和在线性水平布局中是不一样的,属性的宽高可设置为wrap_content。另外如果要多行显示可以使用标签<TableRow >并将每行中的控件放置其中。缺点是布局比较呆板,适用于计算器类的界面。

1. 标签:<TableLayout >

2. 基本属性:

  1. id :表示当前布局或控件的唯一标识,构建后自动在R.java文件中生成一串标识符,可作为查找和引用控件的参考。
  2. layout_width:表示当前布局的宽度,可以使用 match_parentwrap_content 来表示当前的布局大小,或直接使用dp值固定大小。
  3. layout_height :表示当前布局的高度,可以使用 match_parentwrap_content 来表示当前的布局大小,或直接使用dp值固定大小。
  4. background :表示当前布局的背景,可以用十六进制的RGB、ARGBRRGGBB、AARRGGBB来表示,也可以调用 drawble 文件夹当中的图片作为背景。
  5. padding : 表示当前布局内部空间的填充值,一般使用dp来作为衡量单位,线性布局从左上角开始,也可以选择方向paddingTop,paddingLeft,paddingRight,paddingBottom
  6. margin : 表示当前控件或布局距离周围空间布局或父布局的距离,可选方向为margin_left,margin_top,margin_bottom,margin_right
  7. layout_column/ layout_span :子控件跨列显示以及跳列显示.表格布局两行以上才有效果
  8. stretch_column: 拉伸某列,取值为int,其中下标为0开始表示第1列,控件没有占满屏幕生效
  9. shrink_column: 收缩某列,取值同上,控件溢出屏幕生效
  10. collapse_column:隐藏某列,取值同上

3. 代码实例:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TableLayout">
    <TableRow>

        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button1"
           ></Button>

        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button2"
            android:layout_column="2"

           ></Button>

        <Button
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button3"></Button>

    </TableRow>
    <TableRow>

        <Button
            android:id="@+id/btn4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button1"
            ></Button>

        <Button
            android:id="@+id/btn5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button2"
            ></Button>

        <Button
            android:id="@+id/btn6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button3"
            android:layout_span="2"></Button>
  </TableRow>
</TableLayout>

5. 网格布局 GridLayout

1. 标签 <GridLayout />

2. 基本属性:

  1. id :表示当前布局或控件的唯一标识,构建后自动在R.java文件中生成一串标识符,可作为查找和引用控件的参考。

  2. layout_width:表示当前布局的宽度,可以使用 match_parentwrap_content 来表示当前的布局大小,或直接使用dp值固定大小。

  3. layout_height :表示当前布局的高度,可以使用 match_parentwrap_content 来表示当前的布局大小,或直接使用dp值固定大小。

  4. background :表示当前布局的背景,可以用十六进制的RGB、ARGBRRGGBB、AARRGGBB来表示,也可以调用 drawble 文件夹当中的图片作为背景。

  5. padding : 表示当前布局内部空间的填充值,一般使用dp来作为衡量单位,线性布局从左上角开始,也可以选择方向paddingTop,paddingLeft,paddingRight,paddingBottom

  6. margin : 表示当前控件或布局距离周围空间布局或父布局的距离,可选方向为margin_left,margin_top,margin_bottom,margin_right

  7. *orientation:*设置排列方式水平或垂直

  8. *layout_gravity:*设置对其方式,左右上下居中,如需要同时使用两个方向可以使用|,例如 left|bottom

  9. *rowcount:*设置行数,赋值为数字,例如rowcount = 4则有4行

  10. *columncount:*设置列数,用法同上

控件属性:

  1. *layout_row :*设置控件位于第几行,赋值为数字,例如layout_row = 2则控件位于第3行(从0开始计算)

  2. layout_column : 设置控件位于第几列,用法同上

  3. layout_rowSpan: 设置控件纵向跨几行,赋值为数字,例如layout_rowSpan = 4则控件纵向跨4行

  4. layout_columnSpan: 设置控件横向跨几列,用法同上

3. 代码实例:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GridLyout"
    android:orientation="horizontal"
    android:columnCount="4"
    android:rowCount="4">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button1"
        android:layout_row="4"
        android:layout_column="2"
        ></Button>

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button2"
        android:layout_rowSpan="2"></Button>

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button3"></Button>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button4"></Button>

    <Button
        android:id="@+id/btn5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button5"
        android:layout_columnSpan="2"
        ></Button>

    <Button
        android:id="@+id/btn6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button6"
        android:layout_rowSpan="2"></Button>

    <Button
        android:id="@+id/btn7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button7"></Button>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button8"></Button>

</GridLayout>
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值