使用FrameLayout应该注意的地方

本文通过示例XML展示了FrameLayout的用法,强调了如何设置组件的layout_gravity属性来控制它们在布局中的位置。FrameLayout适用于叠加组件,如ImageView、TextView和Button,并在屏幕上展示。
摘要由CSDN通过智能技术生成
先来看官方文档的定义:FrameLayout是最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象 — 比如,一张你要发布的图片。所有的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。后一个子元素将会直接在前一个子元素之上进行覆盖填充,把它们部份或全部挡住(除非后一个子元素是透明的)。 
简单来说:FrameLayout中的子元素总是以屏幕的左上角层叠在一起。 
事实上,这是不确切的,我们可以对子元素添加android:layout_gravity属性来设置他们的位置的。 
比如,下面的布局子控件都在什么位置呢? 
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2.  <FrameLayout   
  3.    xmlns:android="http://schemas.android.com/apk/res/android"   
  4.    android:layout_width="fill_parent"   
  5.    android:layout_height="fill_parent" >   
  6.    
  7.      <ImageView   
  8.          android:id="@+id/image"   
  9.          android:layout_width="fill_parent"    
  10.          android:layout_height="fill_parent"   
  11.          android:scaleType="center"   
  12.          android:src="@drawable/candle"   
  13.          />   
  14.      <TextView   
  15.          android:id="@+id/text1"   
  16.          android:layout_width="wrap_content"   
  17.          android:layout_height="wrap_content"   
  18.          android:layout_gravity="center"   
  19.          android:textColor="#00ff00"   
  20.          android:text="@string/hello"   
  21.          />   
  22.      <Button   
  23.          android:id="@+id/start"   
  24.          android:layout_width="wrap_content"   
  25.          android:layout_height="wrap_content"   
  26.          android:layout_gravity="bottom"   
  27.          android:text="Start"   
  28.          />   
  29.  </FrameLayout>  
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2.  <FrameLayout   
  3.    xmlns:android="http://schemas.android.com/apk/res/android"   
  4.    android:layout_width="fill_parent"   
  5.    android:layout_height="fill_parent" >   
  6.    
  7.      <ImageView   
  8.          android:id="@+id/image"   
  9.          android:layout_width="fill_parent"    
  10.          android:layout_height="fill_parent"   
  11.          android:scaleType="center"   
  12.          android:src="@drawable/candle"   
  13.          />   
  14.      <TextView   
  15.          android:id="@+id/text1"   
  16.          android:layout_width="wrap_content"   
  17.          android:layout_height="wrap_content"   
  18.          android:layout_gravity="center"   
  19.          android:textColor="#00ff00"   
  20.          android:text="@string/hello"   
  21.          />   
  22.      <Button   
  23.          android:id="@+id/start"   
  24.          android:layout_width="wrap_content"   
  25.          android:layout_height="wrap_content"   
  26.          android:layout_gravity="bottom"   
  27.          android:text="Start"   
  28.          />   
  29.  </FrameLayout>  

在FrameLayout布局里面android:layout_margin的各种属性必须依赖于android:layout_gravity,也就是说,要想margin生效,必须设定view的layout_gravity属性。 
下面的配置将2个控件显示在屏幕的中间: 
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     >  
  6.     <LinearLayout  
  7.     android:layout_width="200dip"  
  8.     android:layout_height="wrap_content"  
  9.     android:orientation="vertical"   
  10.     android:gravity="center"  
  11.     android:layout_gravity="center"  
  12.     >  
  13.     <ImageView  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:src="@drawable/empty3"   
  17.         />  
  18.     <TextView  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_marginTop="10dp"  
  22.         android:text="暂无记录"  
  23.         android:textColor="@color/gray_dark"  
  24.         android:textSize="@dimen/font_middle"   
  25.         android:gravity="center"  
  26.         />  
  27.     </LinearLayout>  
  28. </FrameLayout>  
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     >  
  6.     <LinearLayout  
  7.     android:layout_width="200dip"  
  8.     android:layout_height="wrap_content"  
  9.     android:orientation="vertical"   
  10.     android:gravity="center"  
  11.     android:layout_gravity="center"  
  12.     >  
  13.     <ImageView  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:src="@drawable/empty3"   
  17.         />  
  18.     <TextView  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_marginTop="10dp"  
  22.         android:text="暂无记录"  
  23.         android:textColor="@color/gray_dark"  
  24.         android:textSize="@dimen/font_middle"   
  25.         android:gravity="center"  
  26.         />  
  27.     </LinearLayout>  
  28. </FrameLayout>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值