Android开发之--RelativeLayout用法总结

来源:互联网 发布:免费网络推广的方法 编辑:程序博客网 时间:2024/05/19 07:07

在一般情况下用LinearLayout和TableLayout等就已经足够了,但是有的时候仅仅用这些显的很不灵活,有的时候我们希望有绝对布局的方式,Android里确实有这样的一个绝对布局方式,不过已经被废弃了,在这里不推荐使用,那我们就用RelativeLayout吧--相对布局,很灵活的哦~~~如果再配合上LinearLayout等布局,那么,对于大多数的布局要求就都可以满足了。

首先我们来看RelativeLayout的属性:

// 相对于给定ID控件Android:layout_above          将该控件的底部置于给定ID的控件之上;Android:layout_below          将该控件的底部置于给定ID的控件之下;Android:layout_toLeftOf      将该控件的右边缘与给定ID的控件左边缘对齐;Android:layout_toRightOf     将该控件的左边缘与给定ID的控件右边缘对齐; Android:layout_alignBaseline 将该控件的baseline与给定ID的baseline对齐;Android:layout_alignTop      将该控件的顶部边缘与给定ID的顶部边缘对齐;Android:layout_alignBottom   将该控件的底部边缘与给定ID的底部边缘对齐;Android:layout_alignLeft     将该控件的左边缘与给定ID的左边缘对齐;Android:layout_alignRight    将该控件的右边缘与给定ID的右边缘对齐;// 相对于父组件Android:layout_alignParentTop         如果为true,将该控件的顶部与其父控件的顶部对齐;Android:layout_alignParentBottom      如果为true,将该控件的底部与其父控件的底部对齐;Android:layout_alignParentLeft        如果为true,将该控件的左部与其父控件的左部对齐;Android:layout_alignParentRight       如果为true,将该控件的右部与其父控件的右部对齐;// 居中Android:layout_centerHorizontal      如果为true,将该控件的置于水平居中;Android:layout_centerVertical         如果为true,将该控件的置于垂直居中;Android:layout_centerInParent         如果为true,将该控件的置于父控件的中央;// 指定移动像素,值为pxAndroid:layout_marginTop           上偏移的值;Android:layout_marginBottom        下偏移的值;Android:layout_marginLeft        左偏移的值;Android:layout_marginRight        右偏移的值;


 

下面我们再来看个例子:

   <?xml version="1.0" encoding="utf-8"?>     <RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android"              android:layout_width="fill_parent"          android:layout_height="fill_parent"          android:padding="10px"          >                  <TextView            android:id="@+id/textView"          android:text="TextView"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         />         <!--没有对textView的位置做设置,默认为RelativeLayout容器的左上角  -->         <EditText         android:id="@+id/editText"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_below="@id/textView"         android:background="@android:drawable/editbox_background"         />         <!--editText控件位于textView控件的下面-->         <Button           android:id="@+id/buttonSure"         android:text="确定"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@id/editText"         android:layout_alignParentRight="true"         android:layout_marginLeft="10px"         />         <!--buttonSure控件在editText控件的下面,并且于父容器的位置关系为右对齐。  android:layout_marginLeft="10px"                         设置buttonSure控件的左外边距为10像素,即此控件的左边与其他控件相距10像素的距离-->         <Button         android:id="@+id/buttonCancel"         android:text="取消"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_toLeftOf="@id/buttonSure"         android:layout_alignTop="@id/buttonSure"         />         <!--buttonCancel为相对于buttonSure的位置确定。buttonCancel控件的右边缘与buttonSure控件的左边缘对齐,                          顶部边缘与buttonSure控件的 顶部边缘对齐-->     </RelativeLayout> 


到此为止,相信有过Andriod编程经验的人都应该学会relativeLayout的使用了吧,呵呵....

原创粉丝点击