Android FrameLayout布局

FrameLayout  

FrameLayout 又称单帧布局,是 Android 所提供的布局方式里最简单的布局方式,它指定屏幕上的一块空白区域,在该区域填充一个单一对象。例如图片、文字、按钮等。

应用程序开发人员不能为 FrameLayout 中填充的组件指定具体填充位置,默认情况下,这些组件都将被固定在屏幕的左上角,后放入的组件会放在前一个组件上进行覆盖填充,形成部分遮挡或全部遮挡。

开发人员可以通过组件的 android:layout_gravity 属性对组件位置进行适当的修改。

实例 FrameLayoutDemo 演示了 FrameLayout 的布局效果。该布局中共有 4 个 TextView 组件,前 3 个组件以默认方式放置到布局中,第 4 个组件修改 gravity 属性后放置到布局中,运行效果如图 所示。

 

实例 FrameLayoutDemo 中的布局文件 main.xml 的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!--顶部的轮播-->
    <com.youth.banner.Banner
        android:id="@+id/banner_home_header"
        android:layout_width="match_parent"
        android:layout_height="240dp"/>

    <!--轮播下的布局-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!--功能区-->
        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="@drawable/bg_layout_border">
        </RadioGroup>

        <!--入住 离店日期选择-->
        <LinearLayout
            android:id="@+id/ll_home_date_select"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_layout_border"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <TextView
                android:id="@+id/tv_home_total_night"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="n" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="1" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

其中:

 

android:layout_width="wrap_content"
android:layout_height="wrap_content"

 

表明 FrameLayout 布局覆盖了整个屏幕空间。

实例 FrameLayoutDemo 中的 strings.xml 文件内容如下:

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<string name="app_name">FrameLayoutDemo</string>
<string name="first">第一层</string>
<string name="second">第二层</string>
<string name="third">第三层</string>
<string name="forth">第四层</string>
</resources>

 

从运行后的结果可见,前 3 个被放置到 FrameLayout 的 TextView 组件都是以屏幕左上角为基点进行叠加的。第4个 TextView 因为设置了 android:layout_gravity="bottom" 属性而显示到了布局的下方。可自行将 android:layout_gravity 属性值修改为其他属性,查看运行效果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<h3>回答1:</h3><br/>Android中的FrameLayout布局是一种简单的布局方式,它允许在同一屏幕上放置多个控件,这些控件可以重叠在一起。FrameLayout布局的特点是:控件可以放置在任何位置,但是它们的大小和位置都是相对于FrameLayout的大小和位置而言的。在FrameLayout中,后添加的控件会覆盖先添加的控件。因此,FrameLayout常用于实现一些简单的界面效果,如图片轮播、广告展示等。 <h3>回答2:</h3><br/>Android中的Framelayout布局是从根据子视图的层次结构绘制学习图像布局。与其他布局不同,它允许多个子视图重叠在同一个位置上。Framelayout布局通常用于包含多个视图的复杂用户界面,其中每个视图叠加到下一个视图上。一个常见的例子是使用Framelayout布局创建引导屏幕和体验界面。 Framelayout布局还可以在屏幕的不同部分显示不同的内容。例如,如果你想在一个页面上显示一个照片和一个文本框,你可以使用Framelayout布局将照片放在顶部,然后将文本框放在底部,从而将两者分别放在屏幕的顶部和底部。 Framelayout布局非常适合在Android应用中使用,因为它允许不同的视图叠加在一起,从而创建优美且动态的用户界面。它也非常灵活,可以与其他布局组合使用,例如linearlayout和relativelayout,以创建更复杂的UI。 使用Framelayout布局时,我们需要注意重叠的顺序,确保每个子视图的可见性,以及确保每个子视图都有正确的大小和位置。我非常喜欢使用Framelayout布局来构建Android应用的复杂用户界面,它为我们提供了一个强大而灵活的工具,使我们能够创建动态而美丽的UI。 <h3>回答3:</h3><br/>Android布局通常由LinearLayout,RelativeLayout,GridLayout和FrameLayout组成。其中,FrameLayout是最常用的布局之一。FrameLayout是一个布局类,允许您将多个子视图放置在一个单独的视图中,并使用简单的轻量级方式来控制它们的位置和大小。 FrameLayout是一个非常简单的布局,它只是在屏幕上按照子视图排列,没有其他布局,它的根元素是FrameLayoutFrameLayout的特点: 1. FrameLayout可以让我们在同一个屏幕上用多个视图叠放在一起。 2. FrameLayout只会使用最上层的视图,也就是覆盖上面的视图。 3. FrameLayout的每一个子视图都是按照原始大小排列在屏幕上的,并且它们的位置是相对于FrameLayout的左上角而言的。 4. FrameLayout适用于一些小组件布局,比如“确定”和“取消”按钮、气泡等。 FrameLayout的使用方法: 1. 在Activity的XML文件中,建立一个FrameLayout标签。 2. 在FrameLayout标签中添加子视图。 3. 使用android:layout_gravity属性设置子视图的位置和对齐方式。 例如:如果您想让一个ImageView占据整个屏幕并覆盖其他视图,则可以使用如下代码实现: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/background_image" /> </FrameLayout> 上述代码创建了一个FrameLayout,并在其中包含了一个ImageView视图。ImageView的布局宽度和高度都设置为match_parent,这意味着它将占据整个屏幕的宽度和高度。因此,既可以显示“background_image”图片,也可以覆盖其他视图。 总之,FrameLayout是一种非常常用的布局,在Android应用程序中可以发挥重要作用。您可以使用它的轻量级方式进行布局,以使您的应用程序更加高效、灵活和简单。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值