android 线性布局(LinearLayout)

约定不等于承诺〃 2023-10-06 11:28 136阅读 0赞

线性布局是程序中最常见的布局方式之一,

线性布局可以分为水平线性布局和垂直线性布局两种,分别是通过android:orientation=”horizontal”和android:orientation=”vertical”来控制的

线性布局中,有 几个及其重要的参数,直接决定元素的布局和位置,这几个参数是

android:layout_gravity ( 是本元素相对于父元素的对齐方式 )

android:gravity=“bottom|right”(是本元素所有子元素的对齐方式,设置在父元素上,多个值用|隔开)

android:layout_gravity (子元素在父元素的对齐方式,设置在子元素上)

当 android:orientation=“vertical” 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。
当 android:orientation=“horizontal” 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。

android:padding=“10dp” (是本元素所有子元素的与父元素边缘的距离,设置在父元素上)

android:layout_marginLeft=“10dp”(子元素与父元素边缘的距离,设置在子元素上)

android:orientation (线性布局以列或行来显示内部子元素)

android:layout_weight =“1” 分配分配权重值

下面举例说明

在这里插入图片描述

布局代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- 垂直布局 -->
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical" >
  7.   <!-- 垂直布局 -->
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_weight="1"
  11. android:orientation="vertical"
  12. >
  13. <TextView
  14. android:layout_height="match_parent"
  15. android:layout_width="match_parent"
  16. android:layout_weight="1"
  17. android:background="#ff0000"
  18. />
  19. <TextView
  20. android:layout_height="match_parent"
  21. android:layout_width="match_parent"
  22. android:layout_weight="1"
  23. android:background="#00ff00"
  24. />
  25. <TextView
  26. android:layout_height="match_parent"
  27. android:layout_width="match_parent"
  28. android:layout_weight="1"
  29. android:background="#0000ff"
  30. />
  31. </LinearLayout>
  32. <!-- 水平布局 -->
  33. <LinearLayout
  34. android:layout_width="match_parent"
  35. android:orientation="horizontal"
  36. android:layout_weight="2"
  37. >
  38. <TextView
  39. android:layout_height="match_parent"
  40. android:layout_width="match_parent"
  41. android:layout_weight="1"
  42. android:background="#ff0000"
  43. />
  44. <TextView
  45. android:layout_height="match_parent"
  46. android:layout_width="match_parent"
  47. android:layout_weight="1"
  48. android:background="#00ff00"
  49. />
  50. <TextView
  51. android:layout_height="match_parent"
  52. android:layout_width="match_parent"
  53. android:layout_weight="1"
  54. android:background="#0000ff"
  55. />
  56. <TextView
  57. android:layout_height="match_parent"
  58. android:layout_width="match_parent"
  59. android:layout_weight="1"
  60. android:background="#c0c0c0"
  61. />
  62. </LinearLayout>
  63. </LinearLayout>

权重:

android:layout_weight=”1”通过设置控件的layout_weight属性以控制各个控件在布局中的相对大小,线性布局会根据该控件layout_weight值与其所处布局中所有控件layout_weight值之和的比值为该控件分配占用的区域。在水平布局的LinearLayout中有4个TxtView,这4个TextView的layout_weight属性值都为1,那么这4个TextView的大小将拉伸到总大小的四分之一。如果layout_weight指为0,控件会按原大小显示,不会被拉伸;对于其余layout_weight属性值大于0的控件,系统将会减去layout_weight属性值为0的控件的宽度或者高度,再用剩余的宽度或高度按相应的比例来分配每一个控件显示的宽度或高度。

权重最基本的用法就是 对线性布局指定方向(水平或垂直)上剩余空间分配的一个规则,先把规定的大小占完,再来按比例分配剩余空间

特殊情况:

首先计算数值,所有控件加起来后可能超过屏幕大小了,这个时候剩余值就应该是负的,此时按权重分配,权重大的分得值比较大,但是负的,这个时候加上原来的值,反而变小

权重有一个很有用的特点,在一些特殊应用场景,比如有两个控件,一个设置了权重,一个不设置权重,那么这个设置权重的控件会后加载渲染。

发表评论

表情:
评论列表 (有 0 条评论,136人围观)

还没有评论,来说两句吧...

相关阅读