Android實(shí)現(xiàn)擴(kuò)展Menu的方法
本文實(shí)例講述了Android實(shí)現(xiàn)擴(kuò)展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; /**可擴(kuò)展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); //標(biāo)題選項(xiàng)欄 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));//選中的時(shí)候?yàn)橥该魃? this.titleAdapter=titleAdapter; //子選項(xiàng)欄 gvBody = new GridView(context); gvBody.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); gvBody.setSelector(new ColorDrawable(Color.TRANSPARENT));//選中的時(shí)候?yàn)橥该魃? 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); //設(shè)置默認(rèn)項(xiàng) this.setContentView(mLayout); this.setWidth(LayoutParams.FILL_PARENT); this.setHeight(LayoutParams.WRAP_CONTENT); this.setBackgroundDrawable(new ColorDrawable(colorBgTabMenu));// 設(shè)置TabMenu菜單背景 this.setAnimationStyle(aniTabMenu); this.setFocusable(true);// menu菜單獲得焦點(diǎn) 如果沒有獲得焦點(diǎn)menu菜單中的控件事件無法響應(yīng) } 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的每個(gè)分頁的主體 * */ static public class MenuBodyAdapter extends BaseAdapter { private Context mContext; private int fontColor,fontSize; private String[] texts; private int[] resID; /** * 設(shè)置TabMenu的分頁主體 * @param context 調(diào)用方的上下文 * @param texts 按鈕集合的字符串?dāng)?shù)組 * @param resID 按鈕集合的圖標(biāo)資源數(shù)組 * @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的分頁標(biāo)簽部分 * */ static public class MenuTitleAdapter extends BaseAdapter { private Context mContext; private int fontColor,unselcolor,selcolor; private TextView[] title; /** * 設(shè)置TabMenu的title * @param context 調(diào)用方的上下文 * @param titles 分頁標(biāo)簽的字符串?dāng)?shù)組 * @param fontSize 字體大小 * @param fontcolor 字體顏色 * @param unselcolor 未選中項(xiàng)的背景色 * @param selcolor 選中項(xiàng)的背景色 */ 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(); } /** * 設(shè)置選中的效果 */ private void SetFocus(int index) { for(int i=0;i<title.length;i++) { if(i!=index) { title[i].setBackgroundDrawable(new ColorDrawable(unselcolor));//設(shè)置沒選中的顏色 title[i].setTextColor(fontColor);//設(shè)置沒選中項(xiàng)的字體顏色 } } title[index].setBackgroundColor(0x00);//設(shè)置選中項(xiàng)的顏色 title[index].setTextColor(selcolor);//設(shè)置選中項(xiàng)的字體顏色 } 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; /** * 活動(dòng)界面 * @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); //設(shè)置分頁欄的標(biāo)題 titleAdapter = new TabMenu.MenuTitleAdapter(this, new String[] { "常用", "設(shè)置", "工具" }, 16, 0xFF222222,Color.LTGRAY,Color.WHITE); //定義每項(xiàng)分頁欄的內(nèi)容 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[] { "設(shè)置1", "設(shè)置2", "設(shè)置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);//出現(xiàn)與消失的動(dòng)畫 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)+"項(xiàng)"; Toast.makeText(Activity01.this, str, 500).show(); } } @Override /** * 創(chuàng)建MENU */ public boolean onCreateOptionsMenu(Menu menu) { menu.add("menu");// 必須創(chuàng)建一項(xiàng) 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 則顯示系統(tǒng)menu } }
3. 運(yùn)行結(jié)果:
希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
- Android中捕捉menu按鍵點(diǎn)擊事件的方法
- Android中ActionBar以及menu的代碼設(shè)置樣式
- Android仿微信菜單(Menu)(使用C#和Java分別實(shí)現(xiàn))
- android編程之menu按鍵功能實(shí)現(xiàn)方法
- Android創(chuàng)建Menu菜單實(shí)例
- Android學(xué)習(xí)筆記——Menu介紹(三)
- Android學(xué)習(xí)筆記——Menu介紹(二)
- Android學(xué)習(xí)筆記——Menu介紹(一)
- Android提高之自定義Menu(TabMenu)實(shí)現(xiàn)方法
- Android動(dòng)態(tài)添加menu菜單的簡(jiǎn)單方法
相關(guān)文章
5種Android數(shù)據(jù)存儲(chǔ)方式匯總
這篇文章主要為大家整理了5種Android數(shù)據(jù)存儲(chǔ)方式,列出了各存儲(chǔ)方式的優(yōu)缺點(diǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Android開發(fā)學(xué)習(xí)筆記 Gallery和GridView淺析
這篇文章主要介紹了Android開發(fā)學(xué)習(xí)筆記 Gallery和GridView淺析,需要的朋友可以參考下2014-11-11Flutter 狀態(tài)管理scoped model源碼解讀
這篇文章主要為大家介紹了Flutter 狀態(tài)管理scoped model源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11SwipeRefreshLayout+RecyclerView實(shí)現(xiàn)上拉刷新和下拉刷新功能
這篇文章主要介紹了SwipeRefreshLayout+RecyclerView實(shí)現(xiàn)上拉刷新和下拉刷新功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Flutter?webview與網(wǎng)頁通訊交互實(shí)現(xiàn)
最近要在Flutter項(xiàng)目的基礎(chǔ)上加一個(gè)實(shí)時(shí)定位的功能,下面這篇文章主要給大家介紹了關(guān)于Flutter?webview與網(wǎng)頁通訊交互實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04Android Studio中快捷鍵實(shí)現(xiàn)try catch等功能包含代碼塊的實(shí)現(xiàn)方法
這篇文章主要介紹了 Android Studio中快捷鍵實(shí)現(xiàn)try catch等功能包含代碼塊的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09Android基于注解的6.0權(quán)限動(dòng)態(tài)請(qǐng)求框架詳解
這篇文章主要介紹了Android基于注解的6.0權(quán)限動(dòng)態(tài)請(qǐng)求框架詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04