android布局之LinearLayout, layout_weight

layout_weight这个参数很有用,在LinearLayout布局中可以通过这个调整各个组件占的面积的权重,如下是一个例子

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

  1. package com.eoembile.iceskysl.testlayout;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. public class Views extends Activity {
  5. /** Called when the activity is first created. */
  6. @Override
  7. public void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main);
  10. }
  11. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值