前面学习了线性布局、相对布局、表格布局,那么本期来学习第四种布局——FrameLayout帧布局。
一、认识FrameLayout
帧布局是Android布局中最简单的一种,使用FrameLayout标签。
帧布局为每个加入其中的控件创建一个空白区域(称为一帧,每个控件占据一 帧)。釆用帧布局方式设计界面时,只能在屏幕左上角显示一个控件,如果添加多个控件,这些控件会按照顺序在屏幕的左上角重叠显示。
下表显示了 FrameLayout常用的XML属性及相关方法说明。
FrameLayout包含的子元素也受FrameLayout.LayoutParams控制,因此它所包含的子元素也可指定android:layout_gravity属性,该属性控制该子元素在FrameLayout中的对齐方式。
二、示例
接下来通过一个简单的示例程序来学习FrameLayout的使用用法。
同样使用WidgetSample工程,继续使用app/main/res/layout/目录下的activity_main.xml文件,在其中填充如下代码片段:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:background="#FF33ffff" />
<TextView
android:layout_width="240dp"
android:layout_height="240dp"
android:layout_gravity="center"
android:background="#FF33ccff" />
<TextView
android:layout_width="180dp"
android:layout_height="180dp"