Android之自定义系统菜单栏背景
来源:互联网 发布:matlab优化算法工具箱 编辑:程序博客网 时间:2024/05/21 11:31
关键代码为重写Layout.Factory.onCreateView()方法自定义布局,不复杂,所以不多说,简单的几段代码:
public class MenuAct extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuInflater inflater = new MenuInflater(getApplicationContext()); inflater.inflate(R.menu.menu, menu); setMenuBackground(); return true; } public boolean onOptionsItemSelected(MenuItem item) { String info = ""; switch (item.getItemId()) { case R.id.menu_add: info = "Add"; break; case R.id.menu_delete: info = "Delete"; break; case R.id.menu_home: info = "Home"; break; case R.id.menu_help: info = "Help"; break; default: info = "NULL"; break; } Toast toast = Toast.makeText(this, info, Toast.LENGTH_SHORT); toast.show(); return super.onOptionsItemSelected(item); } // 关键代码为重写Layout.Factory.onCreateView()方法自定义布局 protected void setMenuBackground() { MenuAct.this.getLayoutInflater().setFactory(new android.view.LayoutInflater.Factory() { /** * name - Tag name to be inflated.<br/> * context - The context the view is being created in.<br/> * attrs - Inflation attributes as specified in XML file.<br/> */ public View onCreateView(String name, Context context, AttributeSet attrs) { // 指定自定义inflate的对象 if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) { try { LayoutInflater f = getLayoutInflater(); final View view = f.createView(name, null, attrs); new Handler().post(new Runnable() { public void run() { // 设置背景图片 view.setBackgroundResource(R.drawable.menu_background); } }); return view; } catch (InflateException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } return null; } }); } }
/res/menu/menu.xml
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_add" android:title="Add" android:icon="@drawable/menu_add"></item> <item android:id="@+id/menu_delete" android:title="Delete" android:icon="@drawable/menu_delete"></item> <item android:id="@+id/menu_home" android:title="Home" android:icon="@drawable/menu_home"></item> <item android:id="@+id/menu_help" android:title="Help" android:icon="@drawable/menu_help"></item> </menu>
- Android之自定义系统菜单栏背景
- Android自定义菜单栏
- [Android]自定义系统菜单的背景
- Android]自定义系统菜单的背景
- Android 自定义系统菜单的背景源码
- [Android]自定义系统菜单的背景
- Android学习之自定义控件背景
- Android之selector自定义背景实现1
- Android之selector自定义背景实现2
- Android之自定义背景按钮、自定义形状按钮
- Android之自定义背景按钮、自定义形状按钮
- 自定义菜单栏---卫星菜单栏
- 自定义Android菜单背景
- 自定义Android菜单背景
- ANDROID:LISTVIEW 自定义背景
- 自定义Android菜单背景
- android自定义ListView背景
- 自定义Android菜单背景
- Eclipse开发经典教程:插件安装
- Mysql存储过程
- 查看linux系统版本是32位的还是64位的
- ios 正则表达式语法参考
- C语言之memset函数
- Android之自定义系统菜单栏背景
- solr配置扩展词典和停用词典的问题
- IIS的变迁(IIS3, IIS4, IIS5, IIS6, IIS7)
- ubuntu如何修改屏幕默认亮度
- 郁闷,不小心吧SQL Sever弄坏了。
- shell脚本实现删除文件的源代码
- 网页设计实训项目一——规划网站
- 5个常用的MySQL数据库管理工具
- Resetting the Root Password: Unix Systems