Android(java)學習筆記93:Android布局詳解之一:FrameLayout

來源:互聯網
上載者:User

標籤:

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了。雖然用其他的布局也可以,但是用最簡單的不是更省系統資源麼。

 

 

 

Android(java)學習筆記93:Android布局詳解之一: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.