Eclipse 开发 Android, RelativeLayout (学习3)
16lz
2021-12-04
Hello, RelativeLayout
A RelativeLayout
is a ViewGroup that allows you to layout child elements in positions relative to the parent or siblings elements.
- Start a new project/Activity called HelloRelativeLayout.
- 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 likelayout_below
andlayout_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. - 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 themain.xml
layout file. - Run it.
You should see the following:
备注:RelativeLayout 这种布局方式,是靠一些组件关联,然后形成UI界面。更多相关文章
- Eclipse 开发 Android, LinearLayout (学习2)
- android gridview按钮边框和定制点击颜色
- android之实现各个组件点击事件监听
- Android学习之线性布局管理器
- android键盘事件
- Android应用程序组件Content Provider的共享数据更新通知机制分
- android之实现各个组件点击事件监听
- Android应用程序组件Content Provider的启动过程源代码分析(6)
- 《Android/OPhone 开发完全讲义》样章和目录下载