你好,游客 登录 注册 搜索
背景:
阅读新闻

Android PopupWindow重写系统菜单

[日期:2011-11-17] 来源:Linux社区  作者:maylian7700 [字体:大 中 小]
下面是自定义的菜单项,扩展性不是很好,只是把常用的写进去了。
  1. package com.jacp.menu.view;  
  2.   
  3. import Android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.BitmapFactory;  
  6. import android.graphics.Point;  
  7. import android.text.TextUtils;  
  8. import android.view.Gravity;  
  9. import android.view.View;  
  10. import android.view.ViewGroup;  
  11. import android.widget.AbsListView;  
  12. import android.widget.ImageView;  
  13. import android.widget.LinearLayout;  
  14. import android.widget.TextView;  
  15.   
  16. /** 
  17.  * 菜单项 
  18.  * @author maylian7700@126.com 
  19.  * 
  20.  */  
  21. public class MenuItem  
  22. {  
  23.     private LinearLayout mLayout;  
  24.       
  25.     /** 
  26.      * 文本在图片的下方显示 
  27.      */  
  28.     public static final int TEXT_BOTTOM = 0x0;  
  29.       
  30.     /** 
  31.      * 文本在图片的上方显示 
  32.      */  
  33.     public static final int TEXT_TOP = 0x1;  
  34.       
  35.     /** 
  36.      * 文本在图片的左边显示 
  37.      */  
  38.     public static final int TEXT_LEFT = 0x2;  
  39.       
  40.     /** 
  41.      * 文本在图片的右边显示 
  42.      */  
  43.     public static final int TEXT_RIGHT = 0x3;  
  44.       
  45.     /** 
  46.      * 文本的对齐方式 
  47.      */  
  48.     private int mAlign = TEXT_BOTTOM;  
  49.       
  50.     /** 
  51.      * 文本 
  52.      */  
  53.     private String mText;  
  54.       
  55.     /** 
  56.      * 文本颜色 
  57.      */  
  58.     private int mTextColor;  
  59.       
  60.     /** 
  61.      * 文本大小 
  62.      */  
  63.     private int mTextSize;  
  64.       
  65.     /** 
  66.      * 图片的资源ID 
  67.      */  
  68.     private int mImgRes;  
  69.     private Context mContext;  
  70.       
  71.     public MenuItem(Context context)  
  72.     {  
  73.         this(context, 0, null, 0, 0, TEXT_BOTTOM);  
  74.     }  
  75.       
  76.     public MenuItem(Context context, int imgRes, String text, int textColor, int textSize, int align)  
  77.     {  
  78.         mImgRes = imgRes;  
  79.         mText = text;  
  80.         mTextColor = textColor;  
  81.         mTextSize = textSize;  
  82.         mAlign = align;  
  83.         mContext = context;  
  84.     }  
  85.       
  86.     public MenuItem(Context context, MenuItem item)  
  87.     {  
  88.         this(context, 0, null, item.getTextColor(), item.getTextSize(), item.getAlign());  
  89.     }  
  90.       
  91.     /** 
  92.      * 初始化菜单项 
  93.      * @param context 
  94.      */  
  95.     private void initlayout()  
  96.     {  
  97.         Context context = mContext;  
  98.         mLayout = new LinearLayout(context);  
  99.         mLayout.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));  
  100.         mLayout.setGravity(Gravity.CENTER);  
  101.         TextView textView = getTextView(context);  
  102.         ImageView imageView = getImageView(context);  
  103.         if (null != textView && null != imageView)  
  104.         {  
  105.             Point point = getImageDimension(context, mImgRes);  
  106.             switch (mAlign)  
  107.             {  
  108.             case TEXT_BOTTOM: // 文本居下   
  109.                 mLayout.setOrientation(LinearLayout.VERTICAL);  
  110.                 mLayout.addView(imageView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, point.y));  
  111.                 mLayout.addView(textView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));  
  112.                 break;  
  113.             case TEXT_TOP:// 文本居上   
  114.                 mLayout.setOrientation(LinearLayout.VERTICAL);  
  115.                 mLayout.addView(textView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));  
  116.                 mLayout.addView(imageView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, point.y));  
  117.                 break;  
  118.             case TEXT_LEFT:// 文本居左   
  119.                 mLayout.setOrientation(LinearLayout.HORIZONTAL);  
  120.                 mLayout.addView(textView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));  
  121.                 mLayout.addView(imageView, new ViewGroup.LayoutParams(point.x, ViewGroup.LayoutParams.MATCH_PARENT));  
  122.                 break;  
  123.             case TEXT_RIGHT:// 文本居右   
  124.                 mLayout.setOrientation(LinearLayout.HORIZONTAL);  
  125.                 mLayout.addView(imageView, new ViewGroup.LayoutParams(point.x, ViewGroup.LayoutParams.MATCH_PARENT));  
  126.                 mLayout.addView(textView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));  
  127.                 break;  
  128.             }  
  129.         }  
  130.         else  
  131.         {  
  132.             if (null != textView)  
  133.             {  
  134.                 mLayout.addView(textView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));  
  135.             }  
  136.             else if (null != imageView)  
  137.             {  
  138.                 Point point = getImageDimension(context, mImgRes);  
  139.                 mLayout.addView(imageView, new ViewGroup.LayoutParams(point.x, point.y));  
  140.             }  
  141.         }  
  142.     }  
  143.       
  144.     /** 
  145.      * 获取图片的尺寸 
  146.      * @param context 
  147.      * @param res 
  148.      * @return 
  149.      */  
  150.     private Point getImageDimension(Context context, int res)  
  151.     {  
  152.         Point point = new Point();  
  153.         Bitmap bm = BitmapFactory.decodeResource(context.getResources(), mImgRes);  
  154.         point.x = bm.getWidth();  
  155.         point.y = bm.getHeight();  
  156.         bm.recycle();  
  157.         bm = null;  
  158.         return point;  
  159.     }  
  160.       
  161.     /** 
  162.      * 设置图片资源 
  163.      * @param imgRes 
  164.      */  
  165.     public void setImageRes(int imgRes)  
  166.     {  
  167.         mImgRes = imgRes;  
  168.     }  
  169.       
  170.     /** 
  171.      * 设置文本大小 
  172.      * @param size 
  173.      */  
  174.     public void setTextSize(int size)  
  175.     {  
  176.         mTextSize = size;  
  177.     }  
  178.       
  179.     /** 
  180.      * 设置文本颜色 
  181.      * @param color 
  182.      */  
  183.     public void setTextColor(int color)  
  184.     {  
  185.         mTextColor = color;  
  186.     }  
  187.       
  188.     /** 
  189.      * 设置文本内容 
  190.      * @param text 
  191.      */  
  192.     public void setText(String text)  
  193.     {  
  194.         mText = text;  
  195.     }  
  196.       
  197.     /** 
  198.      * 设置文本的对齐方式 
  199.      * @param align 
  200.      */  
  201.     public void setTextAlign(int align)  
  202.     {  
  203.         mAlign = align;  
  204.     }  
  205.       
  206.     public String getText()  
  207.     {  
  208.         return mText;  
  209.     }  
  210.       
  211.     public int getTextColor()  
  212.     {  
  213.         return mTextColor;  
  214.     }  
  215.       
  216.     public int getTextSize()  
  217.     {  
  218.         return mTextSize;  
  219.     }  
  220.       
  221.     /** 
  222.      * 创建TextView 
  223.      * @param context 
  224.      * @return 
  225.      */  
  226.     private TextView getTextView(Context context)  
  227.     {  
  228.         if (TextUtils.isEmpty(mText))  
  229.         {  
  230.             return null;  
  231.         }  
  232.         TextView txtView = new TextView(context);  
  233.         if (mTextColor != 0)  
  234.         {  
  235.             txtView.setTextColor(mTextColor);  
  236.         }  
  237.           
  238.         if (mTextSize != 0)  
  239.         {  
  240.             txtView.setTextSize(mTextSize);  
  241.         }  
  242.         txtView.setText(mText);  
  243.         txtView.setGravity(Gravity.CENTER);  
  244.         return txtView;  
  245.     }  
  246.       
  247.     public int getAlign()  
  248.     {  
  249.         return mAlign;  
  250.     }  
  251.       
  252.     /** 
  253.      * 创建ImageView 
  254.      * @param context 
  255.      * @return 
  256.      */  
  257.     private ImageView getImageView(Context context)  
  258.     {  
  259.         if (mImgRes == 0)  
  260.         {  
  261.             return null;  
  262.         }  
  263.         ImageView view = new ImageView(context);  
  264.         view.setImageResource(mImgRes);  
  265.         return view;  
  266.     }  
  267.       
  268.     /** 
  269.      * 获取菜单项目的内容 
  270.      * @return 
  271.      */  
  272.     public View getView()  
  273.     {  
  274.         initlayout();  
  275.         return mLayout;  
  276.     }  
  277.   
  278. }  
最后一个是把数据适配进去:
  1. package com.jacp.menu.view;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.BaseAdapter;  
  8.   
  9. /** 
  10.  *  
  11.  * @author maylian7700@126.com 
  12.  * 
  13.  */  
  14. public class MenuAdapter extends BaseAdapter  
  15. {  
  16.     private ArrayList<MenuItem> mMenuItems;  
  17.     public MenuAdapter(ArrayList<MenuItem> menuItems)  
  18.     {  
  19.         mMenuItems = menuItems;  
  20.     }  
  21.       
  22.     @Override  
  23.     public int getCount()  
  24.     {  
  25.         return mMenuItems.size();  
  26.     }  
  27.   
  28.     @Override  
  29.     public Object getItem(int position)  
  30.     {  
  31.         return position;  
  32.     }  
  33.   
  34.     @Override  
  35.     public long getItemId(int position)  
  36.     {  
  37.         return position;  
  38.     }  
  39.   
  40.     @Override  
  41.     public View getView(int position, View convertView, ViewGroup parent)  
  42.     {  
  43.         if (null == convertView)  
  44.         {  
  45.             convertView = mMenuItems.get(position).getView();  
  46.         }  
  47.           
  48.         return convertView;  
  49.     }  
  50.   
  51. }  

另外还有动画配置,本来打算全部用代码搞定,结果发现给PopupWindow设置动画用代码搞不定,下面是给PopupWindow添加动画的配置:

在res\values\styles.xml文件中添加以下代码:

  1. <resources>  
  2.     <style name="popup_in_out" parent="android:Animation">  
  3.         <item name="android:windowEnterAnimation">@anim/anim_down_in</item>  
  4.         <item name="android:windowExitAnimation">@anim/anim_down_out</item>  
  5.     </style>  
  6. </resources>  
再在res\anim目录新建两个文件anim_down_in.xml和anim_down_out.xml,分别对应以下代码:

anim_down_in.xml

  1. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  2.     <translate android:fromYDelta="100%" android:toYDelta="0%"  
  3.         android:duration="300">  
  4.     </translate>  
  5. </set>  
anim_down_out.xml
  1. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  2.     <translate android:fromYDelta="0%" android:toYDelta="100%"  
  3.         android:duration="300">  
  4.     </translate>  
  5. </set>  

下面是一个测试的Activity,如果要设置菜单背景透明,只要将R.drawable.bg做成透明图片即可:

  1. package com.jacp.menu;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.KeyEvent;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.AdapterView;  
  9.   
  10. import com.jacp.menu.view.MenuView;  
  11. import com.jacp.menu.view.MenuView.OnMenuItemClickListener;  
  12.   
  13. public class MyBottomMenuActivityActivity extends Activity implements OnClickListener {  
  14.       
  15.     private final int[] mImgRes = { R.drawable.cancel, R.drawable.cancel, R.drawable.cancel, R.drawable.cancel, R.drawable.cancel, R.drawable.cancel, R.drawable.cancel, R.drawable.cancel, R.drawable.cancel };  
  16.       
  17.     private MenuView mMenuView;  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.           
  23.         findViewById(R.id.show_menu).setOnClickListener(this);  
  24.     }  
  25.       
  26.     private void showMenu()  
  27.     {  
  28.         if (null == mMenuView)  
  29.         {  
  30.             mMenuView = new MenuView(this);  
  31. //          mMenuView.setWidth(300);   
  32.             mMenuView.setImageRes(mImgRes);  
  33.             mMenuView.setAnimStyle(R.style.popup_in_out);  
  34.             mMenuView.setBackgroundResource(R.drawable.bg);  
  35.             mMenuView.setText(getResources().getStringArray(R.array.menu_text));  
  36.             mMenuView.setOnMenuItemClickListener(new OnMenuItemClickListener()  
  37.             {  
  38.                 @Override  
  39.                 public void onMenuItemClick(AdapterView<?> parent, View view,  
  40.                         int position)  
  41.                 {  
  42.                       
  43.                 }  
  44.   
  45.                 @Override  
  46.                 public void hideMenu()  
  47.                 {  
  48.                     mMenuView.dismiss();  
  49.                     mMenuView = null;  
  50.                 }  
  51.                   
  52.             });  
  53.         }  
  54.         mMenuView.show();  
  55.     }  
  56.       
  57.     @Override  
  58.     public void onClick(View v)  
  59.     {  
  60.         switch (v.getId())  
  61.         {  
  62.         case R.id.show_menu:  
  63.             showMenu();  
  64.             break;  
  65.         }  
  66.     }  
  67.   
  68.     @Override  
  69.     public boolean onKeyDown(int keyCode, KeyEvent event)  
  70.     {  
  71.         if (event.getAction() == KeyEvent.ACTION_DOWN)  
  72.         {  
  73.             switch (keyCode)  
  74.             {  
  75.             case KeyEvent.KEYCODE_MENU:  
  76.                 showMenu();  
  77.                 break;  
  78.             }  
  79.         }  
  80.         return super.onKeyDown(keyCode, event);  
  81.     }  
  82.       
linux
相关资讯       Android基础教程  Android开发教程 
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* ggg 发表于 2012/8/29 16:34:49
ggg