基本布局之 - FrameLayout

FrameLayout是Android四种基本布局之一,其默认将控件置于左上角。可以通过layout_gravity属性来设定控件的对齐方式,如居左或居右。尽管定位方式有限,但仍有特定场景的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载请注明出处amoscxy的博客:https://mp.youkuaiyun.com/mdeditor/80149067

基本布局之 - FrameLayout

FrameLayout又称作帧布局,这种布局没有方便的定位方式,所有控件都会默认放在布局左上角

1.1 默认效果

  • 最终视图
    这里写图片描述

  • 实例目录
    这里写图片描述

  • 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:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is TextView"/>

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>

</FrameLayout>

可以看到文字和图片都位于布局左上角,由于ImageView是在TextView之后添加的,因此图片压在了文字的上面

1.2 使用layout_gravity属性来指定控件在布局中的对齐方式

我们还可以使用layout_gravity属性来指定控件在布局中的对齐方式,这和LinearLayout中的用法是相似的

  • 最终效果
    这里写图片描述

  • 修改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:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:text="This is TextView"/>

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:src="@mipmap/ic_launcher"/>

</FrameLayout>

我们指定TextView在FrameLayout中居左对齐,指定ImageView在FrameLayout中居右对齐

总的来讲,FrameLayout由于定位方式的缺失,导致它的应用场景比较少

转载请注明出处amoscxy的博客:https://mp.youkuaiyun.com/mdeditor/80149067

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值