android 弹出式对话框实现自定义菜单栏分析

java哥 阅读:435 2021-04-01 10:12:47 评论:0

第一步:创建菜单项实体类(Menu)

/** 
 * 菜单栏选项类 
 */ 
public class Menu { 
 
	//标题 
	private String title; 
	 
	//Id(图片Id) 
	private int iconID; 
 
	public Menu(String title, int iconID) { 
		super(); 
		this.title = title; 
		this.iconID = iconID; 
	} 
 
	public String getTitle() { 
		return title; 
	} 
 
	public void setTitle(String title) { 
		this.title = title; 
	} 
 
	public int getIconID() { 
		return iconID; 
	} 
 
	public void setIconID(int iconID) { 
		this.iconID = iconID; 
	} 
	 
	 
	 
}


第二步:创建弹出式窗口类(自定义菜单栏)---CustomMenu

package com.demo.custommenu; 
 
import java.util.List; 
 
import android.app.Activity; 
import android.graphics.drawable.Drawable; 
import android.view.KeyEvent; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.View.OnKeyListener; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.PopupWindow; 
import android.widget.TextView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.LinearLayout.LayoutParams; 
 
 
/** 
 * 自定义弹出窗口继承PopupWindow 
 */ 
public class CustomMenu extends PopupWindow { 
    //封装GridView组件 
	private GridView gvMenu; 
    //封装View 组件 
	private View view; 
	 
    /** 
     * @param activity 
     * @param menus 
     */ 
    public CustomMenu(Activity activity, List<com.demo.custommenu.Menu> menus) {   
        super(activity);  
        //View 样式文件R.layout.menu 
        view = activity.getLayoutInflater().inflate(R.layout.menu, null);   
        //实例化GridView组件 
        gvMenu = (GridView)view.findViewById(R.id.gv_menu); 
         
        //相关参数设置 
        this.setContentView(view);   
        this.setWidth(LayoutParams.FILL_PARENT);   
        this.setHeight(LayoutParams.WRAP_CONTENT); 
         
        //图片获取 
        Drawable drawable = activity.getResources().getDrawable(R.drawable.selector_focused_shape);  
        //相关特效设置 
        this.setBackgroundDrawable(drawable); 
        this.setAnimationStyle(R.style.PopupAnimation);   
        this.setFocusable(true); 
         
        //设置相关OnKeyListener事件 
        gvMenu.setOnKeyListener(new OnKeyListener() { 
			@Override 
			public boolean onKey(View v, int keyCode, KeyEvent event) { 
				if ((keyCode == KeyEvent.KEYCODE_MENU)&&(isShowing())) {   
					dismiss();//关闭PopupWindow弹出窗口 
			        return true;   
			    }   
				return false; 
			} 
		}); 
        //自定义适配器--MenuAdapter 
        adapter = new MenuAdapter(activity, menus); 
        gvMenu.setAdapter(adapter);   
    } 
    //GridView 为每个Item设置监听事件方法 
    public void setMenuItemListener(OnItemClickListener menuClickListener) { 
    	gvMenu.setOnItemClickListener(menuClickListener);   
    } 
     
     
     
    private MenuAdapter adapter; 
     
    public void dataUpdate() { 
    	//数据设置进行相关的修改工作 
    	adapter.notifyDataSetChanged(); 
    } 
       
    /**  
     * 自定义适配器 
     *   
     */   
    class MenuAdapter extends BaseAdapter {   
         
    	private List<Menu> menus; 
    	 
    	//自定义样式布局文件 
    	private LayoutInflater layoutInflater; 
    	 
    	 
    	 
        /**  
         * ����TabMenu�ķ�ҳ����  
         * @param context ���÷���������  
         * @param texts ��ť���ϵ��ַ�����  
         * @param resID ��ť���ϵ�ͼ����Դ����  
         * @param fontSize ��ť�����С  
         * @param color ��ť������ɫ  
         */   
        public MenuAdapter(Activity activity, List<Menu> menus) {   
            this.menus = menus;   
            layoutInflater = LayoutInflater.from(activity); 
        } 
         
        @Override 
        public int getCount() {   
            return menus.size();   
        }   
         
        @Override 
        public Object getItem(int position) {   
            return menus.get(position)  ; 
        }   
         
        @Override 
        public long getItemId(int position) {   
            return position;   
        }   
         
        @Override 
        public View getView(int position, View convertView, ViewGroup parent) {   
            MenuHolder holder = null; 
            if(convertView==null) { 
            	convertView = layoutInflater.inflate(R.layout.item_menu, null); 
            	 
            	holder = new MenuHolder(); 
            	holder.ivIcon = (ImageView)convertView.findViewById(R.id.iv_menu_item_icon); 
            	holder.tvTitle = (TextView)convertView.findViewById(R.id.tv_menu_item_title); 
            	 
            	convertView.setTag(holder); 
            } 
            else {  
    			holder = (MenuHolder)convertView.getTag();  
    		} 
             
            holder.ivIcon.setImageResource(menus.get(position).getIconID()); 
            holder.tvTitle.setText(menus.get(position).getTitle()); 
        	return convertView; 
        } 
         
        class MenuHolder { 
        	 
        	/** 
        	 * 图片组件 
        	 */ 
        	public ImageView ivIcon; 
        	 
        	/** 
        	 * 文本组件 
        	 */ 
        	public TextView tvTitle; 
        } 
    }   
}


第三步:修改Activity样式文件------BasicActivity

package com.demo.custommenu; 
 
import java.util.ArrayList; 
import java.util.List; 
 
import android.app.Activity;   
import android.os.Bundle;   
import android.view.Gravity; 
import android.view.Menu;   
import android.widget.AdapterView.OnItemClickListener;   
 
public class BaseActivity extends Activity { 
    //自定义弹出式菜单栏 
    private CustomMenu customMenu; 
     
    //样式组件 
    private int parentViewID;  
     
 
    private List<com.demo.custommenu.Menu> menus;  
     
    @Override   
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);   
         
        menus = new ArrayList<com.demo.custommenu.Menu>(); 
 
         
        customMenu = new CustomMenu(this, menus); 
            
         
    } 
     
    /** 
     * �
标签:安卓Android
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
关注我们

一个IT知识分享的公众号