FrameLayout 初体验

最近学习了android的布局样式标签,布局样式标签一共有5个,其中FrameLayout(单帧布局)是最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象 — 比如,一张你要发布的图片。所有的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。后一个子元素将会直接在前一个子元素之上进行覆盖填充,把它们部份或全部挡住(除非后一个子元素是透明的),可以用include标签载入定义的另一个layout文件,现在用一个ProgressBar的例子学习一下FrameLayout。


首先,我们创建一个布局文件,
res/layout/fullscreen_loading.xml, 其内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:gravity="center_vertical|center_horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent  
    android:id="@+id/fullscreen_loading"
    android:visibility="gone"
    >
    <ProgressBar
        android:layout_gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleSmall"
        >
    </ProgressBar>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5.0dip"
        android:text="loading..."
        >
    </TextView>
</LinearLayout>
然后在main.xml 把它include 进来
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
        android:orientation="vertical"
        android:visibility="gone"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    </LinearLayout>
    <include
        android:visibility="visible"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        layout="@layout/fullscreen_loading"
        >
    </include>
</FrameLayout>

主程序 LoadingActivity.java:
package com.google.android.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;

public class LoadingActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
运行的效果为:
FrameLayoutAndroid 中的一个布局容器,它可以用来放置一个或多个子 View,子 View 会按照添加的顺序依次叠放在 FrameLayout 中,并且默认情况下每个子 View 的大小都是填满整个 FrameLayoutFrameLayout 最常见的用法是用来放置一个单独的 View,例如一个 ImageView,通常还会设置 ImageView 的 scaleType 属性来控制图片的显示方式。另外,FrameLayout 也可以用来实现一些特殊的布局效果,例如重叠的 View 或者前景和背景的切换等。 下面是一个简单的 FrameLayout 的示例代码: ```xml <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/image" android:scaleType="centerCrop"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:textSize="24sp" android:textColor="#FFFFFF"/> </FrameLayout> ``` 在这个示例中,FrameLayout 包含了一个 ImageView 和一个 TextView,ImageView 用来显示一张图片,TextView 用来显示一段文本。由于 ImageView 和 TextView 都没有设置位置和大小,所以它们默认会叠放在一起,并且都填满整个 FrameLayout。 需要注意的是,由于 FrameLayout 中的子 View 是按照添加的顺序依次叠放的,所以后添加的子 View 会覆盖前面添加的子 View。如果需要调整子 View 的叠放顺序,可以使用 View 的 bringToFront() 方法或者 setZ() 方法来实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值