目录

1-1  布局通用的属性

1-2   线性布局(LinearLayout)

1、常见属性:

2、线性布局的例子:

 1-3  相对布局(RelativeLayout)

1、常见属性:

2、 相对布局的例子:

 1-4  帧布局(FrameLayout)

1.常用属性

2、帧布局例子:

1-5 表格布局(TableLayout)

1、常见属性:

2、表格布局例子:

1-6 网格布局(GridLayout)

1、常用属性:

2、网格布局的例子

1-7 约束布局ConstraintLayout

 1、ConstraintLayout例子:

1-1  布局通用的属性

属性名称功能android:id设置布局的标识android:layout_width设置布局的宽度android:layout_height设置布局的高度android:layout_margin设置当前布局与屏幕边界或与周围控件的距离android:background设置布局的背景android:padding设置当前布局与该布局中控件的距离

1-2   线性布局(LinearLayout)

        线性布局就是在该布局内的子控件按竖直或者按水平排列。

1、常见属性:

属性功能android:orientation设置布局内控件的排列顺序android:weight在布局内设置控件权重,属性值可直接写int

注:

android:orientation属性有两个参数: (1) vertical:表示在LinearLayout布局中从上到下竖直排序。

(2)horizontal:表示在LinearLayout布局中从左到右水平排序。

android:weight属性就是在LinearLayout布局中的控件的大小按比例来分配。

2、线性布局的例子:

        如下代码:将按钮竖直排列,并且按钮的高度按1:1:2的大小分配

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"

tools:context=".MainActivity">

android:text="按钮1"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

/>

android:text="按钮2"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

/>

android:text="按钮3"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="2"

/>

下面是效果图: 

 1-3  相对布局(RelativeLayout)

        相对布局是通过以父容器或者子控件为参照物的方式来指定子控件的位置。

1、常见属性:

属性名称功能android:layout_centerInParent设置当前控件位于父布局的中央位置android:layout_centerVertical设置当前控件位于父布局的垂直居中位置android:layout_centerHorizontal设置当前控件位于父控件的水平居中位置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设置当前控件的右边界与某控件的右边界对齐

2、 相对布局的例子:

效果图:

代码如下: 

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="150dp"

android:layout_height="150dp"

android:layout_alignParentLeft="true"

android:text="左上角"

android:textSize="30dp" />

android:layout_width="150dp"

android:layout_height="150dp"

android:layout_alignParentBottom="true"

android:text="左下角"

android:textSize="30dp" />

android:layout_width="150dp"

android:layout_height="150dp"

android:layout_alignParentRight="true"

android:text="右上角"

android:textSize="30dp" />

android:layout_width="150dp"

android:layout_height="150dp"

android:layout_alignParentBottom="true"

android:layout_alignParentRight="true"

android:text="右下角"

android:textSize="30dp" />

android:layout_width="150dp"

android:layout_height="150dp"

android:layout_centerInParent="true"

android:text="中间"

android:textSize="30dp" />

 1-4  帧布局(FrameLayout)

        这个布局直接在屏幕上开辟出一块空白的区域,当我们往里面添加控件的时候,会默认把他们放到这块区域的左上角,而这种布局方式却没有任何的定位方式。

1.常用属性

属性名称功能android:foreground设置帧布局容器的前景图像android:foregroundGravity设置前景图像显示的位置

2、帧布局例子:

代码: 

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:foreground="@mipmap/ic_launcher"

android:foregroundGravity="left">

android:layout_width="100dp"

android:layout_height="100dp"

android:background="@color/purple_700"

tools:ignore="SpeakableTextPresentCheck" />

android:layout_width="157dp"

android:layout_height="162dp"

android:background="@color/purple_200"

tools:ignore="SpeakableTextPresentCheck" />

android:layout_width="200dp"

android:layout_height="200dp"

android:background="@color/teal_200"/>

1-5 表格布局(TableLayout)

        表格布局(TableLayout是继承LinearLayout)就是采用行、列的形式来管理布局控件。

1、常见属性:

属性名称功能android:layout_column设置该控件显示位置,如android:Layout_column="1"表示第2个位置显示android:layout_span设置该控件占几行,默认是1行android:stretchColumns设置可被拉伸的列。android:shrinkColumns设置可被收缩的列。android:collapseColumns设置可被隐藏的列。

注意:列的宽度是由该列中最宽那个决定 

2、表格布局例子:

 效果图:

代码如下: 

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:stretchColumns="2">

android:layout_column="0"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="按钮1"

android:textSize="30sp"

/>

android:layout_column="1"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="按钮2"

android:textSize="30sp"

/>

android:layout_column="1"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="按钮3"

android:textSize="30sp"

/>

android:layout_column="2"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="按钮4"

android:textSize="30sp"

/>

android:layout_column="2"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="按钮5"

android:textSize="30sp"

/>

android:layout_column="0"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="按钮6"

android:textSize="30sp"

/>

android:layout_column="2"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="按钮7"

android:textSize="30sp"

/>

1-6 网格布局(GridLayout)

1、常用属性:

属性名称功能android:layout_gravity用来设置该View相对与父View的位置android:orientation设置布局内控件的排列顺序android:columnCount设置列数android:rowCount设置行数android:layout_columnSpan横跨几列android:layout_rowSpan横跨几行android:layout_column第几列android:layout_row第几行

2、网格布局的例子

效果图:

 代码:

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:columnCount="4"

android:orientation="horizontal"

android:rowCount="6" >

android:layout_columnSpan="4"

android:layout_gravity="fill"

android:layout_marginLeft="5dp"

android:layout_marginRight="5dp"

android:background="#D5DEDF"

android:text="0"

android:textSize="50sp" />

android:layout_columnSpan="2"

android:layout_gravity="fill"

android:text="回退" />

android:layout_columnSpan="2"

android:layout_gravity="fill"

android:text="清空" />