FrameLayout幀佈局:
一 功能:
(1)在這個佈局中,所有的子元素都不能被指定放置的位置(不能指定某個空間或子佈局的位置),他們統統放於這塊區域的左上角,並且後面的子元素直接覆蓋在前面的子元素之上,將前面的子元素部分和全部遮擋。
(2)正因如此,所以FrameLayout佈局沒有像 android:gravity=""(指定該控件或佈局 內容的位置)這樣的屬性。所以通常使用了此佈局,並且想要改變此佈局裏面的子控件或子佈局的位置時,只能在子控件或子佈局中使用 android:layout_gravity=""屬性。
(3)在FreamLayout中,子控件是通過棧來繪製的,所以後添加的子控件會被控制在上層。
二 練習 :直接做以下練習
代碼如下
<?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/textView1"
android:layout_width="300dp"
android:layout_height="300dp"
android:gravity="right|bottom"
android:background="#456893"
android:textSize="60dp"
android:text="one" />
<TextView
android:id="@+id/textview2"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#879546"
android:gravity="right|bottom"
android:textSize="40dp"
android:text="two"/>
<TextView
android:id="@+id/textView3"
android:layout_width="130dp"
android:layout_height="130dp"
android:background="#635984"
android:gravity="bottom|right"
android:textSize="30dp"
android:text="three" />
<TextView
android:id="@+id/textView4"
android:layout_width="80dp"
android:layout_height="80dp"
android:gravity="center"
android:background="#987456"
android:textSize="20dp"
android:text="thrid"/>
</FrameLayout>
例如常見的一個進度條
<img src="https://img-blog.csdn.net/20151020184647505?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
示例代碼:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/textview"/>
</FrameLayout>
三:特有屬性
android:foreground="" 設置繪製在所有子控件之上的內容
android:foregroundGravity="" 設置繪製在所有子控件之上內容的gravity屬性
例如下圖:設置一個前景圖,並且讓它的位於右側中間的位置
如圖可知android:foreground="" 的意思就是把你所選的內容(我這裏選了一張圖片)覆蓋在所有子控件之上
示例代碼:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="@drawable/ac"
android:foregroundGravity="right|center_vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="300dp"
android:layout_height="300dp"
android:gravity="right|bottom"
android:background="#456893"
android:layout_gravity="center"
android:textSize="60dp"
android:text="one" />
<TextView
android:id="@+id/textview2"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#879546"
android:gravity="right|bottom"
android:layout_gravity="center"
android:textSize="40dp"
android:text="two"/>
<TextView
android:id="@+id/textView3"
android:layout_width="130dp"
android:layout_height="130dp"
android:background="#635984"
android:gravity="bottom|right"
android:layout_gravity="center"
android:textSize="30dp"
android:text="three" />
<TextView
android:id="@+id/textView4"
android:layout_width="60dp"
android:layout_height="60dp"
android:gravity="center"
android:layout_gravity="center"
android:background="#987456"
android:textSize="20dp"
android:text="thrid"/>
</FrameLayout>