自定义menu

用过UCWEB-Android版的人都应该对其特殊的menu有印象,把menu做成Tab-Menu(支持分页的Menu),可以容纳比Android传统的menu更丰富的内容(Android的menu超过6项则缩略在[更多]里),本文参考网上的例子(作者:CoffeeCole,email:longkefan@foxmail.com),对例子进行简化以及封装,使其作为一个复合控件融入自己的framework。
先来看看本文程序运行的效果:
0_1296229056lqBL.jpg
TabMenu本身就是一个PopupWindow,PopupWindow上面放了两个GridView,第一个GridView就是分页标签,位于PopupWindow的顶部,第二个GridView是菜单,位于PopupWindow的主体。为了实现PopupWindow的弹出/退出的动画效果,本文使用了以下代码:
在工程的res文件夹里添加anim子目录,再新建文件popup_enter.xml:
[xhtml] view plaincopyprint?

  • <?xml version="1.0" encoding="utf-8"?>  
  • <set xmlns:android="http://schemas.android.com/apk/res/android">  
  •     <translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="1000" />  
  •     <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" />  
  • </set>   

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">        <translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="1000" />        <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" /></set>  
新建文件popup_exit.xml:
[xhtml] view plaincopyprint?

  • <?xml version="1.0" encoding="utf-8"?>  
  • <set xmlns:android="http://schemas.android.com/apk/res/android">  
  •     <translate android:fromYDelta="0" android:toYDelta="100%p" android:duration="1000" />  
  •     <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="1000" />  
  • </set>   

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">        <translate android:fromYDelta="0" android:toYDelta="100%p" android:duration="1000" />        <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="1000" /></set>  

在工程的values文件夹里新建文件popup_animation.xml:
<?xml version="1.0" encoding="utf-8"?>  
<resources>     
    <style name="PopupAnimation" parent="android:Animation">
        <item name="android:windowEnterAnimation">@anim/popup_enter</item>  
        <item name="android:windowExitAnimation">@anim/popup_exit</item>   
    </style>  
</resources>



main.xml的源码如下:
[xhtml]  view plain copy print ?

  • <?xml version="1.0" encoding="utf-8"?>  
  • <LinearLayout android:id="@+id/LinearLayout01"  
  •     android:layout_width="fill_parent" android:layout_height="fill_parent"  
  •     xmlns:android="http://schemas.android.com/apk/res/android">  
  •     <TextView android:id="@+id/TextView01" android:layout_height="wrap_content"  
  •         android:layout_width="fill_parent" android:text="扩展Menu----hellogv"></TextView>  
  • </LinearLayout>  

<?xml version="1.0" encoding="utf-8"?><LinearLayout android:id="@+id/LinearLayout01"        android:layout_width="fill_parent" android:layout_height="fill_parent"        xmlns:android="http://schemas.android.com/apk/res/android">        <TextView android:id="@+id/TextView01" android:layout_height="wrap_content"                android:layout_width="fill_parent" android:text="扩展Menu----hellogv"></TextView></LinearLayout>  
TabMenu的封装类TabMenu.java的源码如下:
[java] view plaincopyprint?

  • package com.testTabMenu;  
  • import android.content.Context;  
  • import android.graphics.Color;  
  • import android.graphics.drawable.ColorDrawable;  
  • import android.view.Gravity;  
  • import android.view.View;  
  • import android.view.ViewGroup;  
  • import android.widget.BaseAdapter;  
  • import android.widget.GridView;  
  • import android.widget.ImageView;  
  • import android.widget.LinearLayout;  
  • import android.widget.PopupWindow;  
  • import android.widget.TextView;  
  • import android.widget.AdapterView.OnItemClickListener;  
  • import android.widget.LinearLayout.LayoutParams;  
  • public class TabMenu extends PopupWindow{  
  •     private GridView gvBody, gvTitle;  
  •     private LinearLayout mLayout;  
  •     private MenuTitleAdapter titleAdapter;  
  •     public TabMenu(Context context,OnItemClickListener titleClick,OnItemClickListener bodyClick,  
  •             MenuTitleAdapter titleAdapter,int colorBgTabMenu,int aniTabMenu){  
  •         super(context);  
  •          
  •         mLayout = new LinearLayout(context);  
  •         mLayout.setOrientation(LinearLayout.VERTICAL);  
  •         //标题选项栏   
  •         gvTitle = new GridView(context);  
  •         gvTitle.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  
  •         gvTitle.setNumColumns(titleAdapter.getCount());  
  •         gvTitle.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);  
  •         gvTitle.setVerticalSpacing(1);  
  •         gvTitle.setHorizontalSpacing(1);  
  •         gvTitle.setGravity(Gravity.CENTER);  
  •         gvTitle.setOnItemClickListener(titleClick);  
  •         gvTitle.setAdapter(titleAdapter);  
  •         gvTitle.setSelector(new ColorDrawable(Color.TRANSPARENT));//选中的时候为透明色   
  •         this.titleAdapter=titleAdapter;  
  •         //子选项栏   
  •         gvBody = new GridView(context);  
  •         gvBody.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));  
  •         gvBody.setSelector(new ColorDrawable(Color.TRANSPARENT));//选中的时候为透明色   
  •         gvBody.setNumColumns(4);  
  •         gvBody.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);  
  •         gvBody.setVerticalSpacing(10);  
  •         gvBody.setHorizontalSpacing(10);  
  •         gvBody.setPadding(10, 10, 10, 10);  
  •         gvBody.setGravity(Gravity.CENTER);  
  •         gvBody.setOnItemClickListener(bodyClick);  
  •         mLayout.addView(gvTitle);  
  •         mLayout.addView(gvBody);  
  •          
  •         //设置默认项   
  •         this.setContentView(mLayout);  
  •         this.setWidth(LayoutParams.FILL_PARENT);  
  •         this.setHeight(LayoutParams.WRAP_CONTENT);  
  •         this.setBackgroundDrawable(new ColorDrawable(colorBgTabMenu));// 设置TabMenu菜单背景   
  •         this.setAnimationStyle(aniTabMenu);  
  •         this.setFocusable(true);// menu菜单获得焦点 如果没有获得焦点menu菜单中的控件事件无法响应   
  •     }  
  •       
  •       
  •     public void SetTitleSelect(int index)  
  •     {  
  •         gvTitle.setSelection(index);  
  •         this.titleAdapter.SetFocus(index);  
  •     }  
  •       
  •     public void SetBodySelect(int index,int colorSelBody)  
  •     {  
  •         int count=gvBody.getChildCount();  
  •         for(int i=0;i<count;i++)  
  •         {  
  •             if(i!=index)  
  •                 ((LinearLayout)gvBody.getChildAt(i)).setBackgroundColor(Color.TRANSPARENT);  
  •         }  
  •         ((LinearLayout)gvBody.getChildAt(index)).setBackgroundColor(colorSelBody);  
  •     }  
  •       
  •     public void SetBodyAdapter(MenuBodyAdapter bodyAdapter)  
  •     {  
  •         gvBody.setAdapter(bodyAdapter);  
  •     }  
  •       
  •     /**
  •      * 自定义Adapter,TabMenu的每个分页的主体
  •      *  
  •      */  
  •     static public class MenuBodyAdapter extends BaseAdapter {  
  •         private Context mContext;  
  •         private int fontColor,fontSize;  
  •         private String[] texts;  
  •         private int[] resID;  
  •         /**
  •          * 设置TabMenu的分页主体
  •          * @param context 调用方的上下文
  •          * @param texts 按钮集合的字符串数组
  •          * @param resID 按钮集合的图标资源数组
  •          * @param fontSize 按钮字体大小
  •          * @param color 按钮字体颜色
  •          */  
  •         public MenuBodyAdapter(Context context, String[] texts,int[] resID, int fontSize,int fontColor)   
  •         {  
  •             this.mContext = context;  
  •             this.fontColor = fontColor;  
  •             this.texts = texts;  
  •             this.fontSize=fontSize;  
  •             this.resID=resID;  
  •         }  
  •         public int getCount() {  
  •             return texts.length;  
  •         }  
  •         public Object getItem(int position) {  
  •               
  •             return makeMenyBody(position);  
  •         }  
  •         public long getItemId(int position) {  
  •             return position;  
  •         }  
  •          
  •         private LinearLayout makeMenyBody(int position)  
  •         {  
  •             LinearLayout result=new LinearLayout(this.mContext);  
  •             result.setOrientation(LinearLayout.VERTICAL);  
  •             result.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);     
  •             result.setPadding(10, 10, 10, 10);  
  •               
  •             TextView text = new TextView(this.mContext);  
  •             text.setText(texts[position]);  
  •             text.setTextSize(fontSize);  
  •             text.setTextColor(fontColor);  
  •             text.setGravity(Gravity.CENTER);  
  •             text.setPadding(5, 5, 5, 5);  
  •             ImageView img=new ImageView(this.mContext);  
  •             img.setBackgroundResource(resID[position]);  
  •             result.addView(img,new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)));  
  •             result.addView(text);  
  •             return result;  
  •         }  
  •          
  •         public View getView(int position, View convertView, ViewGroup parent) {  
  •             return makeMenyBody(position);  
  •         }  
  •     }  
  •       
  •       
  •     /**
  •      * 自定义Adapter,TabMenu的分页标签部分
  •      *  
  •      */  
  •     static public class MenuTitleAdapter extends BaseAdapter {  
  •         private Context mContext;  
  •         private int fontColor,unselcolor,selcolor;  
  •         private TextView[] title;  
  •         /**
  •          * 设置TabMenu的title
  •          * @param context 调用方的上下文
  •          * @param titles 分页标签的字符串数组
  •          * @param fontSize 字体大小
  •          * @param fontcolor 字体颜色
  •          * @param unselcolor 未选中项的背景色
  •          * @param selcolor 选中项的背景色
  •          */  
  •         public MenuTitleAdapter(Context context, String[] titles, int fontSize,  
  •                 int fontcolor,int unselcolor,int selcolor) {  
  •             this.mContext = context;  
  •             this.fontColor = fontcolor;  
  •             this.unselcolor = unselcolor;  
  •             this.selcolor=selcolor;  
  •             this.title = new TextView[titles.length];  
  •             for (int i = 0; i < titles.length; i++) {  
  •                 title = new TextView(mContext);  
  •                 title.setText(titles);  
  •                 title.setTextSize(fontSize);  
  •                 title.setTextColor(fontColor);  
  •                 title.setGravity(Gravity.CENTER);  
  •                 title.setPadding(10, 10, 10, 10);  
  •             }  
  •         }  
  •         public int getCount() {  
  •             return title.length;  
  •         }  
  •         public Object getItem(int position) {  
  •             return title[position];  
  •         }  
  •         public long getItemId(int position) {  
  •             return title[position].getId();  
  •         }  
  •         /**
  •          * 设置选中的效果
  •          */  
  •         private void SetFocus(int index)  
  •         {  
  •             for(int i=0;i<title.length;i++)  
  •             {  
  •                 if(i!=index)  
  •                 {  
  •                     title.setBackgroundDrawable(new ColorDrawable(unselcolor));//设置没选中的颜色   
  •                     title.setTextColor(fontColor);//设置没选中项的字体颜色   
  •                 }  
  •             }  
  •             title[index].setBackgroundColor(0x00);//设置选中项的颜色   
  •             title[index].setTextColor(selcolor);//设置选中项的字体颜色   
  •         }  
  •          
  •         public View getView(int position, View convertView, ViewGroup parent) {  
  •             View v;  
  •             if (convertView == null) {  
  •                 v = title[position];  
  •             } else {  
  •                 v = convertView;  
  •             }  
  •             return v;  
  •         }  
  •     }  
  • }  

package com.testTabMenu;import android.content.Context;import android.graphics.Color;import android.graphics.drawable.ColorDrawable;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.GridView;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.PopupWindow;import android.widget.TextView;import android.widget.AdapterView.OnItemClickListener;import android.widget.LinearLayout.LayoutParams;public class TabMenu extends PopupWindow{        private GridView gvBody, gvTitle;        private LinearLayout mLayout;        private MenuTitleAdapter titleAdapter;        public TabMenu(Context context,OnItemClickListener titleClick,OnItemClickListener bodyClick,                        MenuTitleAdapter titleAdapter,int colorBgTabMenu,int aniTabMenu){                super(context);                                mLayout = new LinearLayout(context);                mLayout.setOrientation(LinearLayout.VERTICAL);                //标题选项栏                gvTitle = new GridView(context);                gvTitle.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));                gvTitle.setNumColumns(titleAdapter.getCount());                gvTitle.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);                gvTitle.setVerticalSpacing(1);                gvTitle.setHorizontalSpacing(1);                gvTitle.setGravity(Gravity.CENTER);                gvTitle.setOnItemClickListener(titleClick);                gvTitle.setAdapter(titleAdapter);                gvTitle.setSelector(new ColorDrawable(Color.TRANSPARENT));//选中的时候为透明色                this.titleAdapter=titleAdapter;                //子选项栏                gvBody = new GridView(context);                gvBody.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));                gvBody.setSelector(new ColorDrawable(Color.TRANSPARENT));//选中的时候为透明色                gvBody.setNumColumns(4);                gvBody.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);                gvBody.setVerticalSpacing(10);                gvBody.setHorizontalSpacing(10);                gvBody.setPadding(10, 10, 10, 10);                gvBody.setGravity(Gravity.CENTER);                gvBody.setOnItemClickListener(bodyClick);                mLayout.addView(gvTitle);                mLayout.addView(gvBody);                                //设置默认项                this.setContentView(mLayout);                this.setWidth(LayoutParams.FILL_PARENT);                this.setHeight(LayoutParams.WRAP_CONTENT);                this.setBackgroundDrawable(new ColorDrawable(colorBgTabMenu));// 设置TabMenu菜单背景                this.setAnimationStyle(aniTabMenu);                this.setFocusable(true);// menu菜单获得焦点 如果没有获得焦点menu菜单中的控件事件无法响应        }                        public void SetTitleSelect(int index)        {                gvTitle.setSelection(index);                this.titleAdapter.SetFocus(index);        }                public void SetBodySelect(int index,int colorSelBody)        {                int count=gvBody.getChildCount();                for(int i=0;i<count;i++)                {                        if(i!=index)                                ((LinearLayout)gvBody.getChildAt(i)).setBackgroundColor(Color.TRANSPARENT);                }                ((LinearLayout)gvBody.getChildAt(index)).setBackgroundColor(colorSelBody);        }                public void SetBodyAdapter(MenuBodyAdapter bodyAdapter)        {                gvBody.setAdapter(bodyAdapter);        }                /**         * 自定义Adapter,TabMenu的每个分页的主体         *          */        static public class MenuBodyAdapter extends BaseAdapter {                private Context mContext;                private int fontColor,fontSize;                private String[] texts;                private int[] resID;                /**                 * 设置TabMenu的分页主体                 * @param context 调用方的上下文                 * @param texts 按钮集合的字符串数组                 * @param resID 按钮集合的图标资源数组                 * @param fontSize 按钮字体大小                 * @param color 按钮字体颜色                 */                public MenuBodyAdapter(Context context, String[] texts,int[] resID, int fontSize,int fontColor)                 {                        this.mContext = context;                        this.fontColor = fontColor;                        this.texts = texts;                        this.fontSize=fontSize;                        this.resID=resID;                }                public int getCount() {                        return texts.length;                }                public Object getItem(int position) {                                                return makeMenyBody(position);                }                public long getItemId(int position) {                        return position;                }                                private LinearLayout makeMenyBody(int position)                {                        LinearLayout result=new LinearLayout(this.mContext);                        result.setOrientation(LinearLayout.VERTICAL);                        result.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);                                result.setPadding(10, 10, 10, 10);                                                TextView text = new TextView(this.mContext);                        text.setText(texts[position]);                        text.setTextSize(fontSize);                        text.setTextColor(fontColor);                        text.setGravity(Gravity.CENTER);                        text.setPadding(5, 5, 5, 5);                        ImageView img=new ImageView(this.mContext);                        img.setBackgroundResource(resID[position]);                        result.addView(img,new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)));                        result.addView(text);                        return result;                }                                public View getView(int position, View convertView, ViewGroup parent) {                        return makeMenyBody(position);                }        }                        /**         * 自定义Adapter,TabMenu的分页标签部分         *          */        static public class MenuTitleAdapter extends BaseAdapter {                private Context mContext;        private int fontColor,unselcolor,selcolor;        private TextView[] title;        /**         * 设置TabMenu的title         * @param context 调用方的上下文         * @param titles 分页标签的字符串数组         * @param fontSize 字体大小         * @param fontcolor 字体颜色         * @param unselcolor 未选中项的背景色         * @param selcolor 选中项的背景色         */        public MenuTitleAdapter(Context context, String[] titles, int fontSize,                int fontcolor,int unselcolor,int selcolor) {            this.mContext = context;            this.fontColor = fontcolor;            this.unselcolor = unselcolor;            this.selcolor=selcolor;            this.title = new TextView[titles.length];            for (int i = 0; i < titles.length; i++) {                title = new TextView(mContext);                title.setText(titles);                title.setTextSize(fontSize);                title.setTextColor(fontColor);                title.setGravity(Gravity.CENTER);                title.setPadding(10, 10, 10, 10);            }        }        public int getCount() {            return title.length;        }        public Object getItem(int position) {            return title[position];        }        public long getItemId(int position) {            return title[position].getId();        }        /**         * 设置选中的效果         */        private void SetFocus(int index)        {                for(int i=0;i<title.length;i++)                {                        if(i!=index)                        {                                title.setBackgroundDrawable(new ColorDrawable(unselcolor));//设置没选中的颜色                    title.setTextColor(fontColor);//设置没选中项的字体颜色                        }                }                title[index].setBackgroundColor(0x00);//设置选中项的颜色            title[index].setTextColor(selcolor);//设置选中项的字体颜色        }                public View getView(int position, View convertView, ViewGroup parent) {            View v;            if (convertView == null) {                v = title[position];            } else {                v = convertView;            }            return v;        }        }} 
testTabMenu介绍了数据的定义以及TabMenu的使用,源码如下:
[java] view plaincopyprint?

  • package com.testTabMenu;  
  • import android.app.Activity;  
  • import android.graphics.Color;  
  • import android.os.Bundle;  
  • import android.view.Gravity;  
  • import android.view.Menu;  
  • import android.view.View;  
  • import android.widget.AdapterView;  
  • import android.widget.AdapterView.OnItemClickListener;  
  • import android.widget.Toast;  
  • public class testTabMenu extends Activity {  
  •     TabMenu.MenuBodyAdapter []bodyAdapter=new TabMenu.MenuBodyAdapter[3];  
  •     TabMenu.MenuTitleAdapter titleAdapter;  
  •     TabMenu tabMenu;  
  •     int selTitle=0;  
  •     @Override  
  •     public void onCreate(Bundle savedInstanceState) {  
  •         super.onCreate(savedInstanceState);  
  •         setContentView(R.layout.main);  
  •         //设置分页栏的标题   
  •         titleAdapter = new TabMenu.MenuTitleAdapter(this, new String[] { "常用",  
  •                 "设置", "工具" }, 16, 0xFF222222,Color.LTGRAY,Color.WHITE);  
  •         //定义每项分页栏的内容   
  •         bodyAdapter[0]=new TabMenu.MenuBodyAdapter(this,new String[] { "常用1", "常用2", },   
  •                  new int[] { R.drawable.menu_test,  
  •                 R.drawable.menu_bookmark},13, 0xFFFFFFFF);  
  •            
  •         bodyAdapter[1]=new TabMenu.MenuBodyAdapter(this,new String[] { "设置1", "设置2",  
  •                     "设置3"}, new int[] { R.drawable.menu_edit,  
  •                     R.drawable.menu_delete, R.drawable.menu_fullscreen},13, 0xFFFFFFFF);  
  •            
  •         bodyAdapter[2]=new TabMenu.MenuBodyAdapter(this,new String[] { "工具1", "工具2",  
  •                     "工具3", "工具4" }, new int[] { R.drawable.menu_copy,  
  •                     R.drawable.menu_cut, R.drawable.menu_normalmode,  
  •                     R.drawable.menu_quit },13, 0xFFFFFFFF);  
  •            
  •            
  •         tabMenu=new TabMenu(this,  
  •                  new TitleClickEvent(),  
  •                  new BodyClickEvent(),  
  •                  titleAdapter,  
  •                  0x55123456,//TabMenu的背景颜色   
  •                  R.style.PopupAnimation);//出现与消失的动画   
  •            
  •          tabMenu.update();  
  •          tabMenu.SetTitleSelect(0);  
  •          tabMenu.SetBodyAdapter(bodyAdapter[0]);  
  •     }  
  •       
  •     class TitleClickEvent implements OnItemClickListener{  
  •         @Override  
  •         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  •                 long arg3) {  
  •             selTitle=arg2;  
  •             tabMenu.SetTitleSelect(arg2);  
  •             tabMenu.SetBodyAdapter(bodyAdapter[arg2]);  
  •         }  
  •     }  
  •       
  •     class BodyClickEvent implements OnItemClickListener{  
  •         @Override  
  •         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  •                 long arg3) {  
  •             tabMenu.SetBodySelect(arg2,Color.GRAY);  
  •             String str="第"+String.valueOf(selTitle)+"栏/n/r"  
  •             +"第"+String.valueOf(arg2)+"项";  
  •             Toast.makeText(testTabMenu.this, str, 500).show();  
  •               
  •         }  
  •          
  •     }  
  •     @Override  
  •     /**
  •      * 创建MENU
  •      */  
  •     public boolean onCreateOptionsMenu(Menu menu) {  
  •         menu.add("menu");// 必须创建一项   
  •         return super.onCreateOptionsMenu(menu);  
  •     }  
  •     @Override  
  •     /**
  •      * 拦截MENU
  •      */  
  •     public boolean onMenuOpened(int featureId, Menu menu) {  
  •         if (tabMenu != null) {  
  •             if (tabMenu.isShowing())  
  •                 tabMenu.dismiss();  
  •             else {  
  •                 tabMenu.showAtLocation(findViewById(R.id.LinearLayout01),  
  •                         Gravity.BOTTOM, 0, 0);  
  •             }  
  •         }  
  •         return false;// 返回为true 则显示系统menu   
  •     }  
  •       




  • 本功能的实现主要包括两个页面文件,一个是main.xml的主页面(背景),另一个是弹出页面框。
    Main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/background"
        android:orientation="vertical" >
    </LinearLayout>
    Menu_layout.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        android:orientation="horizontal" >
        <!-- 垂直布局  -->
        <LinearLayout
            android:id="@+id/home"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/button_normal_translucent"
            android:orientation="vertical" >
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingTop="5dp"
                android:src="@drawable/home" />
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text=" 首页 " />
        </LinearLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/button_normal"
            android:gravity="center"
            android:orientation="horizontal" >
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/mine" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" 我的 " />
        </LinearLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/button_normal"
            android:orientation="vertical" >
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingTop="18dp"
                android:src="@drawable/more" />
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:paddingTop="5dp"
                android:text=" 更多 " />
        </LinearLayout>
    </LinearLayout>
    Mainactivity.java
    package mobile.android.ch06.custom.menu;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Display;
    import android.view.Gravity;
    import android.view.KeyEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.PopupWindow;
    import android.widget.Toast;
    public class Main extends Activity
    {
           private PopupWindow pop;
           private View layout;
           /**表示弹出菜单是否出现。1:打开状态,2:不显示状态*/
           private int state = 2;//关闭状态
           public static final int OPENED=1;
           public static final int CLOSED=2;
            Display display=null;//获取屏幕尺寸
           @Override
           public void onCreate(Bundle savedInstanceState)
           {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.main);
                  display=getWindowManager().getDefaultDisplay();//获取屏幕尺寸
           }
      
                         
           @Override//当有键按下的时候
           public boolean onKeyDown(int keyCode, KeyEvent event)
           {
                  switch (keyCode)
                  {
                         case KeyEvent.KEYCODE_MENU:
                                if (state == OPENED)return false;//如果弹出窗口处于活动状态,则再按菜单无效
                                //得到菜单布局对象
                                layout = getLayoutInflater().inflate(R.layout.menu_layout, null);
                                 
                                //制作弹出菜单
                                pop = new PopupWindow(layout,display.getWidth(),display.getHeight());
                                //设置layoutPopupWindow中显示的位置
                                pop.showAtLocation(layout, Gravity.BOTTOM, 0, 0);
                               
                                View home = layout.findViewById(R.id.home);//获取一个菜单
                                home.setOnClickListener(new OnClickListener()//为这个菜单设置点击事件
                                {
                             @Override
                                       public void onClick(View view)
                                       {
                                              Toast.makeText(Main.this, "单击定制菜单.", Toast.LENGTH_LONG).show();
                                              pop.dismiss();//关闭弹出窗口
                                              state = CLOSED;//弹出窗口处于关闭状态
                                       }
                                });
                               
                               
                               state = OPENED;
                                return false;
                //如果菜单处于打开状态,则关闭弹出菜单;如果菜单没有出来,直接关闭应用程序。
                         case KeyEvent.KEYCODE_BACK:
                                if (state == OPENED)
                                {
                                       pop.dismiss();
                                       state = CLOSED;
                                }
                                else if (state == CLOSED)
                                {
                                       finish();
                                }
                                return false;
                  }
                  return super.onKeyDown(keyCode, event);
           }
    }

    1.jpg (949.27 KB, 下载次数: 6)

    1.jpg

     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值