Frame Layout

来源:互联网 发布:淘宝差评被报复怎么办 编辑:程序博客网 时间:2024/05/16 16:11

FrameLayout


这种布局会为每个加入其中的组件创建一个空白的区域,即为一帧,每个子组件占据一帧。

FrameLayout包含的子元素受FrameLayout_LayoutParams控制,因此当我们往里面添加组件的时候,所有的组件都会放置于这块区域的左上角,而帧布局的大小由子控件中最大的子控件决定。

这个布局的属性很简单,下面举两个例子


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/ic_launcher"/>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="50sp"        android:text="@string/world_peace"/></FrameLayout>

效果图:

这里写图片描述


同时,我们也可以为组件添加layout_gravity属性,从而指定组件的对齐方式,如下可见。


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:width="170pt"        android:height="170pt"        android:background="#ff0"/>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:width="140pt"        android:height="140pt"        android:background="#f0f"/>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:width="110pt"        android:height="110pt"        android:background="#0ff"/>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:width="80pt"        android:height="80pt"        android:background="#f00"/>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:width="50pt"        android:height="50pt"        android:background="#0f0"/>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:width="20pt"        android:height="20pt"        android:background="#00f"/></FrameLayout>

效果图

这里写图片描述


这里将所有的TextView的layout_gravity属性设置为center。并且将后添加的图片宽高逐渐减小,避免了最先添加的TextView被完全遮挡。


知识点参考自Android官方文档及《疯狂Android讲义》/李刚

0 0
原创粉丝点击