Hello, RelativeLayout

A RelativeLayout is a ViewGroup that allows you to layout child elements in positions relative to the parent or siblings elements.

  1. Start a new project/Activity called HelloRelativeLayout.
  2. Open the layout file. Make it like so:
    <?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>

    Pay attention to each of the additional layout_* attributes (besides the usual width and height, which are required for all elements). When using relative layout, we use attributes like layout_below and layout_toLeftOf to describe how we'd like to position each View. Naturally, these are different relative positions, and the value of the attribute is the id of the element we want the position relative to.

  3. Make sure your Activity loads this layout in the onCreate() method:

    public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);}

    R.layout.main refers to the main.xml layout file.

  4. Run it.

You should see the following:

备注:RelativeLayout 这种布局方式,是靠一些组件关联,然后形成UI界面。

更多相关文章

  1. Eclipse 开发 Android, LinearLayout (学习2)
  2. android gridview按钮边框和定制点击颜色
  3. android之实现各个组件点击事件监听
  4. Android学习之线性布局管理器
  5. android键盘事件
  6. Android应用程序组件Content Provider的共享数据更新通知机制分
  7. android之实现各个组件点击事件监听
  8. Android应用程序组件Content Provider的启动过程源代码分析(6)
  9. 《Android/OPhone 开发完全讲义》样章和目录下载

随机推荐

  1. 为什么fork炸弹没有崩溃android?
  2. 汇总(之三):交叉编译工具链arm-none-linux-g
  3. yum和rpm安装zabbix 2.0.9 For centos 6
  4. 如何测试已部署的Web应用程序
  5. linux 使用yum给已安装的软件降级
  6. linux设置了 /etc/profile ~/.bashrc等配
  7. / usr / bin / ld:找不到-lcurl
  8. Linux-vfork与fork简单对比分析
  9. linux下Nload安装说明
  10. MyEclipse10.1正式版官网下载(附Win+Llinu