Android提高十八篇之自定义Menu(TabMenu) [转]

阅读更多
本文来自 http://blog.csdn.net/hellogv/ ,引用必须注明出处!
用过UCWEB-Android版的人都应该对其特殊的menu有印象,把menu做成Tab-Menu(支持分页的Menu),可以容纳比Android传统的menu更丰富的内容(Android的menu超过6项则缩略在[更多]里),本文参考网上的例子(作者:CoffeeCole,email:[email protected]),对例子进行简化以及封装,使其作为一个复合控件融入自己的framework。

先来看看本文程序运行的效果:

Android提高十八篇之自定义Menu(TabMenu) [转]_第1张图片

TabMenu本身就是一个PopupWindow,PopupWindow上面放了两个GridView,第一个GridView就是分页标签,位于PopupWindow的顶部,第二个GridView是菜单,位于PopupWindow的主体。为了实现PopupWindow的弹出/退出的动画效果,本文使用了以下代码:

在工程的res文件夹里添加anim子目录,再新建文件popup_enter.xml:

  
  
      
      
   


新建文件popup_exit.xml:

  
  
      
      
   


在工程的values文件夹里新建文件popup_animation.xml:

  
     
      
 



main.xml的源码如下:

  
  
      
  


TabMenu的封装类TabMenu.java的源码如下:
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 
 

testTabMenu介绍了数据的定义以及TabMenu的使用,源码如下:
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   
    }  
      
}  



其它自定义menu相关文章:
  • Android中自定义Menu
  • http://edu.codepub.com/2011/0731/32385.php
  • Android提高十八篇之自定义Menu(TabMenu) [转]_第2张图片
  • 大小: 529.3 KB
  • 查看图片附件

你可能感兴趣的:(android,ucweb)