为了更好的管理Android应用的用户界面中的组件,Android提供了布局管理器。经过使用布局管理器,Android应用的图形用户界面具备良好的平台无关性。一般,推荐使用布局管理器来管理组件的分布、大小,而不是直接设置组件位置和大小。例如,当设置一个文本框(TextView),为了让这个文本框在不一样的手机屏幕上都能良好的运行,手动的控制它的大小及位置将给编程带来巨大的困难,而使用布局管理器则能够解决这个问题,布局管理器能够根据运行平台来调整组件的大小,而程序员须要作的,仅仅是为容器选择合适的布局管理器。android
Android的布局管理器自己也是一个UI组件,Android中的全部布局管理器都是ViewGroup的子类。全部的布局管理器均可以做为容器类使用,所以能够调用多个重载的addView()向布局管理器中添加组件,咱们也能够在一个布局管理器中嵌套其余的布局管理器,由于布局管理器也继承了View,也可做为普通的UI组件使用。程序员
**GridLayout和RelativeLayout已被Android9标注为不推荐使用,推荐使用ConstraintLayout替代它们,编程
⒈LinearLayout(线性布局)app
线性布局由LinearLayout类来表明,线性布局有点像Swing编程里的Box,它们都会将容器里的组件一个挨着一个排列起来。LinearLayout能够控制各组件时横向排列仍是纵向排列(经过属性android:orientation控制)。ide
Android的线性布局不会换行,当组件一个挨着一个排列到头以后,剩下的组件将不会显示出来。布局
LinearLayout经常使用的XML属性及相关方法以下表:spa
XML属性 | 相关方法 | 说明 |
android:baselineAligned | setBaselineAligned(boolean) | 该属性设置为false,将会阻止该布局管理器与code 它的子元素的基线对齐xml |
android:divider | setDividerDrawable(Drawable) | 设置垂直布局时两个按钮之间的分割线 |
android:gravity | setGravity(int) | 设置布局管理器内组件的对齐方式。该属性blog 支持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>
⒉TableLayout(表格布局)