我無法找到一個簡單的方法來樣式我PopupMenu的,所以我用「PopupWindow」相反,通過一個ListView給它,它的風格,因爲我想要的。
popView=layoutInflater.inflate(R.layout.pop_layout, null); // layout with a listview to put in the popup window
lv=(ListView)popView.findViewById(R.id.pop_listview); // then set listview parameters
final PopupWindow contentsopupWindow = new PopupWindow(popView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
contentsopupWindow.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.pixel_dot));// set a background so the menu disappears when you click outside it
contentsopupWindow.setTouchable(true);
contentsopupWindow.setFocusable(true);
contentsopupWindow.setOutsideTouchable(true);
contentsopupWindow.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
contentsopupWindow.dismiss();
return true;
}
return false;
}
});
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
contentsopupWindow.showAsDropDown(anchorView);
然後在列表項上單擊使用contentsopupWindow.dismiss();自動關閉窗口 – 2012-12-26 14:15:41