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的專欄