`

7.3 RelativeLayout布局详解

阅读更多
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开发案例驱动教程》第七章
  • 大小: 7.1 KB
  • 大小: 9.6 KB
分享到:
评论

相关推荐

    Android入门到精通源代码.

    6.2.3 相对布局(RelativeLayout) 6.2.4 表格布局(TableLayout) 6.2.5 绝对布局(AbsoluteLayout) 6.3 界面中的字体 6.3.1 设置系统字体 6.3.2 引用用户自定义字体 6.4 应用实例详解:制作手机桌面 6.4.1 实例...

    android开发揭秘PDF

    4.3.3.相对布局(RelativeLayout) 4.3.4 表单布局(TableLayout) 4.3.5 切换卡(TabWidget) 4,4 小结 第5章 Android游戏开发 5.1 Android游戏开发框架 5.1.1 View类开发框架 5.1.2 SurfaceView类开发框架 5.2 Graphics...

    《Android应用开发揭秘》附带光盘代码.

    相对布局(RelativeLayout)  4.3.4 表单布局(TableLayout)  4.3.5 切换卡(TabWidget)  4,4 小结  第5章 Android游戏开发  5.1 Android游戏开发框架  5.1.1 View类开发框架  5.1.2 SurfaceView类开发框架  5.2...

    《Android应用开发揭秘》源码

    相对布局(RelativeLayout)  4.3.4 表单布局(TableLayout)  4.3.5 切换卡(TabWidget)  4,4 小结  第5章 Android游戏开发  5.1 Android游戏开发框架  5.1.1 View类开发框架  5.1.2 SurfaceView类开发框架  5.2...

    Android应用开发揭秘pdf高清版

    4.3.3.相对布局(RelativeLayout) 4.3.4 表单布局(TableLayout) 4.3.5 切换卡(TabWidget) 4,4 小结 第5章 Android游戏开发 5.1 Android游戏开发框架 5.1.1 View类开发框架 5.1.2 SurfaceView类开发框架 5.2 Graphics...

    Android实例代码

    2.2、布局管理(Layout):LinearLayout、TableLayout、FrameLayout、RelativeLayout; 2.3、基本界面组件:TextView、EditText; Button、ImageButton; 9Patch; RadioButton、CheckBox; ToggleButton;AnalogClock、...

    疯狂Android讲义(第2版)源代码 第6章~第9章

    2.2、布局管理(Layout):LinearLayout、TableLayout、FrameLayout、RelativeLayout; 2.3、基本界面组件:TextView、EditText; Button、ImageButton; 9Patch; RadioButton、CheckBox; ToggleButton;AnalogClock...

Global site tag (gtag.js) - Google Analytics