Android布局管理器总结

  • 布局管理器
  • 一、相对布局管理器(RelativeLayout)
  • 二、线性布局管理器(LinearLayout)
  • 三、帧布局管理器(FrameLayout)
  • 四、表格布局管理器(TableLayout)
  • 五、网格布局管理器(GridLayout)

布局管理器

android:layout_width:用来设置布局宽度。
android:layout_height:用来设置布局高度。以上两个属性在布局管理器中一般设置为match_parent,即与父容器相同。
android:paddingBotton:设置底内边距。
android:paddingLeft:设置左内边距。
android:paddingRight:设置右内边距。
android:paddingTop:设置顶内边距。以上四个属性可以设置为具体数值,比如16dp;也可以使用尺寸资源进行定义:

一、相对布局管理器(RelativeLayout)

在activity_main.xml中使用RelativeLayout标签来定义相对布局:

android:gravity属性用来设置各组件摆放方式。比如居中摆放。android:ignoreGravity属性用来指定哪个组件不受android:gravity所影响。比如利用android:gravity设置所有组件居中摆放,又用android:ignoreGravity设置组件1不受android:gravity影响,这时组件1就会恢复原来的摆放方式。


对于参数值为id

属性 功能
android:layout_above 将该控件的底部置于给定ID的控件之上
android:layout_below 将该控件的底部置于给定ID的控件之下
android:layout_toLeftOf 将该控件的右边缘与给定ID的控件左边缘对齐,将该组件放在其他某个组件左边
android:layout_toRightOf 将该控件的左边缘与给定ID的控件右边缘对齐,将该组件放在其他某个组件右边
android:layout_alignTop 将该控件的顶部边缘与给定ID的顶部边缘对齐
android:layout_alignBottom 将该控件的底部边缘与给定ID的底部边缘对齐
android:layout_alignLeft 将该控件的左边缘与给定ID的左边缘对齐
android:layout_alignRight 将该控件的右边缘与给定ID的右边缘对齐


对于参数值为true,相对于父组件

属性 功能
android:layout_alignParentTop 如果为true,将该控件的顶部与其父控件的顶部对齐
android:layout_alignParentBottom 如果为true,将该控件的底部与其父控件的底部对齐
android:layout_alignParentLeft 如果为true,将该控件的左部与其父控件的左部对齐
android:layout_alignParentRight 如果为true,将该控件的右部与其父控件的右部对齐

和居中有关的一些属性

属性 功能
android:layout_centerHorizontal 如果为true,将该控件的置于水平居中
android:layout_centerVertical 如果为true,将该控件的置于垂直居中
android:layout_centerInParent 如果为true,将该控件的置于父控件的中央;

Margin和Padding属性:

  • Margin:设置组件与父容器(通常是布局)的边距
属性 功能
android:layout_margin: 指定控件的四周的外部留出一定的边距
android:layout_marginLeft: 指定控件的左边的外部留出一定的边距
android:layout_marginTop: 指定控件的上边的外部留出一定的边距
android:layout_marginRight: 指定控件的右边的外部留出一定的边距
android:layout_marginBottom: 指定控件的下边的外部留出一定的边距
  • Padding:设置组件内部元素间的边距(可以理解为填充)
属性 功能
android:padding : 指定控件的四周的内部留出一定的边距
android:paddingLeft: 指定控件的左边的内部留出一定的边距
android:paddingTop: 指定控件的上边的内部留出一定的边距
android:paddingRight: 指定控件的右边的内部留出一定的边距
android:paddingBottom: 指定控件的下边的内部留出一定的边距

这两个后面都跟着一个参数,通常用dp作为单位,eg:android:margin = “10dp”

二、线性布局管理器(LinearLayout)

线性布局内的控件不换行or换列,组件依次排列,超出容器的控件则不会被显示

属性说明

属性 功能
android:orientation 取值为horizontal(水平)或vertical(竖直)
android:layout_weight 为容器内的组件设置权重,表示当所有控件全部排列完毕后,该被设置的组件占父容器剩余空白部分的多少比重
android:layout_gravity 为容器内的控件设置该控件在父容器中的对齐方式 (当父容器线性设置为 vertical 纵向时,则只有设置与左右相关的值才起作用,ex:left、right当父容器线性设置为 horizontal 横向时,则只有设置与上下相关的值才起作用,ex:top、bottom)
android:gravity 设置控件上面的文字 在 该组件里面的对齐方式

三、帧布局管理器(FrameLayout)

帧布局管理器为每一个放入其中的组件创建一个空白的区域,这些组件按照先后顺序层叠放置,后面的组件会覆盖前面的组件。
在activity_main.xml文件中用FrameLayout标签来定义帧布局管理器。

四、表格布局管理器(TableLayout)

表格布局有TableLayout所代表,TableLayout继承了LinearLayout,因此他的本质依然是LinearLayout。
表格布局采用行、列的形式来进行管理,在使用的时候不需要声明多少行、多少列,而是通过添加TableRow、其他组件来控制表格的行数和列数

android:collapseColumns="",隐藏某列。可隐藏多列(多列序号间用英文逗号 , 连接),取值为数字,第几列就取几(注意:列的索引从0开始)android:stretchColumns="",表示 被设置的这些列 可拉伸 (注意:TableLayout中列的索引从0开始)android:shrinkColumns=""用于:设置某列可收缩(当一行中的内容超出屏幕宽度时,缩小该列的空间使这行的内容展示出来),可多行设置-
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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=".MainActivity"    android:orientation="vertical">        <TableLayout        android:id="@+id/tableLayout_one"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:collapseColumns="1"        >        <TableRow>            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="普通按钮"/>            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="隐藏按钮"/>            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="普通按钮"/>        TableRow>        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="独自一行的按钮"/>    TableLayout>        <TableLayout        android:id="@+id/tableLayout_two"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:stretchColumns="2">        <TableRow>            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="普通按钮"/>            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="普通按钮"/>            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="拉伸按钮"/>        TableRow>    TableLayout>    <TableLayout        android:id="@+id/tableLayout_three"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        >        <TableRow>            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="普通按钮111111111111111111111111"/>            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="拉伸按钮"/>            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="拉伸按钮"/>        TableRow>    TableLayout>        <TableLayout        android:id="@+id/tableLayout_four"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:shrinkColumns="1,2"        >        <TableRow>            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="普通按钮111111111111111111111111"/>            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="收缩按钮"/>            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="收缩按钮"/>        TableRow>    TableLayout>LinearLayout>

五、网格布局管理器(GridLayout)

GridLayout网格布局管理器是android 4.0 以后才增加的布局管理器

1.android:columnCount :设置网格的最大列数2.android:orientation:设置网格中组件的排列方式3.android:rowCount :设置网格的最大行数

GridLayout中的内部类LayoutParams——用来控制各子组件的分布

1.android:layout_column :用来指定子组件位于网格布局管理器的第几列。从第0列算起。2.android:layout_columnSpan :设置子组件横跨几列。3.android:layout_columnWeight:设置子组件在水平方向的权重,用来分配水平方向剩余空间4.android:layout_gravity :用来设置子组件以什么方式占据网格空间5.android:layout_row :指定子组件位于第几行。6.android:layout_rowSpan :指定子组件纵向跨几行。7.android:layout_rowWeight :指定子组件在垂直方向的权重

更多相关文章

  1. Android(安卓)Vertical TextView 文字竖排
  2. 项目中那些事|控件之TextView
  3. 相对布局属性
  4. Android(安卓)Studio之安卓学习布局管理器
  5. [android]控件Button常用属性
  6. Android相对布局属性全集
  7. android属性
  8. android中点击事件的4种写法
  9. Android(安卓)布局各个属性的含义

随机推荐

  1. Android 解析字符乱码解决
  2. Android 添加系统设置属性
  3. Android AsyncTask 源码解析
  4. Android之Hello World
  5. 使用arm-eabi-addr2line, ndk-stack工具
  6. android错误:Installation error: INSTALL
  7. Android(安卓)有两种办法可以监控SDCARD
  8. Android RelativeLayout和LinearLayout性
  9. Android 网络管理
  10. Android基础 : Android(安卓)ContentProv