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
- <?xml version="1.0" encoding="utf-8"?>
- <!-- 声明一个相对布局 -->
- <RelativeLayout
- android:id="@+id/RelativeLayout01"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#ffffff"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <!-- 第一个ImageView控件,位于父控件的中央位置 -->
- <ImageView
- android:id="@+id/ImageView01"
- android:background="@drawable/center"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true">
- </ImageView>
- <!-- 第二个ImageView控件,位于第一个控件的右侧 -->
- <ImageView
- android:id="@+id/ImageView02"
- android:background="@drawable/down"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_toRightOf="@id/ImageView01"
- android:layout_alignTop="@id/ImageView01">
- </ImageView>
- <!-- 第三个ImageView控件,位于第一个控件的上方 -->
- <ImageView
- android:id="@+id/ImageView03"
- android:background="@drawable/up"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_above="@id/ImageView01"
- android:layout_alignLeft="@id/ImageView01">
- </ImageView>
- </RelativeLayout>
Activity代码为:
view plain copy
- package com.sunchis;
- import android.app.Activity;
- import android.os.Bundle;
- public class Android extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main); //设置屏幕
- }
- }
- Android五种布局管理器之『RelativeLayout』
- Android五种布局管理器之『RelativeLayout』
- Android五种布局管理器之『RelativeLayout』
- Android五种布局管理器之『FrameLayout』
- Android五种布局管理器之『LinearLayout』
- Android五种布局管理器之『AbsoluteLayout』
- Android五种布局管理器之『FrameLayout』
- Android五种布局管理器之『TableLayout』
- Android五种布局管理器之『LinearLayout』
- Android五种布局管理器之『FrameLayout』
- Android五种布局管理器之『AbsoluteLayout』
- Android五种布局管理器之『TableLayout』
- android五大布局之-RelativeLayout
- Android相对布局管理器RelativeLayout
- 一步一步学android之布局管理器——RelativeLayout
- android五大布局之RelativeLayout(相对布局)
- android四种布局管理器 FrameLayout LinearLayout TableLayout RelativeLayout
- 五大布局之RelativeLayout
- Android组成原理之六大金刚
- Android五种布局管理器之『LinearLayout』
- C#SendMessage用法
- 编码 GBK 的不可映射字符
- Android极速启动模拟器
- Android五种布局管理器之『RelativeLayout』
- Java的安装与配置
- hdu 1709 (母函数,有些特殊)
- Java RandomAccessFile的使用
- 关于spring与hibernate整合后的事务
- 创业12年 最野蛮的屌丝创业者
- Android五种布局管理器之『FrameLayout』
- linux unzip程序简介
- Android五种布局管理器之『AbsoluteLayout』