Android中的UI元素都是使用View和ViewGroup对象创建的,View是一个可以显示在屏幕界面与用户进行交互的对象,ViewGroup是一个包含多个View和ViewGroup的容器,用来定义UI布局。
为了将由View对象创建的UI元素合理的,美观的布置在界面上,我们可以采用以下的五大布局:
一:LinearLayout--线性布局
1:作用
使用线性布局之后,存在于该布局之内的控件将一个挨着一个的排列起来。排列方式可以水平,也可以垂直,具体的设置可以由下列属性指定:
2:属性
(1) android:layout_width和android:layout_height
这两个属性为必须有的属性,分别标志元素的宽度与高度。有三个值
◆match_parent:充满父级元素,后来版本采用的方式,对于顶级元素而言,父级元素为整个手机屏幕
◆fill_parent:充满父级元素,较早使用的一种方式
◆wrap_content:自适应,只显示足够的空间
(2)android:orientation
指明存在于其中的元素的排列方式。有两个取值
◆vertical:存在于其中的元素,单独占据一行,垂直排列
◆horizontal:存在于其中的元素,单独占据一列,水平排列
(3)android:gravity
用于设置View内部元素的排列方式,取值可以包括:left(左对齐),right(右对齐),top(上对齐),bottom(下对齐),center(居中对齐),center_horizontal(水平居中),center_vertical(垂直居中)等,还可有组合使用,如right|top(右上)。
比如:一个按钮可以设置其上文字在按钮上的位置、一个布局可以设置存在于其中控件的位置等
(4)android:layout_gravity
用于设置该view在其父view中的对齐方式,取值包括:left(左对齐),right(右对齐),top(上对齐),bottom(下对齐),center(居中对齐),center_horizontal(水平居中),center_vertical(垂直居中)等,还可有组合使用,如right|top(右上)。
比如:一个按钮可以设置自己在其父级View上的位置
(5)android:layout_weight
权重,根据设定值实现通过百分比来进行布局的目的。取值建议设定为整数值。此属性仅在LinearLayout布局下存在。
当android:layout_width和android:layout_height设定为match_parent时,对比中的判断为反相关,取值越大,比重越小
当android:layout_width和android:layout_height设定为wrap_content时,对比中的判断为正相关,取值越小,比重越大
3:应用
◆设计布局:利用LinearLayout实现屏幕上四个角落及中间分别存在一个按钮
◆ 分析:这里用到了布局之间的嵌套
(1)我们可以这样来看,在主体布局添加三个水平布局,分别设置权重为1,使其平分屏幕。
(2)第一个水平布局上添加一个按钮,默认位置存在于左上角位置
(3)第一个水平布局嵌套添加一个垂直布局,让其宽度充满父级元素,添加按钮,设置属性android:layout_gravity为right,是空间以独占一行靠右的形式存在
(4)第二个水平布局设置属性android:gravity为center,让存在于其中的控件居中显示,添加按钮之后就在中间了
(5)第三个水平布局添加按钮,设置属性android:layout_gravity为bottom,是空间以独占一列靠下的形式存在
(6)第三个水平布局嵌套添加一个垂直布局,设置属性android:gravity为right|bottom,让存在于其中的控件靠右居下显示
◆具体实现:
<LinearLayout 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" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="Button" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" > <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" > <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:text="Button" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:gravity="right|bottom" android:orientation="vertical" > <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout> </LinearLayout> </LinearLayout> |
◆结果:
二:RelativeLayout--相对布局
1:作用
存在于这个布局之中控件的位置,是按相互之间的相对位置来确定的
2:属性
(1)线性布局的前4个属性,在这里依然存在
(2)相对于父级控件位置的属性
◆ android:layout_alignParentLeft="true":存在父级控件的左侧
◆android:layout_alignParentTop="true":存在附近控件的顶部
◆ android:layout_alignParentBottom="true":存在父级控件的下部
◆ android:layout_alignParentRight="true":存在父级控件的右侧
(3)相对于其他控件的位置属性
◆ android:layout_toLeftOf="true":存在于其他控件的左侧
◆ android:layout_toRightOf="true":存在于其他控件的右侧
◆android:layout_above="true":存在于其他控件的上面
◆android:layout_below="true":存在于其他控件的下面
3:应用
◆布局设计:运用RelativeLayout布局方式,实现四个按钮的如结果所示的排布
◆分析:
(1)添加按钮1 ,默认存在于屏幕的左上角
(2)添加按钮2 ,设置属性android:layout_toRightOf="@+id/button1",让它存在于第一个按钮的右侧
(3)添加按钮3 ,按钮3 存在于2 的下面,1的右面,因此属性值应为android:layout_below="@+id/button2"和android:layout_toRightOf="@+id/button1"
(4)添加按钮4 ,按钮4存在于1 的下面,设置属性为 android:layout_below="@+id/button1"
◆具体实现
<RelativeLayout 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" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:text="Button1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/button1" android:text="Button2" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button2" android:layout_toRightOf="@+id/button1" android:text="Button3" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:text="Button4" /> </RelativeLayout> |
◆结果
三:TableLayout--表格布局
1:作用
这种布局以行和列的形式管理控件。
2:使用
每一行为一个TableRow对象,然后再TableRow对象中添加控件,以行的形式排列一行。
3:应用
◆布局设计:设计出如计算器般的按钮排列布局
◆分析:
(1)三行三列,需要三个TableRow,9个按钮
(2)每个TableRow,依次添加3个按钮,排列即可
◆具体实现
<TableLayout 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" tools:context=".MainActivity" > <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3" /> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4" /> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5" /> <Button android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="6" /> </TableRow> <TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/button7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="7" /> <Button android:id="@+id/button8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="8" /> <Button android:id="@+id/button9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="9" /> </TableRow> </TableLayout> |
◆结果
四:FrameLayout--帧布局
1:作用
帧布局中的每个组件都是一个画面,且存在其中的控件以罗列的方式显示,只有最上面的那个控件消失之后,后面的页面才会依次显示。
2:实现
<FrameLayout 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" tools:context=".MainActivity" > <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:id="@+id/progressBar2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ProgressBar android:id="@+id/progressBar3" style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </FrameLayout> |
3:样式:可见控件“起层”了
五:AbsoluteLayout--绝对布局
顾名思义,它是根据具体的坐标位置来绝对的定义控件的位置,这样针对不同分辨率的手机来说无疑是一个巨大的麻烦,因此官方文档中明确建议不适用绝对布局。所有的绝对布局均可以靠其他布局组合完成,因此不建议使用。
转载于:https://blog.51cto.com/cinderella7/1280142