android FrameLayout詳解,androidframelayout
首先看示範:
FrameLayout架構布局是最簡單的布局形式。所有添加到這個布局中的視圖都以層疊的方式顯示。第一個添加的控制項被放在最底層,最後一個添加到架構布局中的視圖顯示在最頂層,上一層的控制項會覆蓋下一層的控制項。這種顯示方式有些類似於堆棧。
當我們往裡面添加組件的時候,所有的組件都會放置於這塊地區的左上方;
幀布局的大小由子控制項中最大的子控制項決定,如果都組件都一樣大的話,同一時刻就只能看到最上面的那個組件了。
1 layout_gravity
FrameLayout根本無法控制他的子控制項的位置,子控制項可以通過android:layout_gravity屬性來控制自己在父控制項中的位置,從而制定組件的對其方式。
2 layout_margin
FrameLayout布局裡面的控制項單獨設定layout_margin類的屬性沒有效果。FrameLayout中的控制項layout_margin設定要依賴layout_gravity屬性,否則layout_margin設定無效。layout_gravity有好幾個值可以設定,具體要設定哪一個呢?其實layout_gravity可以理解為設定控制項的參考點,控制項最終顯示位置最終由layout_gravity和layout_margin共同決定。
如果想要控制項正常顯示,可以將控制項的layout_gravity設定為top,以螢幕左上方為參考點。
3 前景映像:
永遠處於幀布局最頂的,直接面對使用者的映像,,就是不會被覆蓋的圖片
常用屬性:
android:foreground:設定該幀版面配置容器的前景映像
android:foregroundGravity:設定前景映像顯示的位置
幀布局在遊戲開發方面用的比較多。當你需要自己寫一個View的時候,在View裡面已經完成了你的邏輯(例如遊戲^_^),那麼這個View只需要一個容器放置,就可以使用FrameLayout了。雖然用其他的布局也可以,但是用最簡單的不是更省系統資源麼。
3應用執行個體
activity代碼
package mm.shandong.com.testframelayout;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.widget.Button;import android.widget.FrameLayout;public class TestFrameLayoutActivity extends AppCompatActivity { FrameLayout frameLayout; Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_frame_layout); btn = (Button) findViewById(R.id.btn); frameLayout = (FrameLayout) findViewById(R.id.frameLayout); frameLayout.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { int x = (int) motionEvent.getX(); int y = (int) motionEvent.getY(); int width = btn.getWidth(); int height = btn.getHeight(); btn.layout(x, y, x + width, y + height); return false; } }); }}
xml代碼
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="下面是一個FrameLayout,預設布局" /> <FrameLayout android:layout_width="match_parent" android:layout_height="80dp" android:background="#ff0000"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一層" android:textColor="#00ff00" android:textSize="55sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第二層" android:textColor="#0000ff" android:textSize="45sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第三層" android:textColor="#00ffff" android:textSize="35sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第四層" android:textColor="#ffff00" android:textSize="25sp" /> </FrameLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="下面是一個FrameLayout,通過layout_gravity和margin調節位置,並設定前景圖片和背景顏色" /> <FrameLayout android:layout_width="match_parent" android:layout_height="180dp" android:background="#ff0000" android:foreground="@drawable/red" android:foregroundGravity="right|bottom"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top|left" android:layout_marginLeft="20dp" android:layout_marginTop="0dp" android:text="左上 left:20 Top:0" android:textColor="#00ff00" android:textSize="15sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top|left" android:layout_marginLeft="20dp" android:layout_marginTop="25dp" android:text="左上 left:20 Top:25" android:textColor="#00aa00" android:textSize="15sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|left" android:layout_marginBottom="20dp" android:layout_marginLeft="45dp" android:text="左下 left:45 Bottom:20" android:textColor="#0000ff" android:textSize="15sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="置中" android:textColor="#00ffff" android:textSize="15sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right|top" android:layout_marginTop="30dp" android:text="右上 top 30" android:textColor="#ffff00" android:textSize="15sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right|bottom" android:layout_marginBottom="80dp" android:text="最上層,仍被遮蓋" android:textSize="15sp" /> </FrameLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="下面是一個FrameLayout,通過layout方法設定控制項具體位置,請單擊framelayou" /> <FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff0000"> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="請點擊別處" /> </FrameLayout></LinearLayout>
本人微博:honey_11
Demo下載
最後,以上例子都來源與安卓無憂,請去應用寶或者豌豆莢下載:例子源碼,源碼例子文檔一網打盡