今天研究了一下午,终于整出了,menu的背景图片和背景色的设置。效果如上图,呵呵不太好看啊

这我就不管了,只作为参考,你们自己看看吧。代码如下:

package com.hooypay.Activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.LayoutInflater.Factory;

public class MenuActivity extends BaseGroupActivity {
private Intent intent;
private static final String TAG = "DEBUG";
//创建menu,Activity调用onCreateOptionsMenu(Menu menu)方法,传一个实现Menu接口的menu对象,供使用。
@Override
public boolean onCreateOptionsMenu(Menu menu){

//menu.add()里面有四个参数依次是:
//第一个,组别。
//第二个,ID。是menu识别编号,供识别menu用的。很重要。
//第三个,顺序。这个参数的大小决定菜单出现的先后顺序。顺序是参数由小到大,菜单从左到右,从上到下排列。一行最多三个。
//第四个,显示文本。

menu.add(0,Menu.FIRST,0,"主页").setIcon(R.drawable.conf_home);
menu.add(0,Menu.FIRST+1,1,"我的订单").setIcon(android.R.drawable.ic_menu_save);
menu.add(0,Menu.FIRST+2,2,"个人信息").setIcon(android.R.drawable.ic_menu_send);

menu.add(1,Menu.FIRST+3,3,"更多").setIcon(R.drawable.conf_more);
menu.add(1,Menu.FIRST+4,4,"退出").setIcon(android.R.drawable.ic_menu_delete);
menu.add(1,Menu.FIRST+5,5,"帮助").setIcon(android.R.drawable.ic_menu_help);

setMenuBackground();

intent= new Intent();
return true;
}

//设置menu菜单的背景
protected void setMenuBackground(){

Log.d(TAG, "Enterting setMenuBackGround");
getLayoutInflater().setFactory( new Factory() {

@Override
public View onCreateView ( String name, Context context, AttributeSet attrs ) {

if ( name.equalsIgnoreCase( "com.android.internal.view.menu.IconMenuItemView" ) ) {

try { // Ask our inflater to create the view
LayoutInflater f = getLayoutInflater();
final View view = f.createView( name, null, attrs );
new Handler().post( new Runnable() {
public void run () {
// view.setBackgroundResource( R.drawable.menu_backg);//设置背景图片
view.setBackgroundColor(new Color().GREEN);//设置背景色
}
} );
return view;
}
catch ( InflateException e ) {}
catch ( ClassNotFoundException e ) {}
}
return null;
}
});
}


//菜单项选择事件
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case Menu.FIRST:
intent.setClass(MenuActivity.this, MainActivity.class);
throudIntent(intent);
break;
case Menu.FIRST+1:
intent.setClass(MenuActivity.this, OrderActivity.class);
throudIntent(intent);
break;
case Menu.FIRST+2:
intent.setClass(MenuActivity.this, UserInfoActivity.class);
throudIntent(intent);
break;
case Menu.FIRST+3:
intent.setClass(MenuActivity.this, HomeTabGroup.class);
throudIntent(intent);
break;
case Menu.FIRST+4:
isExit();
break;
case Menu.FIRST+5:
// Toast.makeText(this,"帮助菜单被点击了",Toast.LENGTH_SHORT).show();
break;
}
return false;
}
//菜单被显示之前的事件
@Override
public boolean onPrepareOptionsMenu(Menu menu){
// Toast.makeText(this,"菜单被显示之前的事件,可以在这里调整菜单",Toast.LENGTH_LONG).show();
return true;
//必须返回True才能调用onCreateOptionsMenu(Menu menu)方法。
}

//选择菜单的关闭事件。
//有三种关闭方式,menu按钮再被点击,back按钮被点击,用户选择另一个菜单项。
@Override
public void onOptionsMenuClosed(Menu menu){
// Toast.makeText(this,"菜单被关闭了",Toast.LENGTH_SHORT).show();
}
//跳转到其他页面
private void throudIntent(Intent intent){
startActivity(intent);
}
//提示是否退出
public void isExit(){
AlertDialog.Builder alertbBuilder = new AlertDialog.Builder(this);
alertbBuilder.setIcon(R.drawable.infoicon).setTitle("提示...").setMessage("你确定要离开客户端吗?");
alertbBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.putExtra("exit", "exit");//添加参数,这是退出的依据
intent.setClass(MenuActivity.this, loginActivity.class);//跳转到login界面,根据参数退出
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //注意本行的FLAG设置,clear所有Activity记录
startActivity(intent);//注意啊,在跳转的页面中进行检测和退出
}
});
alertbBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create();
alertbBuilder.show();
}
}

上面的代码是建立在别人的代码之上的。他没有写背景,我给加上的。

希望牛人给些建议,也有利于我的进步

更多相关文章

  1. android 游戏 实战打飞机游戏 菜单页面(1)
  2. Android(安卓)pcm文件播放方法(AudioTrack)
  3. Android(安卓)退出所有Activity
  4. Android(安卓)API 中文 (51) —— ZoomButtonsController
  5. Android(安卓)中传递 json 的问题
  6. 用Android自带的DrawerLayout和ActionBarDrawerToggle实现侧滑效
  7. Android(安卓)Post请求SQL数据库
  8. [android]android自动化测试三之设定AVD各项参数
  9. Android(安卓)模仿renren的左右划动菜单栏

随机推荐

  1. Android(安卓)不得不写一下了
  2. Android中native进程内存泄露的调试技巧
  3. Android(安卓)手机上安装并运行 Ubuntu 1
  4. 谈谈移动应用开发环境
  5. Android如何扩大按钮点击区域
  6. Android软件广告屏蔽方法及代码
  7. Android(安卓)性能优化之使用MAT分析内存
  8. Android: NDK编程入门笔记
  9. 如何使用 Eclipse 给 Android(安卓)模拟
  10. 关于android的9path图片处理