標籤: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(幀布局)