您的位置:首页 > 移动开发 > Android开发

Android【布局管理器】——相对布局RelativeLayout

2015-05-05 17:23 141 查看
相对布局要比前面讲的线性布局和表格布局要灵活一些,所以平常用得也是比较多的。相对布局控件的位置是与其周围控件的位置相关的,从名字可以看出来,这些位置都是相对的,确定出了其中一个控件的位置就可以确定另一个控件的位置了。
本次实验就是显示如下的activity:

    


其中只有2个button,1个textview,1个edittext。

实现上面activity比较简单,其xml代码如下:

<span style="font-family:Comic Sans MS;font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10px" >

<TextView
android:id="@+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/input_dis"
tools:context=".MainActivity" />

<EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/input"
android:background="@android:drawable/editbox_background"
/>

<Button
android:id="@+id/ok"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@id/edit"
android:layout_alignParentRight="true"
android:layout_marginLeft="10px"
android:text="@string/ok"
/>

<Button
android:id="@+id/cancel"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@id/edit"
android:layout_toLeftOf="@id/ok"
android:text="@string/cancel"

/>

</RelativeLayout></span>


在相对布局中有如下属性,解释如下:

// 相对于给定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,将该控件的置于父控件的中央;

// 指定移动像素

android:layout_marginTop 上偏移的值;

android:layout_marginBottom 下偏移的值;

android:layout_marginLeft   左偏移的值;

android:layout_marginRight   右偏移的值;



android:layout_alignParentRight="true"

使当前控件的右端和父控件的右端对齐。这里属性值只能为true或false,默认false。

android:layout_marginLeft="10dip"

使当前控件左边空出相应的空间。

android:layout_toLeftOf="@id/ok"

使当前控件置于id为ok的控件的左边。

android:layout_alignTop="@id/ok"

使当前控件与id控件的上端对齐。

padding表示填充,margin表示边距

可通过android:padding属性进行设置,4个方向的边距属性为android:paddingLeft, android:paddingRight, android:paddingTop, and android:paddingBottom.

结论:

*android:layout_marginBottom

*android:layout_marginLeft

*android:layout_marginRight

*android:layout_marginTop

上面几个属性的值是根据下面的相对位置的对象的值来做计算的,如果没有相对的对象就以总体布局来计算

*android:layout_below

*android:layout_above

*android:layout_toLeftOf

*android:layout_toRightOf

*android:layout_alignTop

*android:layout_centerHrizontal //是否支持横屏或竖屏

*android:layout_centerVertical //这个根据单词的意思:中心垂直

*android:layout_centerInparent //

android:layout_centerInParent="true"//居中在父对象

android:layout_centerInParent="false" ... 浏览器不支持多窗口显示,意思就是说所有页面在单一窗口打开,这样避免了页面布局控制显示问题

下面的相对于父的相对位置

*android:layout_alignParentBottom

*android:layout_alignParentLeft

*android:layout_alignParentRight

*android:layout_alignParentTop

*android:layout_alignWithParentIfMissing

activity的相对布局比较灵活,一些常见的属性也比较多,用得多自然就会了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: