FrameLayout就是屏幕上的一個“定位器”,可以使用它去顯示一個單一的視圖。被添加到FrameLayout上的視圖views總是被固定在這個佈局的左上角。考慮以下的代碼:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/RLayout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <TextView
- android:id="@+id/lblComments"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:text="Hello, Android!" />
- <FrameLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/lblComments"
- android:layout_below="@+id/lblComments"
- android:layout_centerHorizontal="true" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/droid" >
- </ImageView>
- </FrameLayout>
- </RelativeLayout>
這裡,在RelativeLayout中內嵌瞭一個FrameLayuout,在FrameLayuout中內嵌瞭一個ImageView。效果圖:
但是,如果想要在這個FrameLayuout中添加另外的view(比如一個Button),那麼這個view就會重疊在“之前的”view上面(本例中是顯示圖片的ImageView)。代碼:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/RLayout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <TextView
- android:id="@+id/lblComments"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:text="Hello, Android!" />
- <FrameLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/lblComments"
- android:layout_below="@+id/lblComments"
- android:layout_centerHorizontal="true" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/droid" >
- </ImageView>
- <Button
- android:layout_width="124dp"
- android:layout_height="wrap_content"
- android:text="Print Picture" />
- </FrameLayout>
- </RelativeLayout>
最終效果圖:
摘自 manoel的專欄