Android布局的相对性

不常接触Android布局的同学可能会为一个问题犯糊涂,即该属性是相对于父容器还是相对于子元素,还是只对自己有效,下面我们根据一个例子来简单理一下布局相对性的思路。

        
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <!--
  7. 以下的LinearLayout中android:layout_gravity="center"
  8. 相对于其父容器重力方向,所以水平居中
  9. -->
  10. <LinearLayout
  11. android:id="@+id/linearLayout1"
  12. android:layout_width="200dip"
  13. android:layout_height="60dip"
  14. android:layout_gravity="center"
  15. android:background="#00ff00"
  16. android:orientation="horizontal">
  17. <TextView
  18. android:id="@+id/textView1"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_marginRight="2dip"
  22. android:background="#ff0000"
  23. android:text="TextA"/>
  24. <TextView
  25. android:id="@+id/textView2"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:background="#ff0000"
  29. android:text="TextB"/>
  30. </LinearLayout>
  31. <!--
  32. 以下的LinearLayout中android:gravity="bottom"
  33. 表示该LinearLayout的子View相对于LinearLayout是居底部的
  34. -->
  35. <LinearLayout
  36. android:id="@+id/linearLayout2"
  37. android:layout_width="200dip"
  38. android:layout_height="60dip"
  39. android:layout_gravity="center"
  40. android:layout_marginTop="5dip"
  41. android:background="#ffff00"
  42. android:gravity="bottom"
  43. android:orientation="horizontal">
  44. <TextView
  45. android:id="@+id/TextView03"
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content"
  48. android:layout_marginRight="2dip"
  49. android:background="#ff0000"
  50. android:text="TextA"/>
  51. <TextView
  52. android:id="@+id/TextView04"
  53. android:layout_width="wrap_content"
  54. android:layout_height="wrap_content"
  55. android:background="#ff0000"
  56. android:text="TextB"/>
  57. </LinearLayout>
  58. </LinearLayout>

这里对应的效果图贴上

该图分别对应了布局文件中的两个布局思路介绍

  1. 相对于父元素
  2. 相对于子元素

由此我们也来发现以下一般规律:

对于android:layout_xxx开始的属性一般用于“自身”及“相对于父元素”,如android:layout_gravity="center",相对于父容器居中

而对于android:xxx开始的属性则一般作用域子元素,如android:orientation="vertical",其子元素居中

当然,以上只是根据命名方式做的一般性猜测,对于有些属性,如android:layout_marginRight,其相对于父元素或兄弟元素,android:layout_height="60dip"则只对自身有效

更多相关文章

  1. ActionBar的简单使用(修改背景颜色,文字居中,图标)
  2. android LayoutInflater实例化的方法
  3. 类似于android短信校验码的demo
  4. Android(安卓)图片的读取与写入
  5. Android(安卓)Layout之一:FrameLayout
  6. Android(安卓)五大布局
  7. Android(安卓)CoordinatorLayout+AppBarLayout+ToolBar实现标题
  8. Android(安卓)listView+CheckBox的实现
  9. Android(安卓)ConstraintLayout使用指南

随机推荐

  1. android - 为安全而设计 - 3 - 开发文档
  2. RelativeLayout布局用到的主要属性
  3. Android(安卓)Studio 单刷《第一行代码》
  4. android - 为安全而设计 - 3 - 开发文档
  5. RelativeLayout
  6. Android—最新版Android(安卓)studioSDK
  7. Android野史系列:3.利用Android(安卓)Stud
  8. (三)Android事件分发机制 - Activity篇
  9. Android中如何设置RadioButton在文字的右
  10. Android的简单介绍