一 布局文件的创建
1.点左面layout文件夹,单击鼠标右键,选New—>layout resource file
第二种方式
点左面layout文件夹,单击鼠标右键,选New—>xml-use-选layout xml file/values xml file
二 布局文件类型
1.表格布局(TableLayout)
以表格形式排列控件,通过行和列页面分为多个单元格,每个单元格都可以添加控件。
表格布局需要和TableRow配合使用,每一行都由TableRow对象组成,因为TableRow的数量决定表格的行数,列数由最多控件的TableRow决定。
布局属性 | 功能 |
---|---|
android:stretchColumns | 该列被拉伸 |
android:shrinkColumns | 该列被收缩 |
android:collapseColumns | 该列被隐藏 |
控件属性 | 功能 |
---|---|
android:layout_column | 设置单元格显示位置下标值从0开始的 |
android:layout_span | 设置单元格占几列,默认1列 |
例子:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:stretchColumns="2"
android:layout_height="match_parent">
<TableRow
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button5"
android:layout_column="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
</TableLayout>
效果:
2.线性布局(LinearLayout)
特点:以水平或或垂直反向排列,水平排列时,从左到右;垂直排列时,从上到下,控件的方向用orientation
控制,默认用android:orientation="horizontal"
,是水平排列,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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"
android:textSize="20sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"
android:textSize="20sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3"
android:textSize="20sp"
/>
</LinearLayout>
效果图
注意
这里的layout_width的属性值不能设置成match_parent(填充父窗体),其余的控件会被挤出来,显示不出来,layout_width设置成wrap_content(包裹内容)
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮1"
android:textSize="20sp"
/>
<!--这里的layout_width的属性值不能设置成match_parent(填充父窗体),
其余的控件会被挤出来,显示不出来-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"
android:textSize="20sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3"
android:textSize="20sp"
/>
</LinearLayout>
这样的效果是
在上面三个按钮的内个图,可以发现按钮并没有把行填满,在真实的情况下,并没有这样的,影响美观。
这里可以用layout_weight
设置权重,在button中使用属性时,控件宽度不在由layout_width
决定,指定为0dp就可以了。
<?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:layout_width="0dp"
android:layout_height="wrap_content"
android:text="按钮1"
android:textSize="20sp"
android:layout_weight="1"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="按钮2"
android:textSize="20sp"
android:layout_weight="2"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="按钮3"
android:textSize="20sp"
android:layout_weight="1"
/>
</LinearLayout>
效果为:
3.相对布局(RelativeLayout)
相对布局是通过相对定位的方式指定控件位置,即以其他控件或父容器为参照物,来摆放控件位置。
控件属性名称 | 功能 |
---|---|
android:layout_centerInParent | 当前控件位于父布局的中央位置 |
android:centerVertical | 当前控件位于父布局垂直居中位置 |
android:layout_centerHorizontal | 当前控件位于父控件水平居中位置 |
android:layout_above | 当前控件位于某控件上方 |
---|---|
android:layout_below | 当前控件位于某控件下方 |
android:layout_toLeftOf | 当前控件位于某控件左侧 |
android:layout_toRightOf | 当前控件位于某控件右侧 |
android:layout_alignParentTop | 当前控件位于布局顶端 |
android:layout_alignParentBottom | 当前控件位于布局底端 |
android:layout_alignParentLeft | 当前控件位于布局左侧 |
android:layout_alignParentRight | 当前控件位于布局右侧 |
android:layout_alignTop | 当前控件上边界与某控件上边界对齐 |
android:layout_alignBottom | 当前控件下边界与某控件下边界对齐 |
android:layout_alignLeft | 当前控件左边界与某控件左边界对齐 |
android:layout_alignRight | 当前控件右边界与某控件右边界对齐 |
android:layout_marginTop | 当前控件上边界与某控件的距离 |
android:layout_marginBottom | 当前控件下边界与某控件的距离 |
android:layout_marginLeft | 当前控件左边界与某控件的距离 |
android:layout_marginRight | 当前控件右边界与某控件的距离 |
布局控件属性 | 功能描述 |
---|---|
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:paddingBottom="20sp"
>
<!--
android:paddingBottom="20sp"父窗体距底边距20sp
-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn1"
android:text="按钮1"
android:textSize="20dp"
android:layout_alignParentBottom="true"
/>
<!--
android:layout_alignParentBottom属性共2个值,如果位true代表该控件的底部与其父控件的底部对齐
-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn2"
android:text="按钮2"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="200sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn3"
android:text="按钮3"
android:textSize="20dp"
android:layout_toRightOf="@id/btn2"
android:layout_alignBottom="@id/btn2"
android:layout_marginBottom="100sp"
/>
<!--
@id/btn2代表引用id等于btn2的控件
-->
</RelativeLayout>
效果:
(4) 帧布局(FrameLayout)
该布局为每个加入其中的控件创建一个空白区域,称为一帧,每个控件占据一帧,采用帧布局方式设计界面时,所有控件都默认显示在屏幕左上角,并按照先后放入的顺序重叠摆放,先放入的控件显示在最底层,后放入的控件显示在最顶层。帧布局适用于图层设计。
布局属性 | 功能 |
---|---|
android:foreground | 设置帧布局容器的前景图像 |
android:foregroundGravity | 设置前景图像显示位置 |
注意:android:foreground设置帧布局容器的前景图片,始终在所有控件之上
android:foregroundGravity设置前景图像显示位置
@mipmap/ic_launcher代表引入mipmap文件下的ic_launcher.png图片
例子
<?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="@mipmap/ic_launcher"
android:foregroundGravity="left"
>
<Button
android:layout_height="400dp"
android:layout_width="400dp"
android:text="按钮1"
android:textSize="20dp"
></Button>
<Button
android:layout_height="200dp"
android:layout_width="200dp"
android:text="按钮1"
android:textSize="20dp"
></Button>
</FrameLayout>
效果
常用的就是这四种