RelativeLayout相对布局
于LinearLayout相比,RelativeLayout 更加随意,可以通过相对定位的方式让控件出现在布局的任何位置,
正因为如此,RelativeLayout的属相非常多
<RelativeLayout
xmlns:
android
=
"http://schemas.android.com/apk/res/android"
android :layout_width= "match_parent"
android :layout_height= "match_parent" >
<Button
android :id= "@+id/button1"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_alignParentTop= "true"
android :layout_alignParentLeft= "true"
android :text= "Button1" />
<Button
android :id= "@+id/button2"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_alignParentRight= "true"
android :layout_alignParentTop= "true"
android :text= "Button2" />
<Button
android :id= "@+id/button3"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_centerInParent= "true"
android :text= "Button3" />
<Button
android :id= "@+id/button4"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_alignParentBottom= "true"
android :layout_alignParentLeft= "true"
android :text= "Button4" />
<Button
android :id= "@+id/button5"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_alignParentBottom= "true"
android :layout_alignParentRight= "true"
android :text= "Button5" />
</RelativeLayout>
android :layout_width= "match_parent"
android :layout_height= "match_parent" >
<Button
android :id= "@+id/button1"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_alignParentTop= "true"
android :layout_alignParentLeft= "true"
android :text= "Button1" />
<Button
android :id= "@+id/button2"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_alignParentRight= "true"
android :layout_alignParentTop= "true"
android :text= "Button2" />
<Button
android :id= "@+id/button3"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_centerInParent= "true"
android :text= "Button3" />
<Button
android :id= "@+id/button4"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_alignParentBottom= "true"
android :layout_alignParentLeft= "true"
android :text= "Button4" />
<Button
android :id= "@+id/button5"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_alignParentBottom= "true"
android :layout_alignParentRight= "true"
android :text= "Button5" />
</RelativeLayout>
//相对于父布局进行定位
//Button1于父布局的左上角对齐
//Button2于父布局的右上角对齐
//Button3于父布局的居中显示
//Button4于父布局的左下角对齐
//Button5于父布局的右下角对齐
也可以控件相对于控件进行定位
<RelativeLayout
xmlns:
android
=
"http://schemas.android.com/apk/res/android"
android :layout_width= "match_parent"
android :layout_height= "match_parent" >
<Button
android :id= "@+id/button3"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_centerInParent= "true"
android :text= "Button3" />
<Button
android :id= "@+id/button1"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_above= "@+id/button3"
android :layout_toLeftOf= "@+id/button3"
android :text= "Button1" />
<Button
android :id= "@+id/button2"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_above= "@+id/button3"
android :layout_toRightOf= "@+id/button3"
android :text= "Button2" />
<Button
android :id= "@+id/button4"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_below= "@+id/button3"
android :layout_toLeftOf= "@+id/button3"
android :text= "Button4" />
<Button
android :id= "@+id/button5"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_below= "@+id/button3"
android :layout_toRightOf= "@+id/button3"
android :text= "Button5" />
</RelativeLayout>
android :layout_width= "match_parent"
android :layout_height= "match_parent" >
<Button
android :id= "@+id/button3"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_centerInParent= "true"
android :text= "Button3" />
<Button
android :id= "@+id/button1"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_above= "@+id/button3"
android :layout_toLeftOf= "@+id/button3"
android :text= "Button1" />
<Button
android :id= "@+id/button2"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_above= "@+id/button3"
android :layout_toRightOf= "@+id/button3"
android :text= "Button2" />
<Button
android :id= "@+id/button4"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_below= "@+id/button3"
android :layout_toLeftOf= "@+id/button3"
android :text= "Button4" />
<Button
android :id= "@+id/button5"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_below= "@+id/button3"
android :layout_toRightOf= "@+id/button3"
android :text= "Button5" />
</RelativeLayout>
//注意:当一个控件去引用另一个控件的id时,该控件一定要定义在引用控件的后面,不然会出现找不到id的情况
RelativeLayout 还有另外一组相对于控件进行定位的属性
//android:layout_alignLeft 表示让一个控件的左边缘和另一个控件的左边缘对齐
//android:layout_alignRight 表示让一个控件的右边缘和另一个控件的右边缘对齐
//android:layout_alignTop 表示让一个控件的上边缘和另一个控件的上边缘对齐
//android:layout_alignButtom 表示让一个控件的下边缘和另一个控件的下边缘对齐