0


Android学习之路(3) 布局

线性布局LinearLayout

前几个小节的例程中,XML文件用到了LinearLayout布局,它的学名为线性布局。顾名思义,线性布局 像是用一根线把它的内部视图串起来,故而内部视图之间的排列顺序是固定的,要么从左到右排列,要 么从上到下排列。在XML文件中,LinearLayout通过属性android:orientation区分两种方向,其中从左 到右排列叫作水平方向,属性值为horizontal;从上到下排列叫作垂直方向,属性值为vertical。如果LinearLayout标签不指定具体方向,则系统默认该布局为水平方向排列,也就是默认

  1. android:orientation="horizontal"

.

下面做个实验,让XML文件的根节点挂着两个线性布局,第一个线性布局采取horizontal水平方向,第 二个线性布局采取vertical垂直方向。然后每个线性布局内部各有两个文本视图,通过观察这些文本视图 的排列情况,从而检验线性布局的显示效果。详细的XML文件内容如下所示:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <LinearLayout
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:orientation="horizontal">
  9. <TextView
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="横排第一个"
  13. android:textSize="17sp"
  14. android:textColor="#000000" />
  15. <TextView
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="横排第二个"
  19. android:textSize="17sp"
  20. android:textColor="#000000" />
  21. </LinearLayout>
  22. <LinearLayout
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:orientation="vertical">
  26. <TextView
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:text="竖排第一个"
  30. android:textSize="17sp"
  31. android:textColor="#000000" />
  32. <TextView
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:text="竖排第二个"
  36. android:textSize="17sp"
  37. android:textColor="#000000" />
  38. </LinearLayout>
  39. </LinearLayout>

运行测试App,进入如下图所示的演示页面,可见horizontal为横向排列,vertical为纵向排列,说明android:orientation的方向属性确实奏效了。

除了方向之外,线性布局还有一个权重概念,所谓权重,指的是线性布局的下级视图各自拥有多大比例 的宽高。比如一块蛋糕分给两个人吃,可能两人平均分,也可能甲分三分之一,乙分三分之二。两人平 均分的话,先把蛋糕切两半,然后甲分到一半,乙分到另一半,此时甲乙的权重比为1:1。甲分三分之 一、乙分三分之二的话,先把蛋糕平均切成三块,然后甲分到一块,乙分到两块,此时甲乙的权重比为1:2。就线性布局而言,它自身的尺寸相当于一整块蛋糕,它的下级视图们一起来分这个尺寸蛋糕,有的 视图分得多,有的视图分得少。分多分少全凭每个视图分到了多大的权重,这个权重在XML文件中通过 属性

  1. android:layout_weight

来表达。

把线性布局看作蛋糕的话,分蛋糕的甲乙两人就相当于线性布局的下级视图。假设线性布局平均分为左 右两块,则甲视图和乙视图的权重比为1:1,意味着两个下级视图的layout_weight属性都是1。不过视图 有宽高两个方向,系统怎知layout_weight表示哪个方向的权重呢?所以这里有个规定,一旦设置了layout_weight属性值,便要求layout_width填0dp或者layout_height填0dp。如果layout_width填0dp,则layout_weight表示水平方向的权重,下级视图会从左往右分割线性布局;如果layout_height填0dp,则layout_weight表示垂直方向的权重,下级视图会从上往下分割线性布局。 按照左右均分的话,线性布局设置水平方向horizontal,且甲乙两视图的layout_width都填0dp,layout_weight都填1,此时横排的XML片段示例如下:

  1. <LinearLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="horizontal">
  5. <TextView
  6. android:layout_width="0dp"
  7. android:layout_height="wrap_content"
  8. android:layout_weight="1"
  9. android:text="横排第一个"
  10. android:textSize="17sp"
  11. android:textColor="#000000" />
  12. <TextView
  13. android:layout_width="0dp"
  14. android:layout_height="wrap_content"
  15. android:layout_weight="1"
  16. android:text="横排第二个"
  17. android:textSize="17sp"
  18. android:textColor="#000000" />
  19. </LinearLayout>

按照上下均分的话,线性布局设置垂直方向vertical,且甲乙两视图的layout_height都填0dp,layout_weight都填1,此时竖排的XML片段示例如下:

  1. <LinearLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="vertical">
  5. <TextView
  6. android:layout_width="wrap_content"
  7. android:layout_height="0dp"
  8. android:layout_weight="1"
  9. android:text="竖排第一个"
  10. android:textSize="17sp"
  11. android:textColor="#000000" />
  12. <TextView
  13. android:layout_width="wrap_content"
  14. android:layout_height="0dp"
  15. android:layout_weight="1"
  16. android:text="竖排第二个"
  17. android:textSize="17sp"
  18. android:textColor="#000000" />
  19. </LinearLayout>

把上面两个片段放到新页面的XML文件,其中第一个是横排区域采用红色背景(色值为ff0000),第二 个是竖排区域采用青色背景(色值为00ffff)。重新运行测试App,打开演示界面如下图所示,可见横 排区域平均分为左右两块,竖排区域平均分为上下两块。

相对布局RelativeLayout

线性布局的下级视图是顺序排列着的,另一种相对布局的下级视图位置则由其他视图决定。相对布局名 为RelativeLayout,因为下级视图的位置是相对位置,所以得有具体的参照物才能确定最终位置。如果不设定下级视图的参照物,那么下级视图默认显示在RelativeLayout内部的左上角。 用于确定下级视图位置的参照物分两种,一种是与该视图自身平级的视图;另一种是该视图的上级视图 (也就是它归属的RelativeLayout)。综合两种参照物,相对位置在XML文件中的属性名称说明见下表。

为了更好地理解上述相对属性的含义,接下来使用RelativeLayout及其下级视图进行布局来看看实际效果图。下面是演示相对布局的XML文件例子:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="150dp" >
  4. <TextView
  5. android:id="@+id/tv_center"
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:layout_centerInParent="true"
  9. android:background="#ffffff"
  10. android:text="我在中间"
  11. android:textSize="11sp"
  12. android:textColor="#000000" />
  13. <TextView
  14. android:id="@+id/tv_center_horizontal"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_centerHorizontal="true"
  18. android:background="#eeeeee"
  19. android:text="我在水平中间"
  20. android:textSize="11sp"
  21. android:textColor="#000000" />
  22. <TextView
  23. android:id="@+id/tv_center_vertical"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_centerVertical="true"
  27. android:background="#eeeeee"
  28. android:text="我在垂直中间"
  29. android:textSize="11sp"
  30. android:textColor="#000000" />
  31. <TextView
  32. android:id="@+id/tv_parent_left"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:layout_alignParentLeft="true"
  36. android:background="#eeeeee"
  37. android:text="我跟上级左边对齐"
  38. android:textSize="11sp"
  39. android:textColor="#000000" />
  40. <TextView
  41. android:id="@+id/tv_parent_right"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:layout_alignParentRight="true"
  45. android:background="#eeeeee"
  46. android:text="我跟上级右边对齐"
  47. android:textSize="11sp"
  48. android:textColor="#000000" />
  49. <TextView
  50. android:id="@+id/tv_parent_top"
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:layout_alignParentTop="true"
  54. android:background="#eeeeee"
  55. android:text="我跟上级顶部对齐"
  56. android:textSize="11sp"
  57. android:textColor="#000000" />
  58. <TextView
  59. android:id="@+id/tv_parent_bottom"
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:layout_alignParentBottom="true"
  63. android:background="#eeeeee"
  64. android:text="我跟上级底部对齐"
  65. android:textSize="11sp"
  66. android:textColor="#000000" />
  67. <TextView
  68. android:id="@+id/tv_left_center"
  69. android:layout_width="wrap_content"
  70. android:layout_height="wrap_content"
  71. android:layout_toLeftOf="@+id/tv_center"
  72. android:layout_alignTop="@+id/tv_center"
  73. android:background="#eeeeee"
  74. android:text="我在中间左边"
  75. android:textSize="11sp"
  76. android:textColor="#000000" />
  77. <TextView
  78. android:id="@+id/tv_right_center"
  79. android:layout_width="wrap_content"
  80. android:layout_height="wrap_content"
  81. android:layout_toRightOf="@+id/tv_center"
  82. android:layout_alignBottom="@+id/tv_center"
  83. android:background="#eeeeee"
  84. android:text="我在中间右边"
  85. android:textSize="11sp"
  86. android:textColor="#000000" />
  87. <TextView
  88. android:id="@+id/tv_above_center"
  89. android:layout_width="wrap_content"
  90. android:layout_height="wrap_content"
  91. android:layout_above="@+id/tv_center"
  92. android:layout_alignLeft="@+id/tv_center"
  93. android:background="#eeeeee"
  94. android:text="我在中间上面"
  95. android:textSize="11sp"
  96. android:textColor="#000000" />
  97. <TextView
  98. android:id="@+id/tv_below_center"
  99. android:layout_width="wrap_content"
  100. android:layout_height="wrap_content"
  101. android:layout_below="@+id/tv_center"
  102. android:layout_alignRight="@+id/tv_center"
  103. android:background="#eeeeee"
  104. android:text="我在中间下面"
  105. android:textSize="11sp"
  106. android:textColor="#000000" />
  107. </RelativeLayout>

上述XML文件的布局效果如下图所示,RelativeLayout的下级视图都是文本视图,控件上的文字说明 了所处的相对位置,具体的控件显示方位正如XML属性中描述的那样。

Java程序如下:

  1. package com.example.chapter03;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. public class RelativeLayoutActivity extends AppCompatActivity {
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_relative_layout);
  9. }
  10. }
标签: android 学习

本文转载自: https://blog.csdn.net/qq_32907491/article/details/132264030
版权归原作者 星如雨グッ!(๑•̀ㅂ•́)و✧ 所有, 如有侵权,请联系我们删除。

“Android学习之路(3) 布局”的评论:

还没有评论