创建选项卡实现弹出菜单,以及自定义菜单样式。
TabHost相信大家都很熟悉,我们经常看到的底部栏就是基于TabHost实现的,也就是常用的选项卡,用来切换不同的Activity,但是怎么去实现弹出菜单呢,又怎么让每个Activity中都能弹出菜单呢,不多说,看代码。
首先我自定义一个BaseActivity,在这里实现对菜单的操作。
1 package com.android; 2 import java.util.ArrayList; 3 import java.util.HashMap; 4 5 import com.android.R; 6 import com.anroid.net.update1; 7 import android.app.Activity; 8 import android.app.AlertDialog; 9 import android.content.DialogInterface; 10 import android.content.DialogInterface.OnKeyListener; 11 import android.content.Intent; 12 import android.os.Bundle; 13 import android.provider.Settings; 14 import android.view.KeyEvent; 15 import android.view.Menu; 16 import android.view.View; 17 import android.widget.AdapterView; 18 import android.widget.GridView; 19 import android.widget.SimpleAdapter; 20 import android.widget.TextView; 21 import android.widget.AdapterView.OnItemClickListener; 22 23 public class BaseActivity extends Activity { 24 protected TextView tv_head; 25 AlertDialog menuDialog;// menu菜单Dialog 26 GridView menuGrid; 27 View menuView; 28 /** 菜单图片 **/ 29 int[] menu_image_array = { 30 R.drawable.menu_quit,R.drawable.menu_checkupdate,R.drawable.menu_checknet,R.drawable.menu_about,R.drawable.menu_rename,R.drawable.menu_return 31 }; 32 /** 菜单文字 **/ 33 String[] menu_name_array = { "退出","检查更新", "设置网络","关于","上传 ","返回" }; 34 35 @Override 36 protected void onCreate(Bundle savedInstanceState) { 37 // TODO Auto-generated method stub 38 super.onCreate(savedInstanceState); 39 menuView = View.inflate(this, R.layout.gridview_menu, null); 40 // 创建AlertDialog 41 menuDialog = new AlertDialog.Builder(this).create(); 42 menuDialog.setView(menuView); 43 menuDialog.setOnKeyListener(new OnKeyListener() { 44 public boolean onKey(DialogInterface dialog, int keyCode, 45 KeyEvent event) { 46 if (keyCode == KeyEvent.KEYCODE_MENU)// 监听按键 47 dialog.dismiss(); 48 return false; 49 } 50 }); 51 menuGrid = (GridView) menuView.findViewById(R.id.gridview); 52 menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array)); 53 /** 监听menu选项 **/ 54 menuGrid.setOnItemClickListener(new OnItemClickListener() { 55 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 56 long arg3) { 57 switch (arg2) { 58 case 0:// 退出 59 BaseActivity.this.finish(); 60 break; 61 case 1:// 检查更新 62 update1 doup=new update1(getApplicationContext()); 63 doup.execute(); 64 break; 65 case 2:// 检查网络 66 Intent i2 = new Intent(); 67 i2.setAction(Settings.ACTION_WIRELESS_SETTINGS); 68 startActivity(i2); 69 break; 70 case 3:// 关于 71 final Intent i3 = new Intent(getApplicationContext(),About.class); 72 startActivity(i3); 73 menuDialog.dismiss(); 74 75 break; 76 case 4:// 上传 77 78 break; 79 case 5:// 返回 80 menuDialog.dismiss(); 81 break; 82 83 } 84 } 85 }); 86 87 } 88 public boolean onCreateOptionsMenu(Menu menu) { 89 menu.add("menu");// 必须创建一项 90 return super.onCreateOptionsMenu(menu); 91 } 92 private SimpleAdapter getMenuAdapter(String[] menuNameArray, 93 int[] imageResourceArray) { 94 ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); 95 for (int i = 0; i < menuNameArray.length; i++) { 96 HashMap<String, Object> map = new HashMap<String, Object>(); 97 map.put("itemImage", imageResourceArray[i]); 98 map.put("itemText", menuNameArray[i]); 99 data.add(map); 100 } 101 SimpleAdapter simperAdapter = new SimpleAdapter(this, data, 102 R.layout.item_menu, new String[] { "itemImage", "itemText" }, 103 new int[] { R.id.item_image, R.id.item_text }); 104 return simperAdapter; 105 } 106 @Override 107 public boolean onMenuOpened(int featureId, Menu menu) { 108 if (menuDialog == null) { 109 menuDialog = new AlertDialog.Builder(this).setView(menuView).show(); 110 } else { 111 menuDialog.show(); 112 } 113 return false;// 返回为true 则显示系统menu 114 } 115 116 @Override 117 118 public boolean onKeyDown(int keyCode, KeyEvent event) { 119 // TODO Auto-generated method stub 120 121 if (keyCode == KeyEvent.KEYCODE_BACK) { 122 123 AlertDialog.Builder builder = new AlertDialog.Builder(getParent()); 124 builder.setMessage("你确定退出吗?") 125 .setCancelable(false) 126 .setPositiveButton("确定", 127 new DialogInterface.OnClickListener() { 128 public void onClick(DialogInterface dialog, 129 int id) { 130 finish(); 131 System.exit(0); 132 } 133 }) 134 .setNegativeButton("返回", 135 new DialogInterface.OnClickListener() { 136 public void onClick(DialogInterface dialog, 137 int id) { 138 dialog.cancel(); 139 } 140 }); 141 AlertDialog alert = builder.create(); 142 alert.show(); 143 return true; 144 } 145 146 return super.onKeyDown(keyCode, event); 147 } 148 private void showsetting(){ 149 150 151 } 152 }
界面文件;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <GridView android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="3" android:verticalSpacing="10dip" android:horizontalSpacing="10dip" android:stretchMode="columnWidth" android:gravity="center" /> </LinearLayout>
然后让每个tab的Activity都继承自BaseActiviy,这样就实现了每个Tab中都能弹出菜单。下面贴出怎样创建Tab选项卡。
package com.android; import com.android.R; import android.app.TabActivity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.Window; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TabHost; import android.widget.TextView; import android.widget.RadioGroup.OnCheckedChangeListener; public class MainActivity extends TabActivity { private static TabHost tabHost;private static RadioButton main_tab_myExam; private static Context context; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); context=this; main_tab_myExam=(RadioButton) findViewById(R.id.main_tab_myExam); tabHost=this.getTabHost(); TabHost.TabSpec spec; Intent intent; intent=new Intent().setClass(this, Qiu.class); spec=tabHost.newTabSpec("糗事").setIndicator("糗事").setContent(intent); tabHost.addTab(spec); intent=new Intent().setClass(this,Dz.class); spec=tabHost.newTabSpec("笑话").setIndicator("笑话").setContent(intent); tabHost.addTab(spec); intent=new Intent().setClass(this, New.class); spec=tabHost.newTabSpec("最新").setIndicator("最新").setContent(intent); tabHost.addTab(spec); intent=new Intent().setClass(this, Fa.class); spec=tabHost.newTabSpec("收藏").setIndicator("收藏").setContent(intent); tabHost.addTab(spec); // tabHost.setCurrentTab(1); RadioGroup radioGroup=(RadioGroup) this.findViewById(R.id.main_tab_group); radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub switch (checkedId) { case R.id.main_tab_addExam: tabHost.setCurrentTabByTag("糗事"); break; case R.id.main_tab_myExam: tabHost.setCurrentTabByTag("笑话"); break; case R.id.main_tab_message: tabHost.setCurrentTabByTag("最新"); break; case R.id.main_tab_settings: tabHost.setCurrentTabByTag("收藏"); break; default: break; } } }); } }
然后让这里面的四个Acyivity都继承自BaseActivity就行了。