1package com.example.mytest; 2import android.os.Bundle; 3import android.app.Activity; 4import android.view.Menu; 5public class MainActivity extends Activity { 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 } 11 12} 13 14package com.example.mytest; 15import android.app.ActivityGroup; 16import android.app.AlertDialog; 17import android.app.Dialog; 18import android.content.DialogInterface; 19import android.content.Intent; 20import android.graphics.Color; 21import android.graphics.drawable.ColorDrawable; 22import android.os.Bundle; 23import android.view.Gravity; 24import android.view.KeyEvent; 25import android.view.View; 26import android.view.Window; 27import android.view.ViewGroup.LayoutParams; 28import android.widget.AdapterView; 29import android.widget.GridView; 30import android.widget.LinearLayout; 31import android.widget.AdapterView.OnItemClickListener; 32 33public class MyActivityGroupImageDemo1 extends ActivityGroup{ 34 private GridView gridviewToolbar; // 定义GridView工具条 35 private MenuImageAdapter menu = null; // 图片适配器 36 private Intent intent=null;// 要操作的Intent 37 private LinearLayout content = null; // 显示内容的布局管理器 38 private int menu_img[] = new int[] { R.drawable.menu_phone, 39 R.drawable.common_account, R.drawable.common_download, R.drawable.menu_news, 40 R.drawable.common_exit }; // 图片显示资源ID 41 private int width = 0 ; // 保存每个菜单项的图片宽度 42 private int height = 0 ; 43 @Override 44 protected void onCreate(Bundle savedInstanceState) { 45 // TODO Auto-generated method stub 46 super.onCreate(savedInstanceState); 47 super.requestWindowFeature(Window.FEATURE_NO_TITLE); 48 super.setContentView(R.layout.mylist); 49 this.gridviewToolbar=(GridView)super.findViewById(R.id.gridviewbar); 50 this.content=(LinearLayout)super.findViewById(R.id.content); 51 this.gridviewToolbar.setNumColumns(this.menu_img.length); 52 this.gridviewToolbar.setBackgroundColor(Color.GRAY); 53 this.gridviewToolbar.setSelector(new ColorDrawable(Color.TRANSPARENT)); 54 this.gridviewToolbar.setGravity(Gravity.CENTER); 55 this.gridviewToolbar.setVerticalSpacing(0); 56 this.width = super.getWindowManager().getDefaultDisplay().getWidth() 57 / this.menu_img.length; // 计算平均宽度 58 this.height = super.getWindowManager().getDefaultDisplay().getHeight() 59 / 8; // 高度 60 this.menu = new MenuImageAdapter(this, this.menu_img, this.width, 61 this.height, R.drawable.menu_selected); // 实例化适配器 62 this.gridviewToolbar.setAdapter(this.menu); // 设置显示数据 63 this.switchActivity(0); // 默认打开第一个 64 this.gridviewToolbar.setOnItemClickListener( 65 new OnItemClickListenerImpl()); // 选中监听 66 } 67 private class OnItemClickListenerImpl implements OnItemClickListener { 68 @Override 69 public void onItemClick(AdapterView<?> parent, View view, int position, 70 long id) { 71 MyActivityGroupImageDemo1.this.switchActivity(position); // 切换选项 72 } 73 } 74 75 /** 76 * 根据ID打开指定的Activity 77 * @param id GridView选中项的序号 78 */ 79 private void switchActivity(int id) { // 切换视图 80 this.menu.setFocus(id); // 选中项获得高亮 81 this.content.removeAllViews(); // 先清除容器中所有View 82 switch (id) { // 根据操作实例化Intent 83 case 0: // 指定操作的Intent 84 this.intent = new Intent(MyActivityGroupImageDemo1.this, 85 MainActivity.class); 86 break; 87 case 1: // 指定操作的Intent 88 this.intent = new Intent(MyActivityGroupImageDemo1.this, 89 MainActivity.class); 90 break; 91 case 2: // 指定操作的Intent 92 this.intent = new Intent(MyActivityGroupImageDemo1.this, 93 MainActivity.class); 94 break; 95 case 3: // 指定操作的Intent 96 this.intent = new Intent(MyActivityGroupImageDemo1.this, 97 MainActivity.class); 98 break; 99 case 4: // 指定操作的Intent 100 this.exitDialog(); // 退出判断 101 return; 102 } 103 this.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // 增加标记 104 Window subActivity = this.getLocalActivityManager().startActivity( 105 "subActivity", this.intent); // Activity 转为 View 106 this.content.addView(subActivity.getDecorView(), 107 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); // 容器添加View 108 } 109 private void exitDialog() { 110 Dialog dialog = new AlertDialog.Builder( 111 MyActivityGroupImageDemo1.this) // 实例化对象 112 .setIcon(R.drawable.pic_m) // 设置显示图片 113 .setTitle("程序退出?") // 设置显示标题 114 .setMessage("您确定要退出本程序吗?") // 设置显示内容 115 .setPositiveButton("确定", // 增加一个确定按钮 116 new DialogInterface.OnClickListener() { // 设置操作监听 117 public void onClick(DialogInterface dialog, 118 int whichButton) { // 单击事件 119 MyActivityGroupImageDemo1.this.finish();// 程序结束 120 } 121 }).setNegativeButton("取消", // 增加取消按钮 122 new DialogInterface.OnClickListener() { // 设置操作监听 123 public void onClick(DialogInterface dialog, 124 int whichButton) { // 单击事件 125 MyActivityGroupImageDemo1.this 126 .switchActivity(0); 127 } 128 }).create(); // 创建Dialog 129 dialog.show(); // 显示对话框 130 } 131 @Override 132 public boolean onKeyDown(int keyCode, KeyEvent event) { 133 if (keyCode == KeyEvent.KEYCODE_BACK) { // 如果是手机上的返回键 134 this.exitDialog(); // 提示退出对话框 135 } 136 return false; 137 } 138} 139 140package com.example.mytest; 141import android.content.Context; 142import android.view.View; 143import android.view.ViewGroup; 144import android.widget.BaseAdapter; 145import android.widget.GridView; 146import android.widget.ImageView; 147public class MenuImageAdapter extends BaseAdapter{ 148 private Context context; 149 private ImageView[] menuImg; 150 private int selectedMenuImg; 151 public MenuImageAdapter(Context context,int imgIds[],int width,int height,int selectedMenuImg) { 152 // TODO Auto-generated constructor stub 153 this.context=context; 154 this.selectedMenuImg=selectedMenuImg; 155 this.menuImg=new ImageView[imgIds.length]; 156 for (int x = 0; x < imgIds.length; x++) { 157 this.menuImg[x]=new ImageView(this.context); 158 this.menuImg[x].setLayoutParams(new GridView.LayoutParams(width,height)); 159 this.menuImg[x].setAdjustViewBounds(false); 160 this.menuImg[x].setPadding(3, 3, 3, 3); 161 this.menuImg[x].setImageResource(imgIds[x]); 162 } 163 } 164 165 @Override 166 public int getCount() { 167 // TODO Auto-generated method stub 168 return this.menuImg.length; 169 } 170 @Override 171 public Object getItem(int position) { 172 // TODO Auto-generated method stub 173 return this.menuImg[position]; 174 } 175 @Override 176 public long getItemId(int position) { 177 // TODO Auto-generated method stub 178 return 0; 179 } 180 @Override 181 public View getView(int position, View convertView, ViewGroup parent) { 182 // TODO Auto-generated method stub 183 ImageView imageView=null; 184 if (convertView==null) { 185 imageView=this.menuImg[position]; 186 }else{ 187 imageView=(ImageView)convertView; 188 } 189 return imageView; 190 } 191 192 public void setFocus(int selId) { 193 // 遍历菜单图片数组 194 for (int x = 0; x < this.menuImg.length; x++) { 195 if (x!=selId) { 196 this.menuImg[x].setBackgroundResource(0); 197 } 198 } 199 //选中的菜单图片高亮显示 200 this.menuImg[selId].setBackgroundResource(this.selectedMenuImg); 201 } 202} 203 204<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 205 xmlns:tools="http://schemas.android.com/tools" 206 android:orientation="vertical" 207 android:layout_width="fill_parent" 208 android:layout_height="fill_parent" 209 android:background="@color/gold"> 210 <TextView 211 android:id="@+id/textView1" 212 android:layout_width="wrap_content" 213 android:layout_height="wrap_content" 214 android:layout_alignParentLeft="true" 215 android:layout_centerVertical="true" 216 android:background="@android:color/transparent" 217 android:text="@string/pledge" 218 android:textSize="25dp" /> 219</RelativeLayout> 220 221<?xml version="1.0" encoding="utf-8"?> 222<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 223 android:layout_width="match_parent" 224 android:layout_height="match_parent" 225 android:orientation="vertical" 226 android:background="@color/gold"> 227 <RelativeLayout 228 android:layout_width="match_parent" 229 android:layout_height="match_parent" > 230 <LinearLayout 231 android:id="@+id/content" 232 android:layout_width="fill_parent" 233 android:layout_height="wrap_content" 234 android:orientation="horizontal"> 235 236 </LinearLayout> 237 <GridView 238 android:layout_height="wrap_content" 239 android:id="@+id/gridviewbar" 240 android:layout_alignParentBottom="true" 241 android:layout_width="fill_parent" 242 android:fadingEdgeLength="5px" 243 android:fadingEdge="vertical"> 244 </GridView> 245</RelativeLayout> 246</LinearLayout> 247 248<?xml version="1.0" encoding="utf-8"?> 249<manifest xmlns:android="http://schemas.android.com/apk/res/android" 250 package="com.example.mytest" 251 android:versionCode="1" 252 android:versionName="1.0" > 253 <uses-sdk 254 android:minSdkVersion="8" 255 android:targetSdkVersion="18" /> 256 <application 257 android:allowBackup="true" 258 android:icon="@drawable/icon" 259 android:label="@string/app_name" 260 android:theme="@style/AppTheme" > 261 <activity 262 android:name=".MyActivityGroupImageDemo1" 263 android:label="@string/app_name" > 264 <intent-filter> 265 <action android:name="android.intent.action.MAIN" /> 266 <category android:name="android.intent.category.LAUNCHER" /> 267 </intent-filter> 268 </activity> 269 <activity 270 android:name=".MainActivity" 271 android:label="@string/app_name"> 272 </activity> 273 </application> 274</manifest>
Android Intent隐式意图显示菜单
Stella981
2021-10-11
1481 0 0
点赞
收藏
评论区
加载中...