来实现自定义的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欢迎访问!

相关文章
相关标签/搜索