Android应用开发学习之相对布局

作者:刘昊昱 

博客:http://blog.csdn.net/liuhaoyutz

 

相对布局RelativeLayout是指按照组件之间的相对位置进行布局,如一个组件在另一个组件的左边、右边、上边或下边等。

RelativeLayout常用的XML属性有:

android:gravity

用于设置布局管理器中各子组件的对齐方式。

android:ignoreGravity

用于指定哪个组件不受gravity属性的影响。

在相对布局管理器中,如果只有上面两个属性是远远不够的,为了更好地控制该布局管理器中的组件,RelativeLayout提供了一个内部类:RelativeLayout.LayoutParams,通过该类提供的大量XML属性,可以很好的控制RelativeLayout中的各组件的布局。

下表是Android官方网站上给出的RelativeLayout.LayoutParams的XML属性

Attribute Name

Description

android:layout_above

Positions the bottom edge of this view above the given anchor view ID. 

android:layout_alignBaseline

Positions the baseline of this view on the baseline of the given anchor view ID. 

android:layout_alignBottom

Makes the bottom edge of this view match the bottom edge of the given anchor view ID. 

android:layout_alignEnd

Makes the end edge of this view match the end edge of the given anchor view ID. 

android:layout_alignLeft

Makes the left edge of this view match the left edge of the given anchor view ID. 

android:layout_alignParentBottom

If true, makes the bottom edge of this view match the bottom edge of the parent. 

android:layout_alignParentEnd

If true, makes the end edge of this view match the end edge of the parent. 

android:layout_alignParentLeft

If true, makes the left edge of this view match the left edge of the parent. 

android:layout_alignParentRight

If true, makes the right edge of this view match the right edge of the parent. 

android:layout_alignParentStart

If true, makes the start edge of this view match the start edge of the parent. 

android:layout_alignParentTop

If true, makes the top edge of this view match the top edge of the parent. 

android:layout_alignRight

Makes the right edge of this view match the right edge of the given anchor view ID. 

android:layout_alignStart

Makes the start edge of this view match the start edge of the given anchor view ID. 

android:layout_alignTop

Makes the top edge of this view match the top edge of the given anchor view ID. 

android:layout_alignWithParentIfMissing

If set to true, the parent will be used as the anchor when the anchor cannot be be found for layout_toLeftOf, layout_toRightOf, etc. 

android:layout_below

Positions the top edge of this view below the given anchor view ID. 

android:layout_centerHorizontal

If true, centers this child horizontally within its parent. 

android:layout_centerInParent

If true, centers this child horizontally and vertically within its parent. 

android:layout_centerVertical

If true, centers this child vertically within its parent. 

android:layout_toEndOf

Positions the start edge of this view to the end of the given anchor view ID. 

android:layout_toLeftOf

Positions the right edge of this view to the left of the given anchor view ID. 

android:layout_toRightOf

Positions the left edge of this view to the right of the given anchor view ID. 

android:layout_toStartOf

Positions the end edge of this view to the start of the given anchor view ID. 

下面我们来看一个例子,该程序运行效果如图所示:

我们来看该程序的主布局文件main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 	android:id="@+id/relativeLayout1" 
 	android:layout_width="fill_parent" 
	android:layout_height="fill_parent" 
	xmlns:android="http://schemas.android.com/apk/res/android"
 >
 	<!-- 添加一个居中显示的文本视图textView1 -->
    <TextView android:text="您正在学习RelativeLayout的用法 ,是否开始动手练习?"
     android:id="@+id/textView1"
     android:textSize="24px"
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:layout_centerInParent="true"
	/>
	<!-- 添加一个在button2左侧显示的按钮button1 -->
    <Button
     android:text="开始练习" 
     android:id="@+id/button1" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_below="@+id/textView1" 
     android:layout_toLeftOf="@+id/button2" 
     />
     <!-- 添加一个按钮button2,该按钮与textView1的右边界对齐 -->
    <Button 
     android:text="不练习" 
     android:id="@+id/button2" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_alignRight="@+id/textView1"
     android:layout_below="@+id/textView1" 
	/>
 </RelativeLayout>


通过这个例子,我们可以理解相对布局控件RelativeLayout的用法了。

 

posted on 2013-07-30 19:20  you Richer  阅读(226)  评论(0编辑  收藏  举报