【Android 开发教程】FrameLayout帧布局

本文介绍了FrameLayout的基本用法,包括如何在一个FrameLayout中嵌套多个视图,并解释了这些视图如何在屏幕上重叠显示。

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

本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


FrameLayout就是屏幕上的一个“定位器”,可以使用它去显示一个单一的视图。被添加到FrameLayout上的视图views总是被固定在这个布局的左上角。考虑以下的代码:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/RLayout"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/lblComments"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:text="Hello, Android!" />  
  14.   
  15.     <FrameLayout  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_alignLeft="@+id/lblComments"  
  19.         android:layout_below="@+id/lblComments"  
  20.         android:layout_centerHorizontal="true" >  
  21.   
  22.         <ImageView  
  23.             android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content"  
  25.             android:src="@drawable/droid" >  
  26.         </ImageView>  
  27.     </FrameLayout>  
  28.   
  29. </RelativeLayout>  
这里,在RelativeLayout中内嵌了一个FrameLayuout,在FrameLayuout中内嵌了一个ImageView。效果图:

但是,如果想要在这个FrameLayuout中添加另外的view(比如一个Button),那么这个view就会重叠在“之前的”view上面(本例中是显示图片的ImageView)。代码:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/RLayout"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/lblComments"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:text="Hello, Android!" />  
  14.   
  15.     <FrameLayout  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_alignLeft="@+id/lblComments"  
  19.         android:layout_below="@+id/lblComments"  
  20.         android:layout_centerHorizontal="true" >  
  21.   
  22.         <ImageView  
  23.             android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content"  
  25.             android:src="@drawable/droid" >  
  26.         </ImageView>  
  27.   
  28.         <Button  
  29.             android:layout_width="124dp"  
  30.             android:layout_height="wrap_content"  
  31.             android:text="Print Picture" />  
  32.     </FrameLayout>  
  33.   
  34. </RelativeLayout>  
最终效果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值