Android五种布局管理器之『RelativeLayout』

来源:互联网 发布:http协议默认端口 编辑:程序博客网 时间:2024/05/24 05:59

转载http://www.sunchis.com/html/android/devolop/2011/0424/317.html

在相对布局(RelativeLayout)中,子控件的位置是相对兄弟控件或父容器而决定的。出于性能考虑,在设计相对布局时,要按照控件之间的依赖关系排列。如View A的位置相当于View B来决定,则需要保证布局文件中View B在View A的前面。

在进行相对布局时,用到的布局属性有很多,首先来看属性值为true或false的属性,见下表:

相对布局中取值为true或false的属性列表

属性名称属性说明android:layout_centerHorizontal当前控件位于父控件的横向中间位置android:layout_centerVertical当前控件位于父控件的纵向中间位置android:layout_centerInParent当前控件位于父控件的中央位置android:layout_alignParentBottom当前控件底端与父控件底端对齐android:layout_alignParentLeft当前控件左侧与父控件左侧对齐android:layout_alignParentRight当前控件右侧与父控件右侧对齐android:layout_alignParentTop当前控件顶端与父控件顶端对齐android:layout_alignWithParentIfMissing参照控件不存在或不可见时参照父控件

接下来再来看看属性值为其他控件id的属性,见下表:

相对布局中取值为其他控件id的属性及其说明

属性名称属性说明android:layout_toRightOf使当前控件位于给出id控件的右侧android:layout_toLeftOf使当前控件位于给出id控件的左侧android:layout_above使当前控件位于给出id控件的上方android:layout_below使当前控件位于给出id控件的下方android:layout_alignTop使当前控件的上边界与给出id控件的上边界对齐android:layout_alignBottom使当前控件的下边界与给出id控件的下边界对齐android:layout_alignLeft使当前控件的左边界与给出id控件的左边界对齐android:layout_alignRight使当前控件的右边界与给出id控件的右边界对齐

最后介绍的是属性值以像素为单位的属性及说明,见下表:

相对布局中取值为像素的属性及说明

属性名称属性说明android:layout_marginLeft当前控件左侧的留白android:layout_marginRight当前控件右侧的留白android:layout_marginTop当前控件上方的留白android:layout_marginBottom当前控件下方的留白android:layout_margin当前控件上下左右四个方向的留白

需要注意的是:在进行相对布局时要避免出现循环依赖,例如设置相对布局在父容器中的排列方式为WRAP_CONTENT,就不能再将相对布局的子控件设置为ALIGN_PARENT_BOTTOM。因为这样会造成子控件和父控件相互依赖和参照的错误。

下面就先来看看相对布局的效果图:

点击放大图片

其中Main.xml代码如下:

view plain   copy
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <!-- 声明一个相对布局 --> 
  3. <RelativeLayout  
  4.     android:id="@+id/RelativeLayout01"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     android:background="#ffffff"  
  8.     xmlns:android="http://schemas.android.com/apk/res/android"> 
  9.      
  10.     <!-- 第一个ImageView控件,位于父控件的中央位置 --> 
  11.     <ImageView  
  12.         android:id="@+id/ImageView01" 
  13.         android:background="@drawable/center" 
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content" 
  16.         android:layout_centerInParent="true"> 
  17.     </ImageView> 
  18.      
  19.     <!-- 第二个ImageView控件,位于第一个控件的右侧 --> 
  20.     <ImageView  
  21.         android:id="@+id/ImageView02"  
  22.         android:background="@drawable/down" 
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content" 
  25.         android:layout_toRightOf="@id/ImageView01"      
  26.         android:layout_alignTop="@id/ImageView01"> 
  27.     </ImageView> 
  28.      
  29.     <!-- 第三个ImageView控件,位于第一个控件的上方 --> 
  30.     <ImageView  
  31.         android:id="@+id/ImageView03"  
  32.         android:background="@drawable/up" 
  33.         android:layout_width="wrap_content"  
  34.         android:layout_height="wrap_content" 
  35.         android:layout_above="@id/ImageView01"      
  36.         android:layout_alignLeft="@id/ImageView01"> 
  37.     </ImageView>   
  38. </RelativeLayout> 

Activity代码为:

view plain   copy
  1. package com.sunchis; 
  2.  
  3. import android.app.Activity; 
  4. import android.os.Bundle; 
  5.  
  6. public class Android extends Activity {  
  7.     @Override 
  8.     public void onCreate(Bundle savedInstanceState) { 
  9.         super.onCreate(savedInstanceState); 
  10.         setContentView(R.layout.main);          //设置屏幕 
  11.     } 

原创粉丝点击