Android布局的介绍

概念

布局:界面控件摆放位置的确定。

所有的布局都继承ViewGroup

在Android4.0以前一共有五种布局:

LinearLayout(线性布局)

RelativeLayout(相对布局)

TableLayout(表格布局)

FrameLayout(帧布局)

AbsoulteLayout(绝对布局)

在Android4.0以后新增GridLayout(网格布局)网格布局的经典实用场景就是计算器界面

注意:在2.2的操作系统中AbsoulteLayout(绝对布局) 而且TableLayout也逐渐少用

目前推荐使用LinearLayout  RrelativeLayout GridLayout

View共有的属性(各种布局和控件都具有的属性,重点)

android:id 设置控件的id

android:layout_width  控件的布局宽度

android:layout_height 控件的布局高度

fill_parent match_parent 与父控件一致(2.2以后推荐使用match_parent)

wrap_content  包裹内容

android:background    设置布局或者控件的背景颜色或者图片

android:layout_margin 控件与控件之间的间距(外边距)

android:padding      控件内容到控件边缘的距离(内边距)

android:onClick      为控件的单击事件绑定监听器

android:visibility    设置该控件是否可见(3个值)

LinearLayout(线性布局):控制其中的控件或者组件水平或者垂直排列的布局

android:orientation  定义布局内控件或者组件的排列方式  可选项:horizontal vertical 默认是水平方向

android:gravity:View中内容的对齐方式

如果该属性定义在布局节点中或者是定义在ViewGroup中则布局中所有子控件的位置都受到该属性的控制

如果该属性定义在TextView Button等非容器类视图控件中时 用来控制这些控件中内容的位置

android:layout_gravity:设置控件相对于父控件的对齐方式

如果LinearLayout的android:orientation="vertical"的时候 layout_gravity=""这里设置为横向的时候才会生效 比如:left right center_horizontal

如果LinearLayout的android:orientation="horizontal"的时候 layout_gravity=""这里设置为纵向的时候才会生效 比如:top bottom center_vertical

LinearLayout指明了方向后 所有子控件按顺序依次排列

weight:LinearLayout特有属性,指的是对屏幕剩余空间的比例分配

android:layout_weight:子控件在LinearLayout中所占的权重

RelativeLayout(相对布局):按照控件之间相对位置来进行的布局

A.与父布局之间的对齐关系

1.android:layout_centerHorizontal    该控件是否位于父控件的水平中心位置

2.android:layout_centerVertical      该控件是否位于父控件的垂直中心位置

3.android:layout_centerInParent      该控件是否位于父控件的中心位置

4.android:layout_alignParentRight    该控件是否与父控件右对齐

5.android:layout_alignParentLeft      该控件是否与父控件左对齐

6.android:layout_alignParentTop      该控件是否与父控件顶对齐

7.android:layout_alignParentBottom    该控件是否与父控件底对齐

B.与兄弟控件的位置关系 该组属性的值是另一个控件的id

1.android:layout_toRightOf            该控件在哪个控件的右边

2.android:layout_toLeftOf            该控件在哪个控件的左边

3.android:layout_above                该控件在哪个控件的上边

4.android:layout_below                该控件在哪个控件的下边

C.与兄弟控件的对齐关系

1.android:layout_alignRight        该控件与哪个控件右对齐

2.android:layout_alignLeft        该控件与哪个控件左对齐

3.android:layout_alignTop        该控件与哪个控件顶对齐

4.android:layout_alignBottom        该控件与哪个控件底对齐

5.android:layout_alignStart       该控件的起始端与哪个控件对齐 4.2以后新出的属性

6.android:layout_alignEnd       该控件的末尾段与哪个控件对齐 4.2以后新出的属性

你可能感兴趣的:(Android布局的介绍)