Android布局管理器总结

    科技2026-03-05  6

    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,指定第二列隐藏 --> <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,指定第三列允许拉伸 --> <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,指定第二列,第三列允许收缩 --> <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 :指定子组件在垂直方向的权重

    Processed: 0.021, SQL: 9