转自:http://www.iteye.com/topic/423173

  1. main.xml
  2. <?xmlversion="1.0"encoding="utf-8"?>
  3. <!--
  4. <LinearLayout>
  5. 线性版面配置,在这个标签中,所有元件都是按由上到下的排队排成的
  6. -->
  7. <LinearLayout
  8. xmlns:android="http://schemas.android.com/apk/res/android"
  9. android:orientation="vertical"
  10. android:layout_width="fill_parent"
  11. android:layout_height="fill_parent">
  12. <!--android:orientation="vertical"表示竖直方式对齐
  13. android:orientation="horizontal"表示水平方式对齐
  14. android:layout_width="fill_parent"定义当前视图在屏幕上
  15. 可以消费的宽度,fill_parent即填充整个屏幕。
  16. android:layout_height="wrap_content":随着文字栏位的不同
  17. 而改变这个视图的宽度或者高度。有点自动设置框度或者高度的意思
  18. layout_weight用于给一个线性布局中的诸多视图的重要度赋值。
  19. 所有的视图都有一个layout_weight值,默认为零,意思是需要显示
  20. 多大的视图就占据多大的屏幕空间。若赋一个高于零的值,则将父视
  21. 图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight
  22. 值以及该值在当前屏幕布局的整体layout_weight值和在其它视图屏幕布
  23. 局的layout_weight值中所占的比率而定。
  24. 举个例子:比如说我们在水平方向上有一个文本标签和两个文本编辑元素。
  25. 该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。
  26. 如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分
  27. 在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个
  28. 文本编辑元素其中第一个的layout_weight值设置为1,而第二个的设置为2
  29. 则剩余空间的三分之二分给第一个,三分之一分给第二个(数值越小,重要
  30. 度越高)。
  31. -->
  32. <LinearLayout
  33. android:orientation="horizontal"
  34. android:layout_width="fill_parent"
  35. android:layout_height="fill_parent"
  36. android:layout_weight="1">
  37. <TextView
  38. android:text="red"
  39. android:gravity="center_horizontal"
  40. android:background="#aa0000"
  41. android:layout_width="wrap_content"
  42. android:layout_height="fill_parent"
  43. android:layout_weight="1"/>
  44. <TextView
  45. android:text="green"
  46. android:gravity="center_horizontal"
  47. android:background="#00aa00"
  48. android:layout_width="wrap_content"
  49. android:layout_height="fill_parent"
  50. android:layout_weight="1"/>
  51. <TextView
  52. android:text="blue"
  53. android:gravity="center_horizontal"
  54. android:background="#0000aa"
  55. android:layout_width="wrap_content"
  56. android:layout_height="fill_parent"
  57. android:layout_weight="1"/>
  58. <TextView
  59. android:text="yellow"
  60. android:gravity="center_horizontal"
  61. android:background="#aaaa00"
  62. android:layout_width="wrap_content"
  63. android:layout_height="fill_parent"
  64. android:layout_weight="1"/>
  65. </LinearLayout>
  66. <LinearLayout
  67. android:orientation="vertical"
  68. android:layout_width="fill_parent"
  69. android:layout_height="fill_parent"
  70. android:layout_weight="2">
  71. <TextView
  72. android:text="rowone"
  73. android:textSize="15pt"
  74. android:layout_width="fill_parent"
  75. android:layout_height="wrap_content"
  76. android:layout_weight="1"/>
  77. <TextView
  78. android:text="rowtwo"
  79. android:textSize="15pt"
  80. android:layout_width="fill_parent"
  81. android:layout_height="wrap_content"
  82. android:layout_weight="1"/>
  83. <TextView
  84. android:text="rowthree"
  85. android:textSize="15pt"
  86. android:layout_width="fill_parent"
  87. android:layout_height="wrap_content"
  88. android:layout_weight="1"/>
  89. <TextView
  90. android:text="rowfour"
  91. android:textSize="15pt"
  92. android:layout_width="fill_parent"
  93. android:layout_height="wrap_content"
  94. android:layout_weight="1"/>
  95. </LinearLayout>
  96. </LinearLayout>

感觉这种形式有点像div+css的方式布局,不过这种方式的灵活性和div+css还是有些不及,主要是那android:layout_weight的值如何去确定,而且采用的是数值越小,重要度越高的方式,分配起来还得好好计算一下。

Java代码 收藏代码
  1. Views.java
  2. packagecom.cn.view;
  3. importandroid.app.Activity;
  4. importandroid.os.Bundle;
  5. publicclassViewsextendsActivity{
  6. /**Calledwhentheactivityisfirstcreated.*/
  7. @Override
  8. publicvoidonCreate(BundlesavedInstanceState){
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.main);
  11. }
  12. }

我使用的版本为1.5.欢迎朋友们讨论




更多相关文章

  1. Android获取屏幕的高度和宽度
  2. 控件:拖动条 --- SeekBar(改变屏幕亮度)
  3. 【Android】获取屏幕分辨率和顶栏高度全屏和横屏
  4. android取得手机屏幕大小DisplayMetrics的核心代码
  5. Android (滑动屏幕切换图片的实现)
  6. [Android]屏幕自适应布局
  7. 【Android】通过手势切换屏幕的几种方法
  8. 音乐播放器 ViewFlipper 滑动屏幕

随机推荐

  1. 用 Linux 下所有的压缩、解压命令造轮子
  2. AIOps 如何优雅服务应用运维?看民生银行智
  3. 分表分库后的id分配问题
  4. 关于面试中必问的跨表Join问题
  5. 这个数据分析师今年升职加薪了!他做对了什
  6. 0318前端第一课作业vscode安装及插件使用
  7. Emmet 语法规则
  8. 如何安装vscode,以及markdown语法的使用
  9. markdown 语法及Emmet 插件的使用
  10. vs code,md,emmet语法