幀布局(FrameLayout)說明:
幀布局是所有安卓布局中最簡單的一個布局,在這個布局中,整個界面被當成一塊空白備用區域,所有的子元素都不能被指定放置的位置,它們統統放于這塊區域的左上角,并且後面的子元素可以直接覆寫在前面的子元素上,将前面的子元素部分和全部遮擋。
示範效果圖(七彩虹):
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnL1MTY5IWM2E2MiZ2N0ImNhVzMzQDOmRTYzkjYyIGO2QzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/FrameLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
>
<TextView
android:id="@+id/TextView01"
android:layout_width="1000px"
android:layout_height="1000px"
android:background="#8A2BE2"
android:text="文本框1"
android:textColor="#ffffff"/>
<TextView
android:id="@+id/TextView02"
android:layout_width="900px"
android:layout_height="900px"
android:background="#0000FF"
/>
<TextView
android:id="@+id/TextView03"
android:layout_width="800px"
android:layout_height="800px"
android:background="#104E8B"
/>
<TextView
android:id="@+id/TextView04"
android:layout_width="700px"
android:layout_height="700px"
android:background="#32CD32"
/>
<TextView
android:id="@+id/TextView05"
android:layout_width="600px"
android:layout_height="600px"
android:background="#FFFF00"
/>
<TextView
android:id="@+id/TextView06"
android:layout_width="500px"
android:layout_height="500px"
android:background="#FFA500"
/>
<TextView
android:id="@+id/TextView07"
android:layout_width="400px"
android:layout_height="400px"
android:background="#ff0000"
/>
</FrameLayout>
FrameLayout作用和用途是什麼呢?
實踐中常選用FrameLayout作為容器,如果你想實作墨迹天氣那樣的拖動效果時,你會看到用的是FrameLayout。在FrameLayout中,用我看的書中的話說是,空間永遠用不完,實作拖動效果的關鍵原理就是framelayout使得幾部分空間的重疊。設定隻有一部分可見。當拖動時,設定其他部分移動