移动应用开发——FrameLayout(帧布局)的基本使用

本文介绍了Android开发中FrameLayout的基础使用,强调其简单的特性,指出所有控件默认位于左上角。通过示例展示了如何利用android:layout_gravity属性调整控件在布局中的对齐方式,以改变默认堆叠顺序。

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

FrameLayout又称作帧布局,它相比于LinearLayout和RelativeLayout要简单很多,因为它的应用场景也少了很多。这种布局没有方便的定位方式,所有的控件都会默认摆放在布局的左上角
在这里插入图片描述
activity_main代码:

<?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/tv"  
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"  
           android:text="This is TextView" />   
         <ImageView     
                android:id="@+id/iv" 
                android:layout_width="wrap_content"             
                android:layout_height="wrap_content"    
                android:src="@mipmap/ic_launcher" /> 
                   </FrameLayout>

效果图:
在这里插入图片描述
可以看到,文字和图片都是位于布局的左上角。由于ImageView是在TextView之后添加的,因此图片压在了文字的上面。
当然除了这种默认效果之外,我们还可以使用android: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/tv"     
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"    
        android:layout_gravity="left"      
        android:text="This is TextView" />
        <ImageView      
                  android:id="@+id/iv"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"     
                  android:layout_gravity="right"    
                  android:src="@mipmap/ic_launcher" /> 
                         </FrameLayout>

效果图:
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值