Android布局詳解之一:FrameLayout


 
原創文章,如有轉載,請注明出處: http://blog.csdn.net/yihui823/article/details/6702273

 

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:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:textSize="50dip"

    android:textColor="#ffffff"

    android:text="第一層"/>

<TextView 

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:textSize="40dip"

    android:textColor="#ffff00"

    android:text="第二層"/>

<TextView 

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:textSize="30dip"

    android:textColor="#ff00ff"

    android:text="第三層"/>

<TextView 

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:textSize="20dip"

    android:textColor="#00ffff"

    android:text="第四層"/>

</FrameLayout>

 

效果如下圖:layoutpic001

 

變化1

我們現在來嘗試改變一下他們的位置。把第一個和第二個文本框改成:

<TextView 

    android:id="@+id/tv1"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:textSize="50dip"

    android:textColor="#ffffff"

    android:text="第一層"/>

<TextView 

    android:id="@+id/tv2"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:textSize="40dip"

    android:textColor="#ffff00"

android:layout_toRightOf="@id/tv1"

android:text="第二層"/>

也就是說,讓第二個文本框放在第一個文本框的右邊。我們來看看效果。看到了沒?還是一樣的不變吧。

變化2

我們來嘗試下android:gravity屬性。把第三個文本框改成:

<TextView 

    android:id="@+id/tv3"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:textSize="30dip"

    android:textColor="#ff00ff"

    android:gravity="right"

android:text="第三層"/>

看看效果如何?天哪!竟然沒有覆蓋,而是錯開了!!!

layoutpic002

首先呢,我們不要大驚小怪。這個現象並不說明FrameLayout失效了。gravity屬性,是控制控件內部文本的格式的。而我們看我們控件的寬的屬性是什么?是“fill_parent”,也就是說,我們文本框的寬度就是屏幕的寬度。那么android:gravity="right"文本靠右,而文本框本身還是左上堆疊在一起的。不信,我們再來改改:

<TextView 

    android:id="@+id/tv3"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:textSize="30dip"

    android:textColor="#ff00ff"

    android:gravity="right"

android:text="第三層"/>

我們讓第三個文本框的寬度自適應,也就是保證顯示全文字即可。這個時候看一下效果呢?是不是打回原形啦?哈哈哈。

變化3

我們再來試試” android:layout_centerVertical”屬性。把第四個文本框改成:

<TextView 

    android:id="@+id/tv4"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:textSize="20dip"

    android:textColor="#00ffff"

    android:layout_centerVertical="true"

android:text="第四層"/>

效果如何?沒任何效果!

 

總結一下,經過以上的3個實驗,我們知道FrameLayout根本無法控制他的子控件的位置。所有的控件都是左上對其。但是控件本身是可以控制自 己內部的布局的。所以利用透明,也是可以完成一些簡單的功能的。例如屏幕四個角各顯示一些文字(是顯示文字,沒法放置控件)。因為每一層覆蓋下一層的時 候,如果用透明背景,則下一層不會被背景覆蓋。

什么是透明背景?這個……說來話長啦。偷個懶,下次寫一下透明的處理。

是不是有人會問,這么簡單的Layout有什么用?我想還是有它存在的價值的。

當你需要自己寫一個View的時候,在View里面已經完成了你的邏輯(例如游戲^_^),那么這個View只需要一個容器放置,就可以使用FrameLayout了。雖然用其他的布局也可以,但是用最簡單的不是更省系統資源么。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM