[转]Android布局之RelativeLayout(相对布局)
2011-03-16 19:02
411 查看
相对布局由于属性比较多,所以用起来有些费力,但灵活性较其他布局方法好,所以掌握好相对布局将会非常有用。先看等一个例子:
<?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">
<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="OK" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel" />
</RelativeLayout>
这是很常见的布局内容,讲解如下:
1 android:layout_below="@id/label"/>
将当前控件放置于id为label 的控件下方。
android:layout_alignParentRight="true"
使当前控件的右端和父控件的右端对齐。这里属性值只能为true或false,默认false。
android:layout_marginLeft="10dip"
使当前控件左边空出相应的空间。
android:layout_toLeftOf="@id/ok"
使当前控件置于id为ok的控件的左边。
android:layout_alignTop="@id/ok"
使当前控件与id控件的上端对齐。
至此,我们已经发现,其属性之繁多。下面简单归纳一下:
第一类:属性值为true或false
*android:layout_centerHrizontal
*android:layout_centerVertical
*android:layout_centerInparent
*android:layout_alignParentBottom
*android:layout_alignParentLeft
*android:layout_alignParentRight
*android:layout_alignParentTop
*android:layout_alignWithParentIfMissing
第二类:属性值必须为id的引用名“@id/id-name”
*android:layout_below
*android:layout_above
*android:layout_toLeftOf
*android:layout_toRightOf
*android:layout_alignTop
第三类:属性值为具体的像素值,如30dip,40px
*android:layout_marginBottom
*android:layout_marginLeft
*android:layout_marginRight
*android:layout_marginTop
再看第二个例子:
<?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">
<AnalogClock
android:id="@+id/aclock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<DigitalClock
android:id="@+id/dclock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/aclock"
android:layout_alignLeft="@id/aclock"
android:layout_marginLeft="40px"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前时间:"
android:layout_toLeftOf="@id/dclock"
android:layout_alignTop="@id/aclock"/>
</RelativeLayout>
两个例子的效果图如下:第一个例子
第二个例子
<?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">
<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="OK" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel" />
</RelativeLayout>
这是很常见的布局内容,讲解如下:
1 android:layout_below="@id/label"/>
将当前控件放置于id为label 的控件下方。
android:layout_alignParentRight="true"
使当前控件的右端和父控件的右端对齐。这里属性值只能为true或false,默认false。
android:layout_marginLeft="10dip"
使当前控件左边空出相应的空间。
android:layout_toLeftOf="@id/ok"
使当前控件置于id为ok的控件的左边。
android:layout_alignTop="@id/ok"
使当前控件与id控件的上端对齐。
至此,我们已经发现,其属性之繁多。下面简单归纳一下:
第一类:属性值为true或false
*android:layout_centerHrizontal
*android:layout_centerVertical
*android:layout_centerInparent
*android:layout_alignParentBottom
*android:layout_alignParentLeft
*android:layout_alignParentRight
*android:layout_alignParentTop
*android:layout_alignWithParentIfMissing
第二类:属性值必须为id的引用名“@id/id-name”
*android:layout_below
*android:layout_above
*android:layout_toLeftOf
*android:layout_toRightOf
*android:layout_alignTop
第三类:属性值为具体的像素值,如30dip,40px
*android:layout_marginBottom
*android:layout_marginLeft
*android:layout_marginRight
*android:layout_marginTop
再看第二个例子:
<?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">
<AnalogClock
android:id="@+id/aclock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<DigitalClock
android:id="@+id/dclock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/aclock"
android:layout_alignLeft="@id/aclock"
android:layout_marginLeft="40px"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前时间:"
android:layout_toLeftOf="@id/dclock"
android:layout_alignTop="@id/aclock"/>
</RelativeLayout>
两个例子的效果图如下:第一个例子
第二个例子
相关文章推荐
- Android RelativeLayout相对布局属性简析
- 第13章、布局Layouts之RelativeLayout相对布局(从零开始学Android)
- Android之RelativeLayout相对布局
- Android布局之RelativeLayout相对布局
- Android开发视频教程之十一(Activity布局初步三:相对布局RelativeLayout)
- Android RelativeLayout (相对布局)使用及介绍
- Android相对布局RelativeLayout属性说明
- Android开发-RelativeLayout内多次引用同一个RelativeLayout,相对布局
- Android实现计算器布局(四种布局方式)之RelativeLayout相对布局
- Android中RelativeLayout相对布局
- Android相对布局(RelativeLayout)
- 【Android基础笔记03】 Android RelativeLayout相对布局和基本UI控件
- Android之RelativeLayout(相对布局)
- Android:控件布局(相对布局)RelativeLayout(转)
- Android两大布局:线性布局(LinearLayout) 相对布局(RelativeLayout)
- Android相对布局RelativeLayout各属性介绍
- Android相对布局(RelativeLayout)
- Android相对布局RelativeLayout,Intent用法,Intent传值(对象),Intent.action大全
- Android应用开发之RelativeLayout (相对布局)+梅花效果案例
- Android 基础 RelativeLayout相对布局