7.3 RelativeLayout布局详解

简介: RelativeLayout相对布局, 允许子元素指定他们相对于其它元素或父元素的位置(通过ID 指定)。因此,可以以左右对齐、上下对齐、置于屏幕中央等形式来排列元素。相对布局在实际应用中比较常用。图7-13所示是垂直方向上的应用。<br>  <br><img src="http://dl.iteye.com/upload/attachment/520149/0b25296b-9a5e-374
RelativeLayout相对布局, 允许子元素指定他们相对于其它元素或父元素的位置(通过ID 指定)。因此,可以以左右对齐、上下对齐、置于屏幕中央等形式来排列元素。相对布局在实际应用中比较常用。图7-13所示是垂直方向上的应用。
 


图7-13 RelativeLayout1
RelativeLayout布局文件请参考代码清单7-15,完整代码请参考chapter7_3工程中relativelayout1.xml代码部分(chapter7_3/res/layout/relativelayout1.xml)。
【代码清单7-15】
<?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="wrap_content"
android:padding="10dip">
<TextView android:id="@+id/labe1" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:text="上" android:background="#ff0060" />

<TextView android:id="@+id/labe2" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_alignParentBottom="true"
android:text="下" android:background="#2121f4" />

<TextView android:id="@+id/labe3" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_above="@id/labe2"
android:layout_below="@id/labe1" android:text="中"
android:background="#ffffff" />
</RelativeLayout>
图7-14所示是水平方向上的应用。
 


图7-14 RelativeLayout2
RelativeLayout布局文件请参考代码清单7-16,完整代码请参考chapter7_3工程中relativelayout2.xml代码部分(chapter7_3/res/layout/relativelayout2.xml)。
【代码清单7-16】
<?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="wrap_content"
android:padding="10dip">

<TextView android:id="@+id/labe4" android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="@string/search" />

<EditText android:id="@+id/entry" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_below="@id/labe4"
android:background="@android:drawable/editbox_background" />

<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="@string/confirm" />

<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok" android:text="@string/cancel" />
</RelativeLayout>
                                        出自《Android开发案例驱动教程》第七章
目录
相关文章
|
8月前
|
XML Android开发 数据格式
Android XML 布局基础(六)相对布局 - RelativeLayout
Android XML 布局基础(六)相对布局 - RelativeLayout
104 0
|
8月前
|
XML Android开发 数据格式
Android XML 布局基础(五)线性布局 - LinearLayout
Android XML 布局基础(五)线性布局 - LinearLayout
86 0
|
8月前
|
XML Android开发 数据格式
Android XML 布局基础(七)帧布局 - FrameLayout
Android XML 布局基础(七)帧布局 - FrameLayout
54 0
|
容器
RelativeLayout(相对布局)
LinearLayout也是我们用的比较多的一个布局,我们更多的时候更钟情于他的weight(权重)属性,等比例划分,对屏幕适配还是帮助蛮大的;但是使用LinearLayout的时候也有一个问题,就是当界面比较复杂的时候,需要嵌套多层的LinearLayout,这样就会降低UI Render的效率(渲染速度),而且如果是listview或者GridView上的item,效率会更低,另外太多层LinearLayout嵌套会占用更多的系统资源,还有可能引发stackoverflow;但是如果我们使用RelativeLayout的话,可能仅仅需要一层就可以完成了,以父容器或者兄弟组件参考+margi
62 0
|
算法 Java Android开发
LinearLayout(线性布局)
本节开始讲Android中的布局,今天我们要讲解的就是第一个布局,LinearLayout(线性布局),我们屏幕适配的使用用的比较多的就是LinearLayout的weight(权重属性),在这一节里,我们会详细地解析LinearLayout,包括一些基本的属性,Weight属性的使用,以及比例如何计算,另外还会说下一个用的比较少的属性:android:divider绘制下划线!
50 0
|
XML Android开发 数据格式
Android开发之LinearLayout布局详解
Android开发之LinearLayout布局详解
269 0
|
Android开发 容器
Android开发之FrameLayout布局
Android开发之FrameLayout布局
237 0
Android开发之FrameLayout布局
|
XML Android开发 数据格式
Android开发中RelativeLayout相对布局
Android开发中RelativeLayout相对布局
288 0
Android开发中RelativeLayout相对布局

热门文章

最新文章