天天看點

仿UCWEB自定義Menu

本文來自http://blog.csdn.net/hellogv/ 

       用過UCWEB-Android版的人都應該對其特殊的menu有印象,把menu做成Tab-Menu(支援分頁的Menu),可以容納比Android傳統的menu更豐富的内容(Android的menu超過6項則縮略在[更多]裡),本文參考網上的例子(作者:CoffeeCole,email:[email protected]),對例子進行簡化以及封裝,使其作為一個複合控件融入自己的framework。

先來看看本文程式運作的效果:

仿UCWEB自定義Menu

TabMenu本身就是一個PopupWindow,PopupWindow上面放了兩個GridView,第一個GridView就是分頁标簽,位于PopupWindow的頂部,第二個GridView是菜單,位于PopupWindow的主體。為了實作PopupWindow的彈出/退出的動畫效果,本文使用了以下代碼:

在工程的res檔案夾裡添加anim子目錄,再建立檔案popup_enter.xml:

建立檔案popup_exit.xml:

在工程的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的源碼如下:

view plain copy to clipboard print ?

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout android:id="@+id/LinearLayout01"  
  3.     android:layout_width="fill_parent" android:layout_height="fill_parent"  
  4.     xmlns:android="http://schemas.android.com/apk/res/android">  
  5.     <TextView android:id="@+id/TextView01" android:layout_height="wrap_content"  
  6.         android:layout_width="fill_parent" android:text="擴充Menu----hellogv"></TextView>  
  7. </LinearLayout>  

TabMenu的封裝類TabMenu.java的源碼如下:

testTabMenu介紹了資料的定義以及TabMenu的使用,源碼如下:

view plain copy to clipboard print ?

  1. package com.testTabMenu;  
  2. import android.app.Activity;  
  3. import android.graphics.Color;  
  4. import android.os.Bundle;  
  5. import android.view.Gravity;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.widget.AdapterView;  
  9. import android.widget.AdapterView.OnItemClickListener;  
  10. import android.widget.Toast;  
  11. public class testTabMenu extends Activity {  
  12.     TabMenu.MenuBodyAdapter []bodyAdapter=new TabMenu.MenuBodyAdapter[3];  
  13.     TabMenu.MenuTitleAdapter titleAdapter;  
  14.     TabMenu tabMenu;  
  15.     int selTitle=0;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         //設定分頁欄的标題  
  21.         titleAdapter = new TabMenu.MenuTitleAdapter(this, new String[] { "常用",  
  22.                 "設定", "工具" }, 16, 0xFF222222,Color.LTGRAY,Color.WHITE);  
  23.         //定義每項分頁欄的内容  
  24.         bodyAdapter[0]=new TabMenu.MenuBodyAdapter(this,new String[] { "常用1", "常用2", },   
  25.                  new int[] { R.drawable.menu_test,  
  26.                 R.drawable.menu_bookmark},13, 0xFFFFFFFF);  
  27.         bodyAdapter[1]=new TabMenu.MenuBodyAdapter(this,new String[] { "設定1", "設定2",  
  28.                     "設定3"}, new int[] { R.drawable.menu_edit,  
  29.                     R.drawable.menu_delete, R.drawable.menu_fullscreen},13, 0xFFFFFFFF);  
  30.         bodyAdapter[2]=new TabMenu.MenuBodyAdapter(this,new String[] { "工具1", "工具2",  
  31.                     "工具3", "工具4" }, new int[] { R.drawable.menu_copy,  
  32.                     R.drawable.menu_cut, R.drawable.menu_normalmode,  
  33.                     R.drawable.menu_quit },13, 0xFFFFFFFF);  
  34.         tabMenu=new TabMenu(this,  
  35.                  new TitleClickEvent(),  
  36.                  new BodyClickEvent(),  
  37.                  titleAdapter,  
  38.                  0x55123456,//TabMenu的背景顔色  
  39.                  R.style.PopupAnimation);//出現與消失的動畫  
  40.          tabMenu.update();  
  41.          tabMenu.SetTitleSelect(0);  
  42.          tabMenu.SetBodyAdapter(bodyAdapter[0]);  
  43.     }  
  44.     class TitleClickEvent implements OnItemClickListener{  
  45.         @Override  
  46.         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  47.                 long arg3) {  
  48.             selTitle=arg2;  
  49.             tabMenu.SetTitleSelect(arg2);  
  50.             tabMenu.SetBodyAdapter(bodyAdapter[arg2]);  
  51.         }  
  52.     }  
  53.     class BodyClickEvent implements OnItemClickListener{  
  54.         @Override  
  55.         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  56.                 long arg3) {  
  57.             tabMenu.SetBodySelect(arg2,Color.GRAY);  
  58.             String str="第"+String.valueOf(selTitle)+"欄/n/r"  
  59.             +"第"+String.valueOf(arg2)+"項";  
  60.             Toast.makeText(testTabMenu.this, str, 500).show();  
  61.         }  
  62.     }  
  63.     @Override  
  64.     public boolean onCreateOptionsMenu(Menu menu) {  
  65.         menu.add("menu");// 必須建立一項  
  66.         return super.onCreateOptionsMenu(menu);  
  67.     }  
  68.     @Override  
  69.     public boolean onMenuOpened(int featureId, Menu menu) {  
  70.         if (tabMenu != null) {  
  71.             if (tabMenu.isShowing())  
  72.                 tabMenu.dismiss();  
  73.             else {  
  74.                 tabMenu.showAtLocation(findViewById(R.id.LinearLayout01),  
  75.                         Gravity.BOTTOM, 0, 0);  
  76.             }  
  77.         }  
  78.         return false;// 傳回為true 則顯示系統menu  
  79.     }  
  80. }  

view plain copy to clipboard print ?

  1. package com.testTabMenu;  
  2. import android.content.Context;  
  3. import android.graphics.Color;  
  4. import android.graphics.drawable.ColorDrawable;  
  5. import android.view.Gravity;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8. import android.widget.BaseAdapter;  
  9. import android.widget.GridView;  
  10. import android.widget.ImageView;  
  11. import android.widget.LinearLayout;  
  12. import android.widget.PopupWindow;  
  13. import android.widget.TextView;  
  14. import android.widget.AdapterView.OnItemClickListener;  
  15. import android.widget.LinearLayout.LayoutParams;  
  16. public class TabMenu extends PopupWindow{  
  17.     private GridView gvBody, gvTitle;  
  18.     private LinearLayout mLayout;  
  19.     private MenuTitleAdapter titleAdapter;  
  20.     public TabMenu(Context context,OnItemClickListener titleClick,OnItemClickListener bodyClick,  
  21.             MenuTitleAdapter titleAdapter,int colorBgTabMenu,int aniTabMenu){  
  22.         super(context);  
  23.         mLayout = new LinearLayout(context);  
  24.         mLayout.setOrientation(LinearLayout.VERTICAL);  
  25.         //标題選項欄  
  26.         gvTitle = new GridView(context);  
  27.         gvTitle.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  
  28.         gvTitle.setNumColumns(titleAdapter.getCount());  
  29.         gvTitle.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);  
  30.         gvTitle.setVerticalSpacing(1);  
  31.         gvTitle.setHorizontalSpacing(1);  
  32.         gvTitle.setGravity(Gravity.CENTER);  
  33.         gvTitle.setOnItemClickListener(titleClick);  
  34.         gvTitle.setAdapter(titleAdapter);  
  35.         gvTitle.setSelector(new ColorDrawable(Color.TRANSPARENT));//選中的時候為透明色  
  36.         this.titleAdapter=titleAdapter;  
  37.         //子選項欄  
  38.         gvBody = new GridView(context);  
  39.         gvBody.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));  
  40.         gvBody.setSelector(new ColorDrawable(Color.TRANSPARENT));//選中的時候為透明色  
  41.         gvBody.setNumColumns(4);  
  42.         gvBody.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);  
  43.         gvBody.setVerticalSpacing(10);  
  44.         gvBody.setHorizontalSpacing(10);  
  45.         gvBody.setPadding(10, 10, 10, 10);  
  46.         gvBody.setGravity(Gravity.CENTER);  
  47.         gvBody.setOnItemClickListener(bodyClick);  
  48.         mLayout.addView(gvTitle);  
  49.         mLayout.addView(gvBody);  
  50.         //設定預設項  
  51.         this.setContentView(mLayout);  
  52.         this.setWidth(LayoutParams.FILL_PARENT);  
  53.         this.setHeight(LayoutParams.WRAP_CONTENT);  
  54.         this.setBackgroundDrawable(new ColorDrawable(colorBgTabMenu));// 設定TabMenu菜單背景  
  55.         this.setAnimationStyle(aniTabMenu);  
  56.         this.setFocusable(true);// menu菜單獲得焦點 如果沒有獲得焦點menu菜單中的控件事件無法響應  
  57.     }  
  58.     public void SetTitleSelect(int index)  
  59.     {  
  60.         gvTitle.setSelection(index);  
  61.         this.titleAdapter.SetFocus(index);  
  62.     }  
  63.     public void SetBodySelect(int index,int colorSelBody)  
  64.     {  
  65.         int count=gvBody.getChildCount();  
  66.         for(int i=0;i<count;i++)  
  67.         {  
  68.             if(i!=index)  
  69.                 ((LinearLayout)gvBody.getChildAt(i)).setBackgroundColor(Color.TRANSPARENT);  
  70.         }  
  71.         ((LinearLayout)gvBody.getChildAt(index)).setBackgroundColor(colorSelBody);  
  72.     }  
  73.     public void SetBodyAdapter(MenuBodyAdapter bodyAdapter)  
  74.     {  
  75.         gvBody.setAdapter(bodyAdapter);  
  76.     }  
  77.     static public class MenuBodyAdapter extends BaseAdapter {  
  78.         private Context mContext;  
  79.         private int fontColor,fontSize;  
  80.         private String[] texts;  
  81.         private int[] resID;  
  82.         public MenuBodyAdapter(Context context, String[] texts,int[] resID, int fontSize,int fontColor)   
  83.         {  
  84.             this.mContext = context;  
  85.             this.fontColor = fontColor;  
  86.             this.texts = texts;  
  87.             this.fontSize=fontSize;  
  88.             this.resID=resID;  
  89.         }  
  90.         public int getCount() {  
  91.             return texts.length;  
  92.         }  
  93.         public Object getItem(int position) {  
  94.             return makeMenyBody(position);  
  95.         }  
  96.         public long getItemId(int position) {  
  97.             return position;  
  98.         }  
  99.         private LinearLayout makeMenyBody(int position)  
  100.         {  
  101.             LinearLayout result=new LinearLayout(this.mContext);  
  102.             result.setOrientation(LinearLayout.VERTICAL);  
  103.             result.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);     
  104.             result.setPadding(10, 10, 10, 10);  
  105.             TextView text = new TextView(this.mContext);  
  106.             text.setText(texts[position]);  
  107.             text.setTextSize(fontSize);  
  108.             text.setTextColor(fontColor);  
  109.             text.setGravity(Gravity.CENTER);  
  110.             text.setPadding(5, 5, 5, 5);  
  111.             ImageView img=new ImageView(this.mContext);  
  112.             img.setBackgroundResource(resID[position]);  
  113.             result.addView(img,new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)));  
  114.             result.addView(text);  
  115.             return result;  
  116.         }  
  117.         public View getView(int position, View convertView, ViewGroup parent) {  
  118.             return makeMenyBody(position);  
  119.         }  
  120.     }  
  121.     static public class MenuTitleAdapter extends BaseAdapter {  
  122.         private Context mContext;  
  123.         private int fontColor,unselcolor,selcolor;  
  124.         private TextView[] title;  
  125.         public MenuTitleAdapter(Context context, String[] titles, int fontSize,  
  126.                 int fontcolor,int unselcolor,int selcolor) {  
  127.             this.mContext = context;  
  128.             this.fontColor = fontcolor;  
  129.             this.unselcolor = unselcolor;  
  130.             this.selcolor=selcolor;  
  131.             this.title = new TextView[titles.length];  
  132.             for (int i = 0; i < titles.length; i++) {  
  133.                 title[i] = new TextView(mContext);  
  134.                 title[i].setText(titles[i]);  
  135.                 title[i].setTextSize(fontSize);  
  136.                 title[i].setTextColor(fontColor);  
  137.                 title[i].setGravity(Gravity.CENTER);  
  138.                 title[i].setPadding(10, 10, 10, 10);  
  139.             }  
  140.         }  
  141.         public int getCount() {  
  142.             return title.length;  
  143.         }  
  144.         public Object getItem(int position) {  
  145.             return title[position];  
  146.         }  
  147.         public long getItemId(int position) {  
  148.             return title[position].getId();  
  149.         }  
  150.         private void SetFocus(int index)  
  151.         {  
  152.             for(int i=0;i<title.length;i++)  
  153.             {  
  154.                 if(i!=index)  
  155.                 {  
  156.                     title[i].setBackgroundDrawable(new ColorDrawable(unselcolor));//設定沒選中的顔色  
  157.                     title[i].setTextColor(fontColor);//設定沒選中項的字型顔色  
  158.                 }  
  159.             }  
  160.             title[index].setBackgroundColor(0x00);//設定選中項的顔色  
  161.             title[index].setTextColor(selcolor);//設定選中項的字型顔色  
  162.         }  
  163.         public View getView(int position, View convertView, ViewGroup parent) {  
  164.             View v;  
  165.             if (convertView == null) {  
  166.                 v = title[position];  
  167.             } else {  
  168.                 v = convertView;  
  169.             }  
  170.             return v;  
  171.         }  
  172.     }  
  173. }  

view plain copy to clipboard print ?

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

view plain copy to clipboard print ?

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