Android UI之FrameLayout(幀布局)

來源:互聯網
上載者:User

標籤:android   ui   布局   framelayou   

Android UI之FrameLayout(幀布局)

說明:幀布局會為每個包含其中的組件開闢一個空白地區(稱為幀),這些幀是一層層疊加在一起的,有點類似於一層層覆蓋貼上去的海報,後面的組件會把前面的組件覆蓋住。

FrameLayout有兩個比較特殊的常用屬性需要注意:

1 android:foreground

對應方法:setForeground(Drawable)
說明:設定幀布局的前景映像,一般為布局添加pressed狀態會用到這個屬性來指定一個Drawable類型對象。
舉個栗子:

<FrameLayout    android:foreground="@drawable/muogu"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:width="320dp"        android:height="320dp"        android:layout_gravity="center"        android:background="#3399AA" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:width="280dp"        android:height="280dp"        android:layout_gravity="center"        android:background="#AA77AA" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:width="240dp"        android:height="240dp"        android:layout_gravity="center"        android:background="#662288" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:width="200dp"        android:height="200dp"        android:layout_gravity="center"        android:background="#895511" />    <ImageView        android:layout_width="160dp"        android:layout_height="160dp"        android:layout_gravity="center"        android:scaleType="fitXY"        android:src="@drawable/nailiu16" /></FrameLayout>

可以看到設定了foreground為小蘑菇的圖片之後,圖片直接展開佔據滿了整個FrameLayout,並將FrameLayout中所有的組件都遮蓋住了。這就是相對與背景色的前景色彩的效果。

2 foregroundGravity

對應方法:setForegroundGravity(int)
說明:看名字就知道,肯定是跟前景色彩擺放有關。沒錯,這個屬性的功能就是定義前景映像的gravity屬性,所以這個屬性必須配合foreground使用。而其中的可選項更gravity是一樣的,可以在我之前的《 Android UI之LinearLayout(線性布局)》中找到屬性工作表。
舉個栗子:

<FrameLayout    android:foreground="@drawable/muogu"    android:layout_width="fill_parent"    android:foregroundGravity="bottom|right"    android:layout_height="fill_parent">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:width="320dp"        android:height="320dp"        android:layout_gravity="center"        android:background="#3399AA" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:width="280dp"        android:height="280dp"        android:layout_gravity="center"        android:background="#AA77AA" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:width="240dp"        android:height="240dp"        android:layout_gravity="center"        android:background="#662288" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:width="200dp"        android:height="200dp"        android:layout_gravity="center"        android:background="#895511" />    <ImageView        android:layout_width="160dp"        android:layout_height="160dp"        android:layout_gravity="center"        android:scaleType="fitXY"        android:src="@drawable/nailiu16" /></FrameLayout>

這裡的代碼更上邊的唯一區別就是在FrameLayout中添加了下面這行代碼。

android:foregroundGravity="bottom|right"

可以看到前景映像還原到了原有大小,而且按照設定放到了右下角,這樣,FrameLayout中的子組件就顯示出來了。

附:引用聲明

《瘋狂Android講義(第二版)》 李剛 《2.2.3 幀布局》 電子工業出版社

Android UI之FrameLayout(幀布局)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.