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

Android学习 8-> 相对布局RelativeLayout

2015-06-17 10:36 267 查看
相对布局RelativeLayout:允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一。由于灵活性大很多,当然属性也多,操作难度也大,属性之间产生冲突的的可能性也大,使用相对布局时要多做些测试。



其xml代码如下(当然也可以试试别的属性定义位置实现其效果):

<span style="font-size:24px;"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<!--
android:layout_alignParentLeft="true"
由于相对布局默认的是从左上角开始放置,所有此属性可以不用设置

-->

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="按钮1" />

<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="按钮2" />

<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="按钮3" />

<!--
android:layout_alignParentLeft="true"
由于此处默认从左边开始放置,所有可以不用设置
-->

<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="按钮4" />

<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="按钮5" />

</RelativeLayout></span>


上面的代码中<!-- -->中间的内容是注释内容。

对于相对布局的用法在之前的介绍中有了,就不再写出来了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: