package com.example.mytest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
package com.example.mytest;
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.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemClickListener;
public class MyActivityGroupImageDemo1 extends ActivityGroup{
private GridView gridviewToolbar; // 定义GridView工具条
private MenuImageAdapter menu = null; // 图片适配器
private Intent intent=null;// 要操作的Intent
private LinearLayout content = null; // 显示内容的布局管理器
private int menu_img[] = new int[] { R.drawable.menu_phone,
R.drawable.common_account, R.drawable.common_download, R.drawable.menu_news,
R.drawable.common_exit }; // 图片显示资源ID
private int width = 0 ; // 保存每个菜单项的图片宽度
private int height = 0 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
super.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.setContentView(R.layout.mylist);
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.setBackgroundColor(Color.GRAY);
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()); // 选中监听
}
private class OnItemClickListenerImpl implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
MyActivityGroupImageDemo1.this.switchActivity(position); // 切换选项
}
}
/**
* 根据ID打开指定的Activity
* @param id GridView选中项的序号
*/
private void switchActivity(int id) { // 切换视图
this.menu.setFocus(id); // 选中项获得高亮
this.content.removeAllViews(); // 先清除容器中所有View
switch (id) { // 根据操作实例化Intent
case 0: // 指定操作的Intent
this.intent = new Intent(MyActivityGroupImageDemo1.this,
MainActivity.class);
break;
case 1: // 指定操作的Intent
this.intent = new Intent(MyActivityGroupImageDemo1.this,
MainActivity.class);
break;
case 2: // 指定操作的Intent
this.intent = new Intent(MyActivityGroupImageDemo1.this,
MainActivity.class);
break;
case 3: // 指定操作的Intent
this.intent = new Intent(MyActivityGroupImageDemo1.this,
MainActivity.class);
break;
case 4: // 指定操作的Intent
this.exitDialog(); // 退出判断
return;
}
this.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // 增加标记
Window subActivity = this.getLocalActivityManager().startActivity(
"subActivity", this.intent); // Activity 转为 View
this.content.addView(subActivity.getDecorView(),
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); // 容器添加View
}
private void exitDialog() {
Dialog dialog = new AlertDialog.Builder(
MyActivityGroupImageDemo1.this) // 实例化对象
.setIcon(R.drawable.pic_m) // 设置显示图片
.setTitle("程序退出?") // 设置显示标题
.setMessage("您确定要退出本程序吗?") // 设置显示内容
.setPositiveButton("确定", // 增加一个确定按钮
new DialogInterface.OnClickListener() { // 设置操作监听
public void onClick(DialogInterface dialog,
int whichButton) { // 单击事件
MyActivityGroupImageDemo1.this.finish();// 程序结束
}
}).setNegativeButton("取消", // 增加取消按钮
new DialogInterface.OnClickListener() { // 设置操作监听
public void onClick(DialogInterface dialog,
int whichButton) { // 单击事件
MyActivityGroupImageDemo1.this
.switchActivity(0);
}
}).create(); // 创建Dialog
dialog.show(); // 显示对话框
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) { // 如果是手机上的返回键
this.exitDialog(); // 提示退出对话框
}
return false;
}
}
package com.example.mytest;
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;
private int selectedMenuImg;
public MenuImageAdapter(Context context,int imgIds[],int width,int height,int selectedMenuImg) {
// TODO Auto-generated constructor stub
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);
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() {
// TODO Auto-generated method stub
return this.menuImg.length;
}
@Override
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) {
// TODO Auto-generated method stub
ImageView imageView=null;
if (convertView==null) {
imageView=this.menuImg[position];
}else{
imageView=(ImageView)convertView;
}
return imageView;
}
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);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/gold">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@android:color/transparent"
android:text="@string/pledge"
android:textSize="25dp" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/gold">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<GridView
android:layout_height="wrap_content"
android:id="@+id/gridviewbar"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:fadingEdgeLength="5px"
android:fadingEdge="vertical">
</GridView>
</RelativeLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mytest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyActivityGroupImageDemo1"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name">
</activity>
</application>
</manifest>
Android Intent隐式意图显示菜单
点赞
收藏