1. 程式人生 > >處女男學Android(十二)---Android 選單(Menu)詳解與應用

處女男學Android(十二)---Android 選單(Menu)詳解與應用

package com.wl.menusdemo;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.Toast;

public class SecondActivity extends Activity implements OnClickListener {

	private Context context;
	private PopupWindow popupWindow;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
		context = this;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			showMenu();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	private void showMenu() {
		LayoutInflater inflater = getLayoutInflater();
		View menuView = inflater.inflate(R.layout.pop_menu, null);
		Button button1 = (Button) menuView.findViewById(R.id.btn_ver_update);
		Button button2 = (Button) menuView.findViewById(R.id.btn_help);
		Button button3 = (Button) menuView.findViewById(R.id.btn_exit_qq);
		Button button4 = (Button) menuView.findViewById(R.id.btn_undo);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		button3.setOnClickListener(this);
		button4.setOnClickListener(this);

		// 設定PopupWindow寬高
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		double screenWidth = metrics.widthPixels * 0.95;

		popupWindow = new PopupWindow(menuView, (int) screenWidth,
				LayoutParams.WRAP_CONTENT);
		// 想要讓PopupWindow中的控制元件能夠使用,就必須設定PopupWindow為focusable
		popupWindow.setFocusable(true);

		// 想做到在你點選PopupWindow以外的區域時候讓PopupWindow消失就做如下兩步操作
		// 1:設定setOutsideTouchable為ture,這個很容易理解吧,跟AlertDialog一樣的
		popupWindow.setOutsideTouchable(true);
		popupWindow.setBackgroundDrawable(getResources().getDrawable(
				android.R.color.transparent));
		popupWindow.setAnimationStyle(R.style.AnimationPreview);
		popupWindow.setOnDismissListener(onDismissListener);

		popupWindow.showAtLocation(getWindow().getDecorView(),
				android.view.Gravity.BOTTOM, 0, 0);

		// 設定父視窗透明度
		WindowManager.LayoutParams wlp = getWindow().getAttributes();
		wlp.alpha = 0.5f;
		getWindow().setAttributes(wlp);
	}

	private OnDismissListener onDismissListener = new OnDismissListener() {

		@Override
		public void onDismiss() {
			WindowManager.LayoutParams wlp = getWindow().getAttributes();
			wlp.alpha = 1.0f;
			getWindow().setAttributes(wlp);

		}
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (popupWindow != null && popupWindow.isShowing())
			popupWindow.dismiss();
		switch (v.getId()) {
		case R.id.btn_ver_update:
			Toast.makeText(context, "版本更新!", Toast.LENGTH_SHORT).show();
			break;
		case R.id.btn_help:
			Toast.makeText(context, "幫助與反饋!", Toast.LENGTH_SHORT).show();
			break;
		case R.id.btn_exit_qq:
			Toast.makeText(context, "退出QQ!", Toast.LENGTH_SHORT).show();
			break;
		case R.id.btn_undo:
			Toast.makeText(context, "取消!", Toast.LENGTH_SHORT).show();
			break;
		}
	}
}

BINGO~結束了,最後看一下效果圖,相似度還不錯吧: