來實現自定義的menu菜單之一:使用PopupWindow

blog已經遷移到這裏了,有更多文章,歡迎你們訪問。php

一:感受使用android自帶的menu菜單背景默認顏色不是很好看,因而想改變一下背景顏色或是添加一個背景圖片。html

二:使用了setBackgroundColor和setTextColor等來修改背景顏色和文字顏色,費勁千辛萬苦弄好以後,在模擬器上可以看見效果,誰知放到手機上卻沒有任何改變,顯示的還是默認的顏色樣式,納悶至極,三:在羣裏問了一下,說是能夠本身寫一個自定的MENU,因而開始了自定義menu的過程。對於我這種新手,花費了好幾天的時間,不過最終,終於搞定。java

 

三:先貼上個人自定義menu效果圖:android

 

 

 

四:下面貼上個人實現代碼:app

主要包括 一個自定義的menu類(CustomMenu)和自定義menu佈局(layout_custom_menu)。ide

CustomMenu.java代碼以下:函數

 

public final class CustomMenu {
	
	private static final String TAG="CustomMenu";	
	private static PopupWindow pop=null;
	private final Activity activity;
	
	public CustomMenu(Activity activity) {
		// TODO Auto-generated constructor stub
		this.activity = activity;
	}
	                
	public  PopupWindow getMenu(OnTouchListener touchListener,OnKeyListener keyListener) {

		View view = activity.getLayoutInflater().inflate(R.layout.layout_custom_menu, null);  // layout_custom_menu菜單的佈局文件
		pop = new PopupWindow(view,ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);          
		pop.setAnimationStyle(R.style.pop_anim_style);
		pop.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.pop_menu_bg));// 這句是關鍵,響應返回鍵必須的語句
		pop.setFocusable(true);
		pop.setTouchable(true);
		pop.setOutsideTouchable(true);
		view.setFocusableInTouchMode(true); 
		pop.setTouchInterceptor(touchListener);
		view.setOnKeyListener(keyListener);
		
		Log.i(TAG, pop.toString());
		return pop;
	
}

 

默認加載的activity_main.xml佈局我這裏就再也不貼出代碼,對本應用的實現沒有影響。貼出  layout_custom_menu.xml代碼以下:佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/zong"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#C1CDC1"
    android:baselineAligned="false"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#A1A1A1"
        android:gravity="center_horizontal"
        android:layout_marginTop="1dp"
        android:orientation="horizontal" >
       
     <LinearLayout
          android:id="@+id/guanyu"
          android:clickable="true"
          android:layout_width="106dp" android:layout_height="wrap_content"
          android:orientation="vertical" android:gravity="center_horizontal"
          android:layout_marginRight="1dp"
          android:layout_marginLeft="1dip"
          android:layout_marginTop="1dp"       
          android:layout_gravity="fill_horizontal"
          android:background="#00868B"
          >
          <ImageView 
			android:id="@+id/menu_item_iv_helper" 
			android:layout_width="wrap_content" 
			android:layout_height="wrap_content"
			android:src="@drawable/menu_about"
			/>
		 <TextView
		     android:id="@+id/menu_item_txt_helper"
		    android:layout_width="wrap_content" 
		    android:layout_height="wrap_content"
			android:textColor="#ff222222"
			android:text="關於"
			/>
		 
       </LinearLayout>
       
     
      <LinearLayout
          android:id="@+id/shezhi"
          android:layout_width="106dp" android:layout_height="wrap_content"
          android:orientation="vertical" android:gravity="center_horizontal"
          android:layout_marginRight="1dp"
          android:layout_marginTop="1dp"
          android:layout_gravity="fill_horizontal"
           android:background="#00868B"
          >
          <ImageView 
			android:id="@+id/menu_item_iv_feedback" 
			android:layout_width="wrap_content" 
			android:layout_height="wrap_content"
			android:src="@drawable/menu_shezhi"
			/>
		 <TextView
		     android:id="@+id/menu_item_txt_feedback"
		    android:layout_width="wrap_content" 
		    android:layout_height="wrap_content"
			android:textColor="#ff222222"
			android:text="設置標識碼"
			/>
       </LinearLayout>
        <LinearLayout
          android:id="@+id/tuichu"  
          android:layout_width="106dp" android:layout_height="wrap_content"
          android:orientation="vertical" android:gravity="center_horizontal"
          android:layout_marginTop="1dp"
          android:layout_marginRight="1dp"
          android:layout_gravity="fill_horizontal"
          android:background="#00868B"
          >
          <ImageView 
			android:id="@+id/menu_item_iv_exit" 
			android:layout_width="wrap_content" 
			android:layout_height="wrap_content"
			android:src="@drawable/menu_logout"
		
			/>
		 <TextView
		     android:id="@+id/menu_item_txt_exit"
		    android:layout_width="wrap_content" 
		    android:layout_height="wrap_content"
			android:textColor="#ff222222"
			android:text="退出"
			/>
       </LinearLayout>
    </LinearLayout>
</LinearLayout>

MainActivity.java代碼以下:ui

public class MainActivity extends Activity {

	private String skey="";
	private View textview3;
	private long ntime;
	private Button btn1,btn2;
	private PopupWindow pop;
	public MyHandler myHandler;
	private ImageButton imgIndex;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		CustomMenu cm = new CustomMenu(this); 
		pop = cm.getMenu(touchListener, keyListener);
		setContentView(R.layout.activity_main);				
	}


	
	/**
	 * 處理鍵盤事件
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub

		if (keyCode == KeyEvent.KEYCODE_MENU) {
			if (pop.isShowing()) {
				pop.dismiss();
			} else {	
				pop.showAtLocation(findViewById(R.id.tt),Gravity.BOTTOM, 0, 0);

			}
		}
		
		
		
		return super.onKeyDown(keyCode, event);
	}
	
	

	 /**
	 * 處理鍵盤事件
	 */
	
		private OnTouchListener touchListener = new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
					pop.dismiss();
				}
				return false;
			}
		};
		private OnKeyListener keyListener = new OnKeyListener() {

			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN
						&& keyCode == KeyEvent.KEYCODE_MENU) {
					pop.dismiss();
					return true;
				}
				return false;
			}
		};

	
	
	


}

五:代碼解釋:this

blog已經遷移到這裏了,有更多文章,歡迎你們訪問。

這裏主要解釋一下 MainActivity中的(onkeydown函數)相關知識:

  1.     當用戶單擊模擬器或手機的menu菜單時文字,觸發 onKeyDown,若是 PopupWindow已經打開,則關閉;
  2.       若是沒有打開,則使用
    pop .showAtLocation(findViewById(R.id. tt ),Gravity. BOTTOM , 0, 0);
    來加載自定義的 layout_custom_menu佈局文件。
  3. tt爲將要自定義的menu加載到的那個佈局上(activity_main.xml)的id名稱。

六:本篇講解到此爲止,請關注第二篇 :

來實現android自定義的menu菜單之二:爲自定義menu中的線性佈局添加事件。


博客文章已經所有遷到:http://phping.sinaapp.com歡迎訪問!

相關文章
相關標籤/搜索