Android UI组件:布局管理器

时间:2023-03-08 23:03:33
Android UI组件:布局管理器

  为了更好的管理Android应用的用户界面中的组件,Android提供了布局管理器。通过使用布局管理器,Android应用的图形用户界面具有良好的平台无关性。通常,推荐使用布局管理器来管理组件的分布、大小,而不是直接设置组件位置和大小。例如,当设置一个文本框(TextView),为了让这个文本框在不同的手机屏幕上都能良好的运行,手动的控制它的大小及位置将给编程带来巨大的困难,而使用布局管理器则可以解决这个问题,布局管理器可以根据运行平台来调整组件的大小,而程序员需要做的,仅仅是为容器选择合适的布局管理器。

  Android的布局管理器本身也是一个UI组件,Android中的所有布局管理器都是ViewGroup的子类。所有的布局管理器都可以作为容器类使用,因此可以调用多个重载的addView()向布局管理器中添加组件,我们也可以在一个布局管理器中嵌套其他的布局管理器,因为布局管理器也继承了View,也可作为普通的UI组件使用。

  **GridLayout和RelativeLayout已被Android9标注为不推荐使用,推荐使用ConstraintLayout替代它们,

⒈LinearLayout(线性布局)

  线性布局由LinearLayout类来代表,线性布局有点像Swing编程里的Box,它们都会将容器里的组件一个挨着一个排列起来。LinearLayout可以控制各组件时横向排列还是纵向排列(通过属性android:orientation控制)。

  Android的线性布局不会换行,当组件一个挨着一个排列到头之后,剩下的组件将不会显示出来。

  LinearLayout常用的XML属性及相关方法如下表:

XML属性 相关方法 说明
android:baselineAligned setBaselineAligned(boolean)

该属性设置为false,将会阻止该布局管理器与

它的子元素的基线对齐

android:divider setDividerDrawable(Drawable) 设置垂直布局时两个按钮之间的分割线
android:gravity setGravity(int)

设置布局管理器内组件的对齐方式。该属性

支持top、bottom、left、right、center_vertical、

fill_vertical、center_horizontal、fill_horizontal、

center、fill、clip_vertical、clip_horizontal几个属性值。

也可以同时指定多种对齐方式的组合。例如left|center_vertical

代表出现在屏幕左边而且垂直居中。

android:measureWithLargestChild setMeasureWithLargestChildEnabled(boolean)

当该属性设置为true时,所有带权重的子元素都会具有

最大子元素的最小尺寸

android:orientation setOrientation(int)

设置布局管理器内组件的排列方式,可以设置为horizontal

(水平排列)、vertical(垂直排列,默认值)两个值的

其中之一

android:weightSum   设置该布局管理器的最大权值和

  LinearLayout包含的所有子元素都受LinearLayout.LayoutParams控制,因此LinearLayout包含的子元素可以额外指定如下属性。

XML属性 相关方法 说明
android:layout_gravity   指定该子元素在LinearLayout中的对齐方式
android:layout_weight   指定该子元素在LinearLayout中所占的权重

  **基本上很多布局管理器都提供了相应的LayoutParams内部类,该内部类用于控制它们的子元素支持指定android:layout_gravity属性,该属性设置该子元素在父容器中的对齐方式。与android:layout_gravity相似的属性还有android:gravity属性(一般容器才支持指定该属性),该属性用于控制它所包含的子元素的对齐方式。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="bottom|center_horizontal"
tools:context=".MainActivity"> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" /> <Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" /> <Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" /> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>

** @+id 是指创建一个id的意思

⒉TableLayout(表格布局)