• Android进阶篇自定义Menu(设置Menu的背景及文字属性)


    系统自带的Menu有各种限制条件,如何设置Menu的背景和文字的各项属性呢?在不自定义的情况下,也是可以设置Menu的背景的。

    /** 设置Menu的背景图 */
        protected void setMenuBackground() {
            this.getLayoutInflater().setFactory(
                    new android.view.LayoutInflater.Factory() {
                        public View onCreateView(String name, Context context,AttributeSet attrs) {
                            // 指定自定义inflate的对象
                            if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
                                try {
                                    LayoutInflater f = getLayoutInflater();
                                    final View view = f.createView(name, null,attrs);
                                    new Handler().post(new Runnable() {
                                        public void run() {
                                            // 设置背景图片
                                            view.setBackgroundResource(R.color.menu);
                                        }
                                    });
                                    return view;
                                } catch (InflateException e) {
                                    e.printStackTrace();
                                } catch (ClassNotFoundException e) {
                                    e.printStackTrace();
                                }
                            }
                            return null;    
                        }
                    }
                );
        }

    但是设置Menu显示文字的各项属性就比较麻烦了,为了更好的解决这些问题,我们最好还是采用自定义Menu的方法。

     自定义Menu就是一个自定义的PopWindow:

    public class TabMenu extends PopupWindow{
        private LinearLayout mLayout;
        private ImageView mImageView;
        private TextView mTextView;
        
        /**
         * @param context 上下文
         * @param onClickListener 单击事件 
         * @param resID 图片资源
         * @param text 显示的文字
         * @param fontSize 显示的文字大小
         * @param fontColor 文字的颜色 
         * @param colorBgTabMenu 背景颜色
         * @param aniTabMenu 消失的动画
         * @return
         */
        public TabMenu(Context context,OnClickListener onClickListener,int resID,String text,int fontSize,
                int fontColor,int colorBgTabMenu,int aniTabMenu){
            super(context);
            
            mLayout=new LinearLayout(context);  
            mLayout.setOrientation(LinearLayout.VERTICAL);  
            mLayout.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);     
            mLayout.setPadding(10, 10, 10, 10);  
              
            mTextView = new TextView(context);  
            mTextView.setTextSize((context.getResources().getDimensionPixelSize(fontSize)));  
            mTextView.setTextColor((context.getResources().getColor(fontColor)));  
            mTextView.setText(text);
            mTextView.setGravity(Gravity.CENTER);  
            mTextView.setPadding(5, 5, 5, 5);  
            
            mImageView=new ImageView(context);  
            mImageView.setBackgroundResource(resID);  
            
            mLayout.addView(mImageView,new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)));  
            mLayout.addView(mTextView);
            mLayout.setOnClickListener(onClickListener);
            
            this.setContentView(mLayout);  
            this.setWidth(LayoutParams.FILL_PARENT);  
            this.setHeight(LayoutParams.WRAP_CONTENT);  
            this.setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(colorBgTabMenu)));  
            this.setAnimationStyle(aniTabMenu);  
            this.setFocusable(true);
        }
    }

    而相应的在主界面,我们也应该进行Menu拦截操作。

    private TabMenu tabMenu;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            tabMenu = new TabMenu(this, null, R.drawable.ic_launcher, "设置", 
                    R.dimen.middle_text_size,R.color.blue,R.color.white,R.style.PopupAnimation);
        }
        
        
        /** 创建MENU */  
        public boolean onCreateOptionsMenu(Menu menu) {  
            menu.add("menu");// 必须创建一项  
            return super.onCreateOptionsMenu(menu);  
        }  
        
        /** 拦截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  
        } 

    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>

    popup_enter.xml:

    <?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>

     

     

     

     

  • 相关阅读:
    用优先级队列实现先进先出队列;
    c#入门经典(第三版) 练习6.8(5)
    请给出一个时间为O(nlgk)、用来将k个已排序链表的算法。此处n为所有输入链表中元素的总数。
    计数排序
    Heap_delete(A,i)操作将结点i中的想从堆A中删去。对含n个元素的最大堆,请给出时间为O(lgn)的HEAPDELETE的实现。
    堆排序
    请给出一个算法,使之对于给定的介于0到k之间的n个整数进行预处理,并能在O(1)时间内,回答出输入的整数中有多少个落在区间[a..b]内,你给出的算法上预处理时间应是O(n+k)。
    sql存储过程传多个id查询,使用in
    SQL使用语句修改列及表名
    泛型约束使用?有些不知道叫什么好!
  • 原文地址:https://www.cnblogs.com/gongcb/p/2494554.html
Copyright © 2020-2023  润新知