一、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 }