Android的PopupWindow控件詳解

PopupWindow是一個懸浮的窗口,可以自定義顯示的內容,控制顯示的位置,下面通過一個例子說明,用Popupwindow實現了一個菜單,代碼如下:

	public void showPopupMenu(){
		if(menu == null){
			LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
			View popView = inflater.inflate(R.layout.popupmenu, null);
			menu = new PopupWindow(popView,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
			menu.setFocusable(true);
			//下面這一句必須加,否則的話,一旦PopupWindow獲得焦點其他空間就沒有響應了,
			//我也是在網上找到的解決方法,具體原因希望知道的說一下
			menu.setBackgroundDrawable(getResources().getDrawable(R.drawable.menu_background));
			menu.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
		}
		else if(menu.isShowing()){
			menu.dismiss();
		}else{
			menu.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
		}
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		Log.i("按鍵", keyCode+"");
		if(keyCode == KeyEvent.KEYCODE_MENU){	
			showPopupMenu();
			return true;
		}
		else{
			return super.onKeyDown(keyCode, event);
		}
		
	}
popupmenu.xml如下,其實最好用GridView實現,這裏偷懶用幾個ImageView代替了

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <!-- <GridView 
        android:id="@+id/gridMenu"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:numColumns="3" -->
	<LinearLayout 
	    android:orientation="horizontal"
	    android:layout_width="fill_parent"
	    android:layout_height="50dp">
	    <ImageView
	        android:layout_width="0dp"
	        android:layout_height="wrap_content"
	        android:layout_weight="1"
	        android:scaleType="centerInside"
	        android:src="@drawable/bluetooth"/>
	    <ImageView
	        android:layout_width="0dp"
	        android:layout_height="wrap_content"
	        android:layout_weight="1"
	        android:scaleType="centerInside"
	        android:src="@drawable/earth"/>
	    <ImageView
	        android:layout_width="0dp"
	        android:layout_height="wrap_content"
	        android:layout_weight="1"
	        android:scaleType="centerInside"
	        android:src="@drawable/email"/>
	    <ImageView
	        android:layout_width="0dp"
	        android:layout_height="wrap_content"
	        android:layout_weight="1"
	        android:scaleType="centerInside"
	        android:src="@drawable/reader"/>
	</LinearLayout>
	<LinearLayout 
	    	android:orientation="horizontal"
	    	android:layout_width="fill_parent"
	    	android:layout_height="50dp">
	    <ImageView
	        android:layout_width="0dp"
	        android:layout_height="wrap_content"
	        android:layout_weight="1"
	        android:scaleType="centerInside"
	        android:src="@drawable/map"/>
	    <ImageView
	        android:layout_width="0dp"
	        android:layout_height="wrap_content"
	        android:layout_weight="1"
	        android:scaleType="centerInside"
	        android:src="@drawable/news"/>
	    <ImageView
	        android:layout_width="0dp"
	        android:layout_height="wrap_content"
	        android:layout_weight="1"
	        android:scaleType="centerInside"
	        android:src="@drawable/reader"/>
	    <ImageView
	        android:layout_width="0dp"
	        android:layout_height="wrap_content"
	        android:layout_weight="1"
	        android:scaleType="centerInside"
	        android:src="@drawable/reader"/>
	</LinearLayout>
</LinearLayout>
效果圖:


這個小Demo存在的一個小問題,之前也遇到過,就是手機上的按鍵有時會沒有響應,比如我按下menu鍵之後,再次按就沒有反應,提示說

01-29 07:01:33.646: W/KeyCharacterMap(1904): No keyboard for id 0
01-29 07:01:33.646: W/KeyCharacterMap(1904): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

不知道什麼原因,糾結了好久

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章