Android實現擴充Menu的方法_Android

來源:互聯網
上載者:User

本文執行個體講述了Android實現擴充Menu的方法。分享給大家供大家參考。具體如下:

1. java代碼:

package com.tabmenu;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;/**可擴充menu *  * @author antking **/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[i] = new TextView(mContext);    title[i].setText(titles[i]);    title[i].setTextSize(fontSize);    title[i].setTextColor(fontColor);    title[i].setGravity(Gravity.CENTER);    title[i].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[i].setBackgroundDrawable(new ColorDrawable(unselcolor));//設定沒選中的顏色     title[i].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;  } }}

2. java代碼:

package com.tabmenu;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;/** * 活動介面 * @author wei * */public class Activity01 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[] { android.R.drawable.alert_dark_frame,    android.R.drawable.ic_delete},13, 0xFFFFFFFF);  bodyAdapter[1]=new TabMenu.MenuBodyAdapter(this,new String[] { "設定1", "設定2",     "設定3"}, new int[] { android.R.drawable.ic_menu_edit,     android.R.drawable.btn_default,android.R.drawable.btn_dropdown},13, 0xFFFFFFFF);  bodyAdapter[2]=new TabMenu.MenuBodyAdapter(this,new String[] { "工具1", "工具2",     "工具3", "工具4" }, new int[] { android.R.drawable.ic_media_ff,    android.R.drawable.ic_menu_delete, android.R.drawable.ic_btn_speak_now,     android.R.drawable.edit_text },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(Activity01.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 }}

3. 運行結果:

希望本文所述對大家的Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.