相对布局由于属性比较多,所以用起来有些费力,但灵活性较其他布局方法好,所以掌握好相对布局将会非常有用。先看等一个例子

 

            
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <TextView
  6. android:id="@+id/label"
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="Type here:"/>
  10. <EditText
  11. android:id="@+id/entry"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:background="@android:drawable/editbox_background"
  15. android:layout_below="@id/label"/>
  16. <Button
  17. android:id="@+id/ok"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_below="@id/entry"
  21. android:layout_alignParentRight="true"
  22. android:layout_marginLeft="10dip"
  23. android:text="OK" />
  24. <Button
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:layout_toLeftOf="@id/ok"
  28. android:layout_alignTop="@id/ok"
  29. android:text="Cancel" />
  30. RelativeLayout>

这是很常见的布局内容,讲解如下:

 

            
  1. android:layout_below="@id/label"/>

将当前控件放置于id为label 的控件下方

        android:layout_alignParentRight="true"

使当前控件的右端和父控件的右端对齐。这里属性值只能为true或false,默认false。

android:layout_marginLeft="10dip"

使当前控件左边空出相应的空间。

        android:layout_toLeftOf="@id/ok"

使当前控件置于id为ok的控件的左边。

        android:layout_alignTop="@id/ok"

使当前控件与id控件的上端对齐。

至此,我们已经发现,其属性之繁多。下面简单归纳一下:
第一类:属性值为true或false
*android:layout_centerHrizontal
*android:layout_centerVertical
*android:layout_centerInparent
*android:layout_alignParentBottom
*android:layout_alignParentLeft
*android:layout_alignParentRight
*android:layout_alignParentTop
*android:layout_alignWithParentIfMissing
第二类:属性值必须为id的引用名“@id/id-name”
*android:layout_below
*android:layout_above
*android:layout_toLeftOf
*android:layout_toRightOf
*android:layout_alignTop
第三类:属性值为具体的像素值,如30dip,40px
*android:layout_marginBottom
*android:layout_marginLeft
*android:layout_marginRight
*android:layout_marginTop

再看第二个例子:

<?xml version="1.0" encoding="utf-8"?>     xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent">   android:id="@+id/aclock"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_centerInParent="true"/>    android:id="@+id/dclock"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_below="@id/aclock"  android:layout_alignLeft="@id/aclock"  android:layout_marginLeft="40px"/>    android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="当前时间:"        android:layout_toLeftOf="@id/dclock"        android:layout_alignTop="@id/aclock"/>    

 

更多相关文章

  1. 读取android手机流量信息
  2. 三、安卓UI学习(1)
  3. android用户界面之按钮(Button)教程实例汇
  4. 在Fragment中设置控件点击方法,执行失败。
  5. TabHost与RadioGroup结合完成的菜单【带效果图】5个Activity
  6. Android开发——Android搜索框架(二)
  7. android用户界面-组件Widget-地图视图MapView
  8. Android常用控件
  9. android用户界面-组件Widget-画廊视图Gallery

随机推荐

  1. PHP开发Android应用程序
  2. Android 防界面劫持方案,无视Android系统
  3. 谷歌应用在Android智能机渗透率高达七成
  4. android v7兼容包RecyclerView的使用(四)—
  5. Android优秀学习资源列表
  6. Android语音便签快速调用-内置录音程序
  7. Android(安卓)Universal Image Loader 源
  8. Android(安卓)greenDAO3x数据库框架使用
  9. 【Android】Android 监听apk安装替换卸载
  10. Android TextView实现滚动跑马灯效果Marq