一、FrameLayout介紹
FrameLayout幀布局是最簡單的布局之一,采用幀布局的容器中無論放入多少個控件,控件默認情況下左上角都對齊到容器的左上角,如果控件一樣大,同一時間只能見到最上面的。
二、用FrameLayout構建一個飲食介紹界面
1、效果

2、源代碼
activity_main.xml
1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" > 5 6 <LinearLayout 7 android:layout_width="fill_parent" 8 android:layout_height="fill_parent" 9 android:orientation="vertical" > 10 11 <LinearLayout 12 android:layout_width="fill_parent" 13 android:layout_height="40dp" 14 android:background="@drawable/title" > 15 16 <TextView 17 android:layout_width="fill_parent" 18 android:layout_height="wrap_content" 19 android:gravity="center" 20 android:text="土豆白菜湯的做法" 21 android:textColor="#ffffff" 22 android:textSize="30sp" /> 23 24 </LinearLayout> 25 26 <LinearLayout 27 android:layout_width="fill_parent" 28 android:layout_height="fill_parent" 29 android:background="#ffd700" > 30 31 <TextView 32 android:layout_width="fill_parent" 33 android:layout_height="fill_parent" 34 android:text="做法: 土豆削皮,切成小拇指粗細的土豆條,沖洗瀝干備用; 取白菜頭,手撕成片,大蔥切絲備用;起油鍋,油熱后,下入蔥絲小火煸炒值微黃,抽出蔥干;下入土豆中火遍炒至土豆姿色變軟;添加熱水 大火燒開,轉小火燉至湯濃;下入白菜葉子,繼續燉煮至白菜和土豆軟爛; 加入適量鹽,出鍋即可" 35 android:textColor="#ffffff" 36 android:textSize="20sp" /> 37 </LinearLayout> 38 </LinearLayout> 39 40 <LinearLayout 41 android:layout_width="fill_parent" 42 android:layout_height="fill_parent" 43 android:gravity="bottom|right" > 44 45 <Button 46 android:id="@+id/btnExit" 47 android:layout_width="wrap_content" 48 android:layout_height="wrap_content" 49 android:text="退出" /> 50 </LinearLayout> 51 52 </FrameLayout>

MainActivity.java
1 package com.weipenng.android.myframelayout; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.view.Menu; 6 import android.view.View; 7 import android.widget.Button; 8 9 public class MainActivity extends Activity { 10 11 12 13 private Button btnExit; 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 btnExit=(Button) findViewById(R.id.btnExit); 20 21 22 btnExit.setOnClickListener(new View.OnClickListener() { 23 24 @Override 25 public void onClick(View v) { 26 27 System.exit(0); 28 } 29 }); 30 31 } 32 33 @Override 34 public boolean onCreateOptionsMenu(Menu menu) { 35 // Inflate the menu; this adds items to the action bar if it is present. 36 getMenuInflater().inflate(R.menu.main, menu); 37 return true; 38 } 39 40 }
