FrameLayout在屏幕上開辟了一塊區域,在這塊區域中可以添加多個子控件,但是所有的子控件都會被對齊到屏幕的左上角。FrameLayout的大小由其所添加的所有子控件中尺寸最大的那個子控件來控制。如果子控件的大小都一樣,同一時刻只能看到最上面的子控件,其他的則被其遮擋(在進行選項卡設計時會用到幀布局)。在FrameLayout中,子控件是通過棧來繪制的,所以后添加的子控件會被繪制在上層。
示例代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "short text"/>
<TextView android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor = "#0000FF"
android:textSize="40px"
android:text = "This is a long text ,long text"/>
</FrameLayout>
結果顯示如下:
FrameLayout:
FrameLayout是最簡單的一個布局對象。它被定制為你屏幕上的一個空白備用區域,之后你可以在其中填充一個單一對象 — 比如,一張你要發布的圖片。所有的子元素將會固定在屏幕的左上角;你不能為FrameLayout中的一個子元素指定一個位置。后一個子元素將會直接在前一個子元素之上進行覆蓋填充,把它們部份或全部擋住(除非后一個子元素是透明的)。
我們看一下效果圖:
其中Main.xml 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 我們在這里加了一個Button按鈕 -->
<Button
android:text="button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="textview"
android:textColor="#0000ff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</FrameLayout>
只有上的widget會覆蓋下面的 上面的Layout的空白處是不會覆蓋的。即使你改變他的背景顏色也是一樣的
對於點擊的觸發事件
最上面的widget會影響下面的觸發事件,但是如果將上面的widget設置為gone 那么就不會影響下面的觸發事件了
先來看官方文檔的定義:FrameLayout是最簡單的一個布局對象。它被定制為你屏幕上的一個空白備用區域,之后你可以在其中填充一個單一對象 — 比如,一張你要發布的圖片。所有的子元素將會固定在屏幕的左上角;你不能為FrameLayout中的一個子元素指定一個位置。后一個子元素將會直接在前一個子元素之上進行覆蓋填充,把它們部份或全部擋住(除非后一個子元素是透明的)。
簡單來說:FrameLayout中的子元素總是以屏幕的左上角層疊在一起。
事實上,這是不確切的,我們可以對子元素添加android:layout_gravity屬性來設置他們的位置的。
比如,下面的布局子控件都在什么位置呢?
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <ImageView
- android:id="@+id/image"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:scaleType="center"
- android:src="@drawable/candle"
- />
- <TextView
- android:id="@+id/text1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:textColor="#00ff00"
- android:text="@string/hello"
- />
- <Button
- android:id="@+id/start"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="bottom"
- android:text="Start"
- />
- </FrameLayout>
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="center" android:src="@drawable/candle" /> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="#00ff00" android:text="@string/hello" /> <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:text="Start" /> </FrameLayout>
在FrameLayout布局里面android:layout_margin的各種屬性必須依賴於android:layout_gravity,也就是說,要想margin生效,必須設定view的layout_gravity屬性。