Android SDK Tutorials系列 - Hello Views - Relative Layout
来源:互联网 发布:淘宝差评什么时候消失 编辑:程序博客网 时间:2024/05/18 02:01
Relative Layout
RelativeLayout
是ViewGroup
的一种,它里面包含的View按照相对位置进行排列,可以指定一个View跟相邻View的位置关系(例如:在某个View的左边,或者下面);或者指定这个View相对于RelativeLayout这个容器的位置(例如底部,或者左边中间)。
RelativeLayout是一个很强大的工具,在设计用户界面的时候可以消除嵌套的ViewGroup。如果你在嵌套使用LinearLayout,你应该可以用单个的RelativeLayout来取代它。
- 创建一个工程:HelloRelativeLayout
- 打开
res/layout/main.xml
并修改如下:<?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>
关注每一个
android:layout_*
属性,例如layout_below
,layout_alignParentRight
, 还有layout_toLeftOf。
使用RelativeLayout的时候,用这些属性来设置每个View的位置。这些属性的每一个都定义了一种相对位置。有些属性使用相邻View的资源ID来定义自己的相对位置。例如,最后一个Button,被摆放在资源IDok
(这是前一个Button)的左边,并和它上对齐。所有的布局属性都定义在
RelativeLayout.LayoutParams
. - 确保你在
onCreate()
方法装载了这个布局:public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);}
setContentView(int)
方法装载这个Activity的布局文件,资源ID —R.layout.main
指向res/layout/main.xml
布局文件。 - 运行应用。
应该能看到下面的画面:
返回 Android SDK Tutorials系列 - Hello Views
转载请注明出处:http://blog.csdn.net/mtding/article/details/6960267
- Android SDK Tutorials系列 - Hello Views - Relative Layout
- Android SDK Tutorials系列 - Hello Views - Linear Layout
- Android SDK Tutorials系列 - Hello Views - Table Layout
- Android SDK Tutorials系列 - Hello Views - Tab Layout
- Android SDK Tutorials系列 - Hello Views
- Android SDK Tutorials系列 - Hello Views - Spinner
- Android SDK Tutorials系列 - Hello Views - Gallery
- Android SDK Tutorials系列 - Hello Views - Grid View
- Android SDK Tutorials系列 - Hello Views - List View
- Android SDK Tutorials系列 - Hello Views - Date Picker
- Android SDK Tutorials系列 - Hello Views - Time Picker
- Android SDK Tutorials系列 - Hello Views - Form Stuff
- Android SDK Tutorials系列 - Hello Views - Auto Complete
- Android SDK Tutorials系列 - Hello Views - Google Map View
- Android SDK Tutorials系列 - Hello Views - Web View
- Android SDK Tutorials系列 - Hello World
- Android SDK Tutorials系列 - Hello Localization
- android Relative Layout 属性
- 临时文件
- 蓝牙三国Kill:这算不算是鸡肋?
- JS 命名空间
- 判断列是否存在
- asp.net后台获取路径的各种方法归纳
- Android SDK Tutorials系列 - Hello Views - Relative Layout
- 会计中的借贷
- Android下载文件保存到路径
- gcc: when initialized here
- Kindeditor 中判断该编辑器中的内容是否为空:
- html5_1
- SQL 月初月末 排除临界 无BUG 日期大全 日期差
- Python字符串比较忽略大小写的方法
- 关于SQL Server的若干注意事项