android 相对布局

来源:互联网 发布:linux内核熵池 编辑:程序博客网 时间:2024/05/01 02:41

今天学习了RelativeLayout,线性布局不是一直能满足我们的要求,RelativeLayout允许子元素指定她们相对与其他元素或父元素的位置(通过id指定)。实例如下

-------------main.xml----------------

使用了RelativeLayout定义了布局方式

<?xml version="1.0" encoding="utf-8"?><!--  android:background 定义了控件的背景色   android:layout_*:可以用来控制控件的位置    android:layout_above:位于指定控件的上面  android:layout_blow:位于指定控件的下面  android:layout_toLeftOf:位于指定控件的左面  android:layout_toRightOf:位于指定控件的右面    android:layout_alignBottom:本控件的下边缘和指定控件下边缘对齐  android:layout_alignTop:本控件的上边缘和指定控件的上边缘对齐  android:layout_alignLeft:本控件的左边缘和指定控件的左边缘对齐  android:layout_alignRight:本控件的右边缘和指定控件的右边缘对齐    android:layout_alignParentButtom:如果值为true则指定控件和父控件的下边缘对齐  android:layout_alignParentTop:如果值为true则指定控件和父控件的上边缘对齐  android:layout_alignParentRight:如果值为true则指定控件和父控件的右边缘对齐  android:layout_alignParentLeft:如果值为true则指定控件和父控件的左边缘对齐    android:layout_centerHorizontal:如果值为true则指定控件再起父控件中水平居中  android:layout_centerVertical:如果值为true则指定控件再起父控件中垂直居中  android:layout_centerInParent:如果值为true则指定控件再起父控件中完全居中   --><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <TextView        android:id="@+id/label"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="Type here:"/>    <EditText        android:id="@+id/entry"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="@android:drawable/editbox_background"        android:layout_below="@id/label"/>    <Button        android:id="@+id/ok"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/entry"        android:layout_alignParentRight="true"        android:layout_marginLeft="10dip"        android:text="确定" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_toLeftOf="@id/ok"        android:layout_alignTop="@id/ok"        android:text="取消" /></RelativeLayout>
-------RelativeLayoutActivity---------import android.app.Activity;import android.os.Bundle;public class RelativeLayoutActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);    }}
	
				
		
原创粉丝点击