Android5种布局类型,android驱动开发权威指南pdf

首先是layout_width和layout_height这两个控件属性,这两个值可以设置为wrap_content(控件大小由控件内容大小变化而变化)和match_parent(填满父窗体由父容器决定控件大小),也可以自定义大小。

改变btn3的layout_width的值使他变成match_parent:

在这里插入图片描述

改变三个按钮的layout_width的值,自定义为100dp、200dp、300dp:

在这里插入图片描述

同理layout_height也是可以一样根据自己的需求自己定义自己需要的高度大小。

![在这里插入图片描述](https://img-blog.csdnimg.cn/20190524102537446.?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shad
ow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNTg1ODAx,size_16,color_FFFFFF,t_70)

之后是orientation一个关键的属性,用于控制控件的排列方向,它有两个值 vertical表示线性布局垂直显示,horizontal表示线性布局水平显示。

将orientation的值变为vertical就会变成水平显示:

在这里插入图片描述

你也可以根据需要使用嵌套线性布局即在线性布局中再使用线性布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“horizontal”

<Button

android:id="@+id/btn1"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“A”

/>

<LinearLayout

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:orientation=“vertical”>

<Button

android:id="@+id/btn2"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“B”

/>

<Button

android:id="@+id/btn3"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“C”

/>

在这里插入图片描述

2.相对布局


相对布局是通过相对定位的方式指定控件位置,即以其它控件或父容器为参照物,摆放控件位置。

表1 设置控件间距的属性

| 控件属性 | 功能描述 |

| — | — |

| android:layout_marginTop | 设置当前控件上边界与某控件的距离 |

| android:layout_marginBottom | 设置当前控件底边界与某控件的距离 |

| android:layout_marginLeft | 设置当前控件左边界与某控件的距离 |

| android:layout_marginRight | 设置当前控件右边界与某控件的距离 |

表2 设置控件位置的属性

| 控件属性 | 功能描述 |

| — | — |

| android:layout_centerInparent | 设置当前控件位置位于父布局的中央位置 |

| android:layout_centerVertical | 设置当前控件位置位于父布局的垂直居中位置 |

| android:layout_centerHrizontal | 设置当前控件位置位于父布局的水平居中位置 |

| android:layout_above | 设置当前控件位于某控件上方 |

| android:layout_below | 设置当前控件位于某控件下方 |

| android:layout_toLeftOf | 设置当前控件位于某控件左侧 |

| android:layout_toRightOf | 设置当前控件位于某控件右侧 |

| android:layout_alignParentTop | 设置当前控件是否与父控件顶端对齐 |

| android:layout_alignParentLeft | 设置当前控件是否与父控件左对齐 |

| android:layout_alignParentRight | 设置当前控件是否与父控件右对齐 |

| android:layout_alignParentBottom | 设置当前控件是否与父控件底端对齐 |

| android:layout_alignTop | 设置当前控件的上边界与某控件的上边界对齐 |

| android:layout_alignBottom | 设置当前控件的下边界与某控件的下边界对齐 |

| android:layout_alignLeft | 设置当前控件的左边界与某控件的左边界对齐 |

| android:layout_alignRight | 设置当前控件的右边界与某控件的右边界对齐 |

表3 设置布局内边距的属性

| 布局属性 | 功能描述 |

| — | — |

| android:paddingTop | 设置布局顶部内边距的距离 |

| android:paddingBottom | 设置布局底部内边距的距离 |

| android:paddingLeft | 设置布局左边内边距的距离 |

| android:paddingRight | 设置布局右边内边距的距离 |

| android:padding | 设置布局四周内边距的距离 |

熟练的掌握以上的表,就能很好的运用相对布局了。下面通过示例来使用以上表中的内容。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:paddingTop=“100dp”

<Button

android:id="@+id/btn1"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“A”

android:layout_alignParentTop=“true”

/>

<Button

android:id="@+id/btn2"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“B”

android:layout_centerInParent=“true”

/>

<Button

android:id="@+id/btn3"

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“C”

android:layout_alignLeft="@+id/btn2"

android:layout_marginLeft=“50dp”

android:layout_above="@id/btn2"

/>

父布局与顶部边距为100dp,A与父控件顶端对齐,B位置位于父布局的中央位置,C的左边界与B控件的左边界对齐,C左边界与B控件的距离为50dp,C位于B控件的上方。

在这里插入图片描述

3.帧布局


帧布局是Android中最简单的一种布局,该布局为每个加入其中的控件创建一个空白区域(称为一帧,每个控件占据一帧。

表4 FrameLayout 属性

| 布局属性 | 功能描述 |

| — | — |

| android:foreground | 设置帧布局容器的前景图像(始终在所有子控件之上) |

| android:foregroundGravity | 设置前景图像显示位置 |

通过一个例子来熟悉一下帧布局:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:foreground="@drawable/b"

android:foregroundGravity=“center”

<Button

android:id="@+id/btn1"

android:layout_width=“300dp”

android:layout_height=“450dp”

android:text=“A”

/>

<Button

android:id="@+id/btn2"

android:layout_width=“150dp”

android:layout_height=“100dp”

android:text=“B”

/>

<Button

android:id="@+id/btn3"

android:layout_width=“50dp”

android:layout_height=“50dp”

android:text=“C”

/>

从运行结果可以看出,按钮都被前景图片给挡住了,所以要注意在使用帧布局的时候,一定要记住前景图片会始终保持在最上层。

在这里插入图片描述

当我们把前景图删除后:按钮就可以显示了。

在这里插入图片描述

4.表格布局


表格布局是以表格的形式排列空间的,通过行和列将界面划分为多个单元格,每个单元格都可以添加控件。

表5 TableLayout 布局属性

| 布局属性 | 功能描述 |

| — | — |

| android:stretchColumns | 拉伸列 |

| android:shrinkColumns | 收缩列 |

| android:collapseColumns | 隐藏列 |

表6 TableLayout 控件属性

| 控件属性 | 功能描述 |

| — | — |

| android:layout_column | 设置单元格显示位置 |

| android:layout_span | 设置单元格占据几行 |

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:stretchColumns=“1”

<Button

android:id="@+id/btn1"

android:layout_width=“wrap_content”

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值