ActivityGroup是Activity的子类,在Activity的基础上还扩充了一些东西,如可以让多个Activity程序在同一界面上运行且互不干扰。这种功能在menu比较多的地方使用起来会很方便,用途也很广泛。下面是自己整理的一些代码供大家学习使用。
1.一般来说菜单一般都包含图片,实现这种功能一般都会用到适配器,该适配器继承BaseAdapter,代码如下
package org.lxh.activity; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; public class MenuImageAdapter extends BaseAdapter{ private Context context; //所有标签的图片显示 private ImageView[] menuImg; //选中的ImageView索引 private int selectedMenuImg; public MenuImageAdapter(Context context,int imgIds[],int width,int height,int selectedMenuImg){ this.context=context; this.selectedMenuImg=selectedMenuImg; this.menuImg=new ImageView[imgIds.length]; for(int x=0;x<imgIds.length;x++){ //实例化ImageView对象 this.menuImg[x]=new ImageView(this.context); //定义图片的布局参数 this.menuImg[x].setLayoutParams(new GridView.LayoutParams(width,height)); this.menuImg[x].setAdjustViewBounds(false); this.menuImg[x].setPadding(3, 3, 3, 3); this.menuImg[x].setImageResource(imgIds[x]); } } public int getCount() { // TODO Auto-generated method stub return this.menuImg.length; } public Object getItem(int position) { // TODO Auto-generated method stub return this.menuImg[position]; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imgView=null; if(imgView==null){ imgView=this.menuImg[position]; }else{ imgView=(ImageView)convertView; } return imgView; } public void setFocus(int selid){ for(int x=0;x<this.menuImg.length;x++){ if(x!=selid){ this.menuImg[x].setBackgroundResource(0); } } this.menuImg[selid].setBackgroundResource(this.selectedMenuImg); } }
package org.lxh.activity; import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class PopupMenuTitleAdapter extends BaseAdapter { private TextView menuTitle[] = null; // 文字显示组件 private int fontColor; private int selectedColor; private int unSelectedColor; public PopupMenuTitleAdapter(Context context, int[] titleIds, int fontColor, int fontSize, int selectedColor, int unSelectedColor) { this.fontColor = fontColor; this.selectedColor = selectedColor; this.unSelectedColor = unSelectedColor; this.menuTitle = new TextView[titleIds.length]; for (int x = 0; x < titleIds.length; x++) { this.menuTitle[x] = new TextView(context); this.menuTitle[x].setText(titleIds[x]); this.menuTitle[x].setTextSize(fontSize); this.menuTitle[x].setGravity(Gravity.CENTER); this.menuTitle[x].setPadding(10, 10, 10, 10); } } @Override public int getCount() { return this.menuTitle.length; } @Override public Object getItem(int position) { return this.menuTitle[position]; } @Override public long getItemId(int position) { return this.menuTitle[position].getId(); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = null; if (convertView == null) { view = this.menuTitle[position]; } else { view = convertView; } return view; } public void setFocus(int index) { for (int x = 0; x < this.menuTitle.length; x++) { if (x != index) { this.menuTitle[x].setBackgroundDrawable(new ColorDrawable( this.unSelectedColor)); this.menuTitle[x].setTextColor(fontColor); } } this.menuTitle[index].setBackgroundColor(0x00); this.menuTitle[index].setTextColor(this.selectedColor); } }下面是弹出菜单的内容适配器,里面包含了多个Activity.
package org.lxh.activity; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; public class PopupMenuBodyAdapter extends BaseAdapter { private ImageView[] menuImg = null ; public PopupMenuBodyAdapter(Context context, int [] picIds) { this.menuImg = new ImageView[picIds.length]; for (int x = 0; x < this.menuImg.length; x++) { this.menuImg[x] = new ImageView(context) ; this.menuImg[x].setImageResource(picIds[x]) ; } } @Override public int getCount() { return this.menuImg.length; } @Override public Object getItem(int position) { return this.menuImg[position]; } @Override public long getItemId(int position) { return this.menuImg[position].getId(); } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = null ; if (convertView == null) { view = this.menuImg[position]; } else { view = convertView; } return view; } }
package org.lxh.activity; import android.content.Context; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.view.Gravity; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.PopupWindow; public class PopMenu extends PopupWindow { private GridView popTitle ; // 表示标题 private GridView popBody ; // 标题主体 private PopupMenuTitleAdapter titleAdapter = null ; private LinearLayout layout = null ; public PopMenu(Context context, int titleIds[],int backgroundColor, OnItemClickListener titleCallback, OnItemClickListener bodyCallback) { super(context); // 还是需要创建弹出的组件 this.titleAdapter = new PopupMenuTitleAdapter(context, titleIds, 0xFF222222, 16, Color.LTGRAY, Color.WHITE); this.layout = new LinearLayout(context) ; this.layout.setOrientation(LinearLayout.VERTICAL) ; this.popTitle = new GridView(context); this.popTitle.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); this.popTitle.setNumColumns(titleIds.length) ; this.popTitle.setHorizontalSpacing(1) ; this.popTitle.setVerticalSpacing(1) ; this.popTitle.setStretchMode(GridView.STRETCH_COLUMN_WIDTH); // 拉申列宽 this.popTitle.setAdapter(this.titleAdapter) ; this.popTitle.setOnItemClickListener(titleCallback) ; this.popBody = new GridView(context) ; this.popBody.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); this.popBody.setNumColumns(5) ; this.popBody.setHorizontalSpacing(1) ; this.popBody.setVerticalSpacing(1) ; this.popBody.setPadding(10, 10, 10, 10) ; this.popBody.setGravity(Gravity.CENTER) ; this.popBody.setStretchMode(GridView.STRETCH_COLUMN_WIDTH); // 拉申列宽 this.popBody.setOnItemClickListener(bodyCallback) ; this.layout.addView(this.popTitle) ; this.layout.addView(this.popBody) ; super.setContentView(this.layout) ; super.setWidth(LayoutParams.FILL_PARENT) ; super.setHeight(LayoutParams.WRAP_CONTENT) ; super.setBackgroundDrawable(new ColorDrawable(backgroundColor)) ; super.setFocusable(true) ; // 允许获得焦点 } public void setPopupMenuBodyAdapter(PopupMenuBodyAdapter adapter) { this.popBody.setAdapter(adapter) ; } public void setPopTitleSelected(int postion) { this.popTitle.setSelection(postion) ; this.titleAdapter.setFocus(postion) ; } public void setPopBodySelected(int position,int selectedColor) { // 设置选中后的颜色 int count = this.popBody.getChildCount() ; for (int x = 0; x < count; x++) { if (x != position) { ImageView img = (ImageView) this.popBody.getChildAt(x) ; img.setBackgroundColor(Color.TRANSPARENT) ; } } ImageView img = (ImageView) this.popBody.getChildAt(position) ; img.setBackgroundColor(selectedColor) ; } }
3.后面就是主要的activity了。此时我们需要显示一个菜单,之后给第四个菜单加弹出式菜单,并在弹出菜单切换程序
package org.lxh.activity; import android.app.ActivityGroup; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.view.Gravity; import android.view.KeyEvent; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.view.Window; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.LinearLayout; import android.widget.Toast; public class PopMenuActivity extends ActivityGroup { private GridView gridviewToolbar; // 工具菜单栏 private MenuImageAdapter menu = null; // 图片适配器 private LinearLayout content = null; // 填充内容 private int menu_img[] = new int[] { R.drawable.menu_main, R.drawable.menu_news, R.drawable.menu_sms, R.drawable.menu_more, R.drawable.menu_exit }; // 填充的图片的资源 private int width = 0; // 求出平均的宽度 private int height = 0; // 求出平均的高度,定位显示 private Intent intent = null; private boolean isShow = false ; private int commonItemIds[] = new int[] { R.drawable.common_account, R.drawable.common_addmark, R.drawable.common_download, R.drawable.common_exit, R.drawable.common_fullscreen, R.drawable.common_history, R.drawable.common_night, R.drawable.common_refresh }; private int setItemIds[] = new int[] { R.drawable.set_button, R.drawable.set_mode, R.drawable.set_nophoto, R.drawable.set_rotation, R.drawable.set_scroll, R.drawable.set_skin, R.drawable.set_system, R.drawable.set_time }; private int totleItemids[] = new int[] { R.drawable.tool_back, R.drawable.tool_copy, R.drawable.tool_file, R.drawable.tool_help, R.drawable.tool_report, R.drawable.tool_report, R.drawable.tool_save, R.drawable.tool_share }; private int titleIds[] = new int[] { R.string.popmenu_common, R.string.popmenu_set, R.string.popmenu_tool }; private PopMenu popMenu = null ; private PopupMenuBodyAdapter commonAdapter = null ; private PopupMenuBodyAdapter setAdapter = null ; private PopupMenuBodyAdapter toolAdapter = null ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.requestWindowFeature(Window.FEATURE_NO_TITLE); // 取消标题 super.setContentView(R.layout.main); this.gridviewToolbar = (GridView) super.findViewById(R.id.gridviewbar); this.content = (LinearLayout) super.findViewById(R.id.content); // 定义工具栏的一些信息显示 this.gridviewToolbar.setNumColumns(this.menu_img.length); // 求出可以保存的个数 this.gridviewToolbar.setSelector(new ColorDrawable(Color.TRANSPARENT)); this.gridviewToolbar.setGravity(Gravity.CENTER); this.gridviewToolbar.setVerticalSpacing(0); this.width = super.getWindowManager().getDefaultDisplay().getWidth() / this.menu_img.length; this.height = super.getWindowManager().getDefaultDisplay().getHeight() / 8; this.menu = new MenuImageAdapter(this, this.menu_img, this.width, this.height, R.drawable.menu_selected); this.gridviewToolbar.setAdapter(this.menu); this.switchActivity(0); // 第一个被选中 this.gridviewToolbar .setOnItemClickListener(new OnItemClickListenerImpl()); this.popMenu = new PopMenu(this, this.titleIds, 0x55123456, new PopupTitleOnItemClickListenerCallback(), new PopupBodyOnItemClickListenerCallback()); this.commonAdapter = new PopupMenuBodyAdapter(this, this.commonItemIds); this.setAdapter = new PopupMenuBodyAdapter(this, this.setItemIds); this.toolAdapter = new PopupMenuBodyAdapter(this, this.totleItemids); this.popMenu.setPopupMenuBodyAdapter(this.commonAdapter) ; this.popMenu.setPopTitleSelected(0) ; } private class OnItemClickListenerImpl implements OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { PopMenuActivity.this.switchActivity(position); } } private void switchActivity(int id) { // 切换选中的操作 this.menu.setFocus(id); // 设置选中图片的背景 this.content.removeAllViews(); // 删除所有的内容 switch (id) { case 0: this.intent = new Intent(PopMenuActivity.this, MyActivity.class); break; case 1: this.intent = new Intent(PopMenuActivity.this, MyActivity.class); break; case 2: this.intent = new Intent(PopMenuActivity.this, MyActivity.class); break; case 3: this.showPopupMenu() ; break; case 4: this.exitDialog() ; return; } this.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); Window subActivity = super.getLocalActivityManager().startActivity( "subActivity", this.intent); this.content.addView(subActivity.getDecorView(), LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); } private void exitDialog() { Dialog dialog = new AlertDialog.Builder(this).setIcon(R.drawable.pic_m) .setTitle("程序退出? ").setMessage("您确定要退出本程序吗?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { PopMenuActivity.this.finish() ; } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { PopMenuActivity.this.switchActivity(0); } }).create(); dialog.show(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK) { this.exitDialog() ; } return false ; } private void showPopupMenu() { if (this.isShow) { // 已经显示了 this.popMenu.dismiss(); this.isShow = false; } else { this.popMenu.showAtLocation( PopMenuActivity.this.gridviewToolbar, Gravity.BOTTOM, 0, this.height); this.isShow = true; } } private class PopupBodyOnItemClickListenerCallback implements OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { PopMenuActivity.this.popMenu.setPopBodySelected(position, Color.GRAY) ; Toast.makeText(PopMenuActivity.this, "执行选项 - " + position, Toast.LENGTH_SHORT).show() ; } } private class PopupTitleOnItemClickListenerCallback implements OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { PopMenuActivity.this.popMenu.setPopTitleSelected(position) ; switch(position) { case 0: PopMenuActivity.this.popMenu .setPopupMenuBodyAdapter(PopMenuActivity.this.commonAdapter); break; case 1: PopMenuActivity.this.popMenu .setPopupMenuBodyAdapter(PopMenuActivity.this.setAdapter); break; case 2: PopMenuActivity.this.popMenu .setPopupMenuBodyAdapter(PopMenuActivity.this.toolAdapter); break; } }} }
这些图片是我从教程里拿的,下面看看弹出菜单的效果。