android ä¸è®©èåmenuæ¾ç¤ºå¨å·¦ä¸è§ï¼å¯ä»¥ä½¿ç¨popupwindowææ¯ï¼ä¹å°±æ¯æ¬æµ®èåï¼è®¾ç½®é»è®¤çä½ç½®ä¸ºå·¦ä¸è§ï¼å¦ä¸ä»£ç ï¼
package com.example.menutype;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;
import android.widget.Toast;
/**
*
* @author tr
* @time 2014-3-10
* @description èªå®ä¹èåï¼ä¸æèåæ ·å¼ï¼æ·»å å¨ç»ææï¼éåonMenuOpened()æ¹æ³ï¼èªå®ä¹"menu"æé®å¼¹åºèå
*/
public class MainActivity extends Activity implements OnClickListener{
private static Toast mToast;
private static Context mContext;
private PopupWindow popupWindow ;
private Button btn_popupwindow;
private View mPopupWindowView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
btn_popupwindow = (Button) findViewById(R.id.btn_popupwindow);
btn_popupwindow.setOnClickListener(this);
initPopupWindow();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/**actionBarä¸æ´å¤æé®*/
getMenuInflater().inflate(R.menu.main, menu);
/**ç¹å»menuï¼å¼¹åºèå*/
/*
*
* add()æ¹æ³çå个åæ°ï¼ä¾æ¬¡æ¯ï¼
*
* 1ãç»å«ï¼å¦æä¸åç»çè¯å°±åMenu.NONE,
*
* 2ãIdï¼è¿ä¸ªå¾éè¦ï¼Androidæ ¹æ®è¿ä¸ªIdæ¥ç¡®å®ä¸åçèå
*
* 3ã顺åºï¼é£ä¸ªèåç°å¨å¨åé¢ç±è¿ä¸ªåæ°ç大å°å³å®
*
* 4ãææ¬ï¼èåçæ¾ç¤ºææ¬
*/
menu.add(Menu.NONE, Menu.FIRST + 1, 1, getResource(R.string.edit_text)).setIcon(
R.drawable.ic_edit);
// setIcon()æ¹æ³ä¸ºèå设置å¾æ ï¼è¿é使ç¨çæ¯ç³»ç»èªå¸¦çå¾æ ï¼åå¦ä»¬çæä¸ä¸,以
// android.Rå¼å¤´çèµæºæ¯ç³»ç»æä¾çï¼æ们èªå·±æä¾çèµæºæ¯ä»¥Rå¼å¤´ç
menu.add(Menu.NONE, Menu.FIRST + 2, 2, getResource(R.string.file)).setIcon(
R.drawable.ic_menu_file);
menu.add(Menu.NONE, Menu.FIRST + 3, 3, getResource(R.string.about)).setIcon(
R.drawable.ic_menu_about);
return true;
}
/**èåæå¼æ¶è°ç¨*/
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
// TODO Auto-generated method stub
showToast("menuèåæå¼ï¼"+featureId);
//ç¹å»"menu"æé®æå¼
if(featureId == 0){
showPopupWindow();
}
return super.onMenuOpened(featureId, menu);// è¿å为true åæ¾ç¤ºç³»ç»menu
// return false;
}
/**menuèåå
³éæ¶è°ç¨*/
@Override
public void onOptionsMenuClosed(Menu menu) {
// TODO Auto-generated method stub
super.onOptionsMenuClosed(menu);
showToast("menuèåå
³é");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case Menu.FIRST + 1:
case R.id.action_edit:
showToast(getResource(R.string.edit_text));
break;
case Menu.FIRST + 2:
case R.id.action_file:
showToast(getResource(R.string.file));
break;
case R.id.action_favorite:
showToast(getResource(R.string.favorite));
break;
case R.id.action_share:
showToast(getResource(R.string.share));
break;
case Menu.FIRST + 3:
case R.id.action_about:
showToast(getResource(R.string.about));
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_popupwindow:
showPopupWindow();
break;
case R.id.textview_about:
showToast(getResource(R.string.about));
popupWindow.dismiss();
break;
case R.id.textview_edit:
showToast(getResource(R.string.edit_text));
popupWindow.dismiss();
break;
case R.id.textview_file:
showToast(getResource(R.string.file));
popupWindow.dismiss();
break;
}
}
/**æ¾ç¤ºpopupwindow*/
private void showPopupWindow(){
if(!popupWindow.isShowing()){
popupWindow.showAsDropDown(btn_popupwindow, btn_popupwindow.getLayoutParams().width/2, 0);
}else{
popupWindow.dismiss();
}
}
/**
* åå§åpopupwindow
*/
private void initPopupWindow(){
initPopupWindowView();
//åå§åpopupwindowï¼ç»å®æ¾ç¤ºviewï¼è®¾ç½®è¯¥viewç宽度/é«åº¦
popupWindow = new PopupWindow(mPopupWindowView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
// è¿ä¸ªæ¯ä¸ºäºç¹å»âè¿åBackâä¹è½ä½¿å
¶æ¶å¤±ï¼å¹¶ä¸å¹¶ä¸ä¼å½±åä½ çèæ¯ï¼ä½¿ç¨è¯¥æ¹æ³ç¹å»çªä½ä¹å¤ï¼æå¯å
³éçªä½
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bitmap_book_read_chapterlist_repeat));
//Backgroundä¸è½è®¾ç½®ä¸ºnullï¼dismissä¼å¤±æ
// popupWindow.setBackgroundDrawable(null);
//设置æ¸å
¥ãæ¸åºå¨ç»ææ
// popupWindow.setAnimationStyle(R.style.popupwindow);
popupWindow.update();
//popupWindowè°ç¨dismissæ¶è§¦åï¼è®¾ç½®äºsetOutsideTouchable(true)ï¼ç¹å»viewä¹å¤/æé®backçå°æ¹ä¹ä¼è§¦å
popupWindow.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
// TODO Auto-generated method stub
// showToast("å
³épopupwindow");
}
});
}