• 【转】android 之LinearLayout布局


    声明:这是android官方网站上的例子,添加了自己的理解,望各位大牛们多多指教

    Java代码 复制代码 收藏代码
    1. main.xml
    2. <?xml version="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="row one"
    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="row two"
    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="row three"
    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="row four"
    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. package com.cn.view;
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. public class Views extends Activity {
    6. /** Called when the activity is first created. */
    7. @Override
    8. public void onCreate(Bundle savedInstanceState) {
    9. super.onCreate(savedInstanceState);
    10. setContentView(R.layout.main);
    11. }
    12. }
  • 相关阅读:
    一个机智的数组去重方法
    gets(),fgets()的作用机制探究
    性能优化之布局优化
    android 中theme和style的语法相关
    可以一直滚动的跑马灯
    ExpandableListView的OnitemLongclickListener事件
    关于 edittext 软键盘退出监听解决办法
    Android 开发之拦截EditText的输入内容,定制输入内容
    decimalFormat(小数格式)
    oracle常用
  • 原文地址:https://www.cnblogs.com/cappuccino/p/2112926.html
Copyright © 2020-2023  润新知