android中常用的布局管理器(二)
接上篇博客
(3)LinearLayout 线性布局管理器
线性布局管理器是将放入其中的组件按照垂直或水平方向来布局,每一行或每一列只能放一个组件,并且不会换行,当组件排列到窗体的边缘后,后面的组件就不会显示出来。
常用属性:
常用属性:
android:orientation(horizontal 水平,vertical 垂直)
android:gravity(对齐方式) 设置组件的对齐位置
android:layout_witdth(宽) (默认为0,当输入一个大于0 的数字时,每个组件对父容器的剩余空间进行分割)
android:layout_height(高)
android:id
android:background(背景)
案列:
android:orientation="horizontal" 水平线性管理器
<?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="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮5" />
</>

android:orientation="vertical" 垂直水平管理器
<?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"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮5" />
</>

android:gravity="center" 设置组件的对其你方式
<?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="center"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮5" />
</>

android:layout_weight="1"
<?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="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"
android:layout_weight="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"
android:layout_weight="1"
/>
</>

(4)FrameLayout 帧布局管理器
帧布局(FrameLayout)在屏幕上开辟了一块区域,在这块区域中可以添加多个子控件,但是所有的子控件都会被对齐到屏幕的左上角。帧布局的大小由其下属子控件中尺寸最大的那个子控 件来控制。如果子控件的大小都一样,同一时刻只能看到最上面的子控件,其他的则被其遮挡(在进行选项卡设计时会用到帧布局)。
注意:在FrameLayout中,子控件是通过栈来绘制的,所以后添加的子控件会被绘制在上层。
FrameLayout继承自ViewGroup类,除了继承自父类的属性和方法,FrameLayout类中也包含了自己特有的一些属性和方法,见下表:
| 属性名称 | 对应方法 | 描述 |
| android:foreground | setForeground(Drawable) | 设置绘制在所有子控件之上的内容 |
| android:foregroundGravity | setForegroundGravity(int) | 设置绘制在所有子控件之上内容的gravity属性 |

浙公网安备 33010602011771号