<pre name="code" class="java">package com.jackie.activitygroupproject;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 MainActivity extends ActivityGroup {
private LinearLayout content = null; // 填充内容
private MenuImageAdapter menu = null; // 图片适配器
private GridView gridViewToolBar; // 工具菜单类
private int[] menu_img = new int[] { R.drawable.pic_1, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4, R.drawable.pic_5 }; // 填充的图片资源
private int width = 0; // 求出平均的宽度
private int height = 0; // 求出平均的高度
private Intent intent;
private PopupMenuBodyAdapter commonAdapter = null;
private PopupMenuBodyAdapter setAdapter = null;
private PopupMenuBodyAdapter toolAdapter = null;
private boolean isShow = false; // 默认更多菜单关闭
private int commonItemIds[] = new int[] { R.drawable.pic_1,
R.drawable.pic_2, R.drawable.pic_3, R.drawable.pic_4,
R.drawable.pic_5, R.drawable.pic_1, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4, R.drawable.pic_5 };
private int setItemIds[] = new int[] { R.drawable.pic_4, R.drawable.pic_5,
R.drawable.pic_1, R.drawable.pic_2, R.drawable.pic_3,
R.drawable.pic_4, R.drawable.pic_5, R.drawable.pic_1,
R.drawable.pic_2, R.drawable.pic_3 };
private int toolItemIds[] = new int[] { R.drawable.pic_5, R.drawable.pic_1,
R.drawable.pic_2, R.drawable.pic_3, R.drawable.pic_1,
R.drawable.pic_2, R.drawable.pic_3, R.drawable.pic_4 };
private PopupMenu popMenu = null;
private int titleIds[] = new int[] { R.string.popmenu_common,
R.string.popmenu_set, R.string.popmenu_tool };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.requestWindowFeature(Window.FEATURE_NO_TITLE); // 取消标题
super.setContentView(R.layout.activity_main);
this.content = (LinearLayout) super.findViewById(R.id.content);
this.gridViewToolBar = (GridView) super.findViewById(R.id.gridviewbar);
// 定义一些工具栏的信息显示
this.gridViewToolBar.setNumColumns(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.pic);
this.gridViewToolBar.setAdapter(menu);
this.switchActivity(0);
this.gridViewToolBar
.setOnItemClickListener(new OnItemClickListenerImpl());
this.popMenu = new PopupMenu(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.toolItemIds);
this.popMenu.setPopupMenuBodyAdapter(commonAdapter);
this.popMenu.setPopTitleSelected(0);
}
/*
* private void initAdapter(){ Field[]
* fields=R.drawable.class.getDeclaredFields();//取得全部属性 for(int
* x=0;x<fields.length;x++){ Map<String,Integer> map=new
* HashMap<String,Integer>(); if(fields[x].getName().startsWith("common_")){
* try { map.put("commonItem", fields[x].getInt(R.drawable.class)); } catch
* (Exception e) { e.printStackTrace(); } this.list1.add(map); }else
* if(fields[x].getName().startsWith("set_")){ try { map.put("setItem",
* fields[x].getInt(R.drawable.class)); } catch (Exception e) {
* e.printStackTrace(); } this.list2.add(map); }else
* if(fields[x].getName().startsWith("tool_")){ try { map.put("toolItem",
* fields[x].getInt(R.drawable.class)); } catch (Exception e) {
* e.printStackTrace(); } this.list3.add(map); } } this.commonAdapter=new
* PopupMenuBodyAdapter(this,null); }
*/
public class OnItemClickListenerImpl implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
MainActivity.this.switchActivity(position);
}
}
private void switchActivity(int id) { // 切换选中的操作
this.menu.setFocus(id); // 设置选中图片的背景
this.content.removeAllViews(); // 删除所有内容
switch (id) {
case 0:
this.intent = new Intent(MainActivity.this, MyActivity.class);
break;
case 1:
this.intent = new Intent(MainActivity.this, MyActivity.class);
break;
case 2:
this.intent = new Intent(MainActivity.this, MyActivity.class);
break;
case 3:
this.showPopupMenu();
break;
case 4:
this.exitDialog();
break;
}
this.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Window subActivity = super.getLocalActivityManager().startActivity(
"subActivity", this.intent);
this.content.addView(subActivity.getDecorView(),
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
private void exitDialog() {
Dialog dialog = new AlertDialog.Builder(this).setIcon(R.drawable.file)
.setTitle("程序退出?").setMessage("您确定要退出本程序吗?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
MainActivity.this.finish();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
MainActivity.this.switchActivity(0);
}
}).create();
dialog.show();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
this.exitDialog();
}
return super.onKeyDown(keyCode, event);
}
private void showPopupMenu() {
if (this.isShow) { // 已经显示了
this.popMenu.dismiss();
this.isShow = false;
} else {
this.popMenu.showAtLocation(MainActivity.this.gridViewToolBar,
Gravity.BOTTOM, 0, this.height);
this.isShow = true;
}
}
public class PopupTitleOnItemClickListenerCallback implements
OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
MainActivity.this.popMenu.setPopTitleSelected(position);
switch (position) {
case 0:
MainActivity.this.popMenu
.setPopupMenuBodyAdapter(commonAdapter);
break;
case 1:
MainActivity.this.popMenu.setPopupMenuBodyAdapter(setAdapter);
break;
case 2:
MainActivity.this.popMenu.setPopupMenuBodyAdapter(toolAdapter);
break;
}
}
}
public class PopupBodyOnItemClickListenerCallback implements
OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
MainActivity.this.popMenu.setPopBodySelected(position, Color.GRAY);
Toast.makeText(MainActivity.this, "执行选项-" + position,
Toast.LENGTH_LONG).show();
}
}
}
2、切换菜单显示的MyActivity
package com.jackie.activitygroupproject;
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 ImageView[] menuImg; // 保存所有要填充的组件
private Context context = null; // 上下文对象
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++) {
this.menuImg[x] = new ImageView(this.context); // 实例化ImageView
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]); // 设置显示的图片
}
}
@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 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgView = null;
if (convertView == null) {
imgView = this.menuImg[position];
} else {
imgView = (ImageView) convertView;
}
return imgView;
}
public void setFocus(int selId) { // 设置选中显示
for (int x = 0; x < menuImg.length; x++) {
if (x != selId) { // 没有选中的
this.menuImg[x].setBackgroundResource(0); //不设置背景图片
}
}
this.menuImg[selId].setBackgroundResource(selectedMenuImg);
}
}
4、弹出的窗口菜单popupMenu.java 定义标题和主体 都用gridView
package com.jackie.activitygroupproject;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
public class PopupMenu extends PopupWindow {
private GridView popTitle; // 表示标题
private GridView popBody; // 表示主体
private PopupMenuTitleAdapter titleAdapter = null;
private LinearLayout layout = null;
public PopupMenu(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.MATCH_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.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
this.popBody.setNumColumns(5);
this.popBody.setGravity(Gravity.CENTER);
this.popBody.setPadding(10, 10, 10, 10);
this.popBody.setHorizontalSpacing(1);
this.popBody.setVerticalSpacing(1);
this.popBody.setStretchMode(GridView.STRETCH_COLUMN_WIDTH); // 设置拉伸列宽
this.popBody.setOnItemClickListener(bodyCallback);
this.layout.addView(popTitle);
this.layout.addView(popBody);
super.setContentView(this.layout);
super.setHeight(LayoutParams.WRAP_CONTENT);
super.setWidth(LayoutParams.MATCH_PARENT);
super.setBackgroundDrawable(new ColorDrawable(backgroundColor));// 显示背景颜色操作
super.setFocusable(true);// 允许获得焦点
}
public void setPopupMenuBodyAdapter(PopupMenuBodyAdapter adapter) {
this.popBody.setAdapter(adapter);
}
public void setPopTitleSelected(int position) {
this.popTitle.setSelection(position);
this.titleAdapter.setFocus(position);
}
public void setPopBodySelected(int position, int selectedColor) { // 设置选中后的颜色
int count = 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);
}
}
5、定义一个标题适配器PopupMenuTitleAdapter.java
package com.jackie.activitygroupproject;
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].setGravity(Gravity.CENTER);
this.menuTitle[x].setTextSize(fontSize);
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);
}
}
6、定义一个主体适配器 PopupMenuBodyAdapter.java
<pre name="code" class="java">package com.jackie.activitygroupproject;
import java.util.List;
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;
}
}
7、两个Activity的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<GridView
android:id="@+id/gridviewbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:fadingEdge="vertical"
android:fadingEdgeLength="5sp" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/man"/>"
</RelativeLayout>