布局最简单的来说就是摆放控件的位置,最终形成用户所看到的Android界面。在Android中常用的布局方式有xml文件布局、代码布局;xml文件布局是最常用的,在代码中布局控件会显得比较繁琐,代码量重。
一、布局种类
1、Layout常用布局:LinearLayout(线性布局)、RelativeLayout(相对布局)
2、View布局:ListView(单列多行列表)、GridView(多行多列列表)
二、LinearLayout线性布局
线性布局就是控件在界面上可以按顺序的排列,配合orientation属性可以从左到右排列,也可以从上到下排列。
1. orientation属性:
(1)horizontal默认从左到右排列
(2)vertical从上到下排列
2. horizontal默认
<?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">
<Button
android:id="@+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认" />
<Button
android:id="@+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消" />
</LinearLayout>
3. 设置为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="vertical">
<Button
android:id="@+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认" />
<Button
android:id="@+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消" />
</LinearLayout>
三、RelativeLayout相对布局
相对布局是以控件与控件的相对位置来展示。相比于线性布局,相对布局只需要一个RelativeLayout即可完成整个Activity显示。这样android对于布局样式xml的解析会更快捷,性能更优,但是排版也会更繁琐,各有各的优点。
<?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">
<Button
android:id="@+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认" />
<Button
android:id="@+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消" />
</RelativeLayout>
运行效果可以看到只展示了一个取消按钮,因为是相对布局2个控件都重叠了,也就是确认按钮被后面的取消按钮覆盖了。所以相对布局需要以控件的相对位置来摆放,接下来修改代码如下:
<?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">
<Button
android:id="@+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认" />
<Button
android:id="@+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button_one"
android:text="取消" />
</RelativeLayout>
在第二个Button设置了一个属性,android:layout_below=”@+id/button_one”表示放在id=button_one的元素下方,运行后如下:
其他属性:
1.RelativeLayout属性
(1)android:layout_below:放在元素的下方
(2)android:layout_above:放在元素的上方
(3)android:layout_toLeftOf:放在元素的左边
(4)android:layout_toRightOf:放在元素的右边
(5)android:layout_alignLeft:与元素左侧对齐
(6)android:layout_alignRight:与元素右侧对齐
(7)android:layout_alignTop:与元素上侧对齐
(8)android:layout_alignBottom:与元素下侧对齐
2. 基准线
(1)概念:为了保证印刷字母的整齐而划定的线。
(2)使用方法同上,控件内容与控件内容的基准线对齐;当字体大小相同时,基准线对齐的使用意义并不大,当字体大小不相同使用。
使用:android:layout_alignBaseline="@+id/button_one"
3.与父控件边缘对齐
(1)android:layout_alignParentLeft:与父元素左侧对齐
(2)android:layout_alignParentRight:与父元素右侧对齐
(3)android:layout_alignParentTop:与父元素上侧对齐
(4)android:layout_alignParentButtom:与父元素下侧对齐
使用:android:layout_alignParentLeft=”true”
4.对齐至父控件的中央
(1)android:layout_centerInParent:对齐父元素正中央,水平垂直都是正中
(2)android:layout_centerHorizontal:对齐父元素水平居中
(3)android:layout_centerVertical:对齐父元素垂直居中
使用:android: layout_centerInParent =”true”
5. 新加属性(Android4.2以上版本使用)
(1)android:layout_alignStart:与参照元素的开始位置对齐
(2)android:layout_alignEnd:与参照元素的结束位置对齐
(3)android:layout_alignParentStart:与父元素的开始位置对齐
(4)android:layout_alignParentEnd:与父元素的结束位置对齐