当前位置:操作系统 > 安卓/Android >>

Android 程式开发:(五)屏幕组件 —— 5.6 FrameLayout帧布局

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

 
  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)。代码:

 
  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>  
最终效果图:

\


摘自 manoel的专栏
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,