import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.PopupWindow; public class MainActivity extends Activity { private Handler handler = new Handler() { public void handleMessage(Message msg) { switch (msg.arg1) { case 1: showBottomMenu(); break; } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Timer timer = new Timer(); timer.schedule(new initBottomMenu(), 100); } private class initBottomMenu extends TimerTask { @Override public void run() { Message msg = new Message(); msg.arg1 = 1; handler.sendMessage(msg); } } /** * 显示底部菜单 */ public void showBottomMenu() { LayoutInflater mLayoutInfalter = (LayoutInflater) this .getSystemService(LAYOUT_INFLATER_SERVICE); View menuView = mLayoutInfalter.inflate(R.layout.menu, null); PopupWindow mPopupWindow = new PopupWindow(menuView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); mPopupWindow.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM, 0, 0); } }
主界面的布局文件:main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="底部菜单实例" /> </LinearLayout>
底部菜单的布局文件:menu.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:background="@drawable/menu_back"> <Button android:id="@+id/menu_ensureButton" android:layout_width="160px" android:layout_height="wrap_content" android:layout_marginTop="5px" android:text="完成" /> <Button android:id="@+id/menu_cancelButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5px" android:text="取消" /> </LinearLayout>