LinearLayout和RelativeLayout

 

LinearLayout

一、所有控件水平或竖直排列

andriod:orientation="vertical"
android:orientation="horizontal"
决定控件水平还是竖直排列

 

二、layout_gravity和android:gravity的区别

android:layout_gravity用于指定控件在布局中的对齐方式
android:gravity用于指定文字在控件中的对齐方式

  注意:只有当LinearLayout的布局方式是horizontal时,只有垂直方向上的布局方式才会生效,因为此时水平方向上的长度是不固定的。没添加一个控件,水平方向上的长度都会改变。此时android:layout_gravity的值可以为top,center_vertial,bottom

 

三、layout_weight

LinearLayout中的一个重要属性,android:layout_weight.这个属性可以按照比例指定控件大小,在手机屏幕的适配性方面起到非常重要的作用
android:layout_width="0dp"
andorid:layout_weight="1"
由于使用了weight属性,宽度就不应该再由android:layout_width来决定,这里指定成0是一种比较规范的写法

LinearLayout

android:orientation="horizontal"

Bottom1

andorid:layout_weight="1"

Bottom2

andorid:layout_weight="1"

  此时两个bottom平分屏幕宽度,因为LinearLayout系统会把LinearLayout下所有控件的layout_weight值相加,得到一个总值,每个控件在horizontal或者vertical上的比例就是该控件的layout_weight值与刚才求出的总值之商。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="horizontal" >
 6 
 7     <EditText
 8         android:id="@+id/editText1"
 9         android:layout_width="0dp"
10         android:layout_height="wrap_content"
11         android:layout_weight="1"
12         android:ems="10"
13         android:hint="textPersonName" >
14  
15     </EditText>
16 
17     <Button
18         android:id="@+id/button1"
19         android:layout_width="0dp"
20         android:layout_height="wrap_content"
21          android:layout_weight="1"
22         android:text="Button" />
23 
24 </LinearLayout>

因此如果想让Bottom1占屏幕宽度的3/5,Bottom2占屏幕宽度的2/5,让Bottom1的layout_weight为"3",bottom2的layout_weight值为"2"。

  如果仅指定了button1的layout_weight值为1,将button2的宽度改回wrap_content。这表示button2的宽度仍按照wrap_content来计算,而Button1会占满屏幕所有的剩余空间。使用这种方式边写的界面,不仅在各种屏幕的适配方面会非常好,而且看起来也更加舒服。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="horizontal" >
 6 
 7     <EditText
 8         android:id="@+id/editText1"
 9         android:layout_width="0dp"
10         android:layout_height="wrap_content"
11         android:layout_weight="1"
12         android:ems="10"
13         android:inputType="textPersonName" >
14  
15     </EditText>
16 
17     <Button
18         android:id="@+id/button1"
19         android:layout_width="wrap_content"
20         android:layout_height="wrap_content"
21          
22         android:text="Button" />
23 
24 </LinearLayout>

RelativeLayout

 

一、相对于父布局的RelativeLayout布局

它可以通过布局使控件出现在任何地方,比LinearLayout更自由一些

layout_alignPrentLeft,layout_alignParentRight,layout_alignParentBottom,layout_alignParentTop,layout_centerInParent。可以通过组合确定屏幕中的五个位置

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <Button
 7         android:id="@+id/button1"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:layout_centerInParent="true"
11         android:text="1" />
12 
13     <Button
14         android:id="@+id/button2"
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content"
17         android:layout_alignParentLeft="true"
18         android:layout_alignParentTop="true"
19         android:text="2" />
20 
21     <Button
22         android:id="@+id/button3"
23         android:layout_width="wrap_content"
24         android:layout_height="wrap_content"
25         android:layout_alignParentRight="true"
26         android:layout_alignParentTop="true"
27         android:text="3" />
28 
29     <Button
30         android:id="@+id/button4"
31         android:layout_width="wrap_content"
32         android:layout_height="wrap_content"
33         android:layout_alignParentRight="true"
34         android:layout_alignParentBottom="true"
35         android:text="4" />
36 
37     <Button
38         android:id="@+id/button5"
39         android:layout_width="wrap_content"
40         android:layout_height="wrap_content"
41         android:layout_alignParentLeft="true"
42         android:layout_alignParentBottom="true"
43         android:text="5" />
44 
45 </RelativeLayout>

 

 

二、相对于控件的RelativeLayout布局

 以上的布局是相对于父布局进行定位的,那控件可不可以相对于控件进行定位呢,当然是可以的,通过layout_above,layout_below,layout_toLeftOf,layout_toRightOf

layout_above="@id/xxx"   表示位于某控件的上一行,具体在那个列不确定

layout_below="@id/xxx"   表示位于某控件的下一行,具体在那个列不确定

layout_toLeftOf="@id/xxx"   表示位于某控件相邻的左边,具体在那个行不确定

layout_toRightOf="@id/xxx"   表示位于某控件相邻的右边,具体在那个行不确定

请看以下代码

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <Button
 7         android:id="@+id/button1"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:layout_centerInParent="true"
11         android:text="1" />
12 
13     <Button
14         android:id="@+id/button2"
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content"
17         android:layout_above="@id/button1"
18         android:layout_toLeftOf="@id/button1"
19         android:text="2" />
20 
21     <Button
22         android:id="@+id/button3"
23         android:layout_width="wrap_content"
24         android:layout_height="wrap_content"
25         android:layout_above="@id/button1"
26         android:layout_toRightOf="@id/button1"
27         android:text="3" />
28 
29     <Button
30         android:id="@+id/button4"
31         android:layout_width="wrap_content"
32         android:layout_height="wrap_content"
33         android:layout_below="@id/button1"
34         android:layout_toLeftOf="@id/button1"
35         android:text="4" />
36 
37     <Button
38         android:id="@+id/button5"
39         android:layout_width="wrap_content"
40         android:layout_height="wrap_content"
41         android:layout_below="@id/button1"
42         android:layout_toRightOf="@id/button1"
43         android:text="5" />
44 
45 </RelativeLayout>

 

三、相对于控件的RelativeLayout布局

RelativeLayout中还有一另外一组基于以上相对控件定位的一组属性,layout_alignLeft,layout_alignRight,layout_alignTop,layout_alignBottom。

layout_alignLeft="@id/xxx"    此控件的左边缘和某控件的左边缘对齐

layout_alignRight="@id/xxx"   此控件的右边缘和某控件的右边缘对齐

layout_alignTop="@id/xxx"    此控件的上边缘和某控件的上边缘对齐

layout_alignBottom="@id/xxx"    此控件的下边缘和某控件的下边缘对齐

以上效果也可用这些属性实现

 

posted @ 2017-06-25 22:17  雁湖初平  阅读(171)  评论(0编辑  收藏  举报