Android 自定义系统菜单的背景源码
01 | package lab.sodino.menutest; |
03 | android.content.Context; |
04 | import android.app.Activity; |
05 | import android.os.Bundle; |
06 | import android.os.Handler; |
07 | import android.util.AttributeSet; |
08 | import android.view.InflateException; |
09 | import android.view.LayoutInflater; |
10 | import android.view.Menu; |
11 | import android.view.MenuInflater; |
12 | import android.view.MenuItem; |
13 | import android.view.View; |
14 | import android.widget.Toast; |
16 | * @author Sodino E-mail:sodinoopen@hotmail.com |
17 | * @version Time:2011-1-26 下午04:42:04 |
19 | public class MenuAct extends Activity { |
21 | public void onCreate(Bundle savedInstanceState) { |
22 | super .onCreate(savedInstanceState); |
23 | setContentView(R.layout.main); |
25 | public boolean onCreateOptionsMenu(Menu menu) { |
26 | super .onCreateOptionsMenu(menu); |
27 | MenuInflater inflater = new MenuInflater(getApplicationContext()); |
28 | inflater.inflate(R.menu.menu, menu); |
32 | public boolean onOptionsItemSelected(MenuItem item) { |
34 | switch (item.getItemId()) { |
38 | case R.id.menu_delete: |
51 | Toast toast = Toast.makeText( this , info, Toast.LENGTH_SHORT); |
53 | return super .onOptionsItemSelected(item); |
56 | protected void setMenuBackground() { |
57 | MenuAct. this .getLayoutInflater().setFactory( new android.view.LayoutInflater.Factory() { |
59 | * name - Tag name to be inflated.<br/> |
60 | * context - The context the view is being created in.<br/> |
61 | * attrs - Inflation attributes as specified in XML file.<br/> |
63 | public View onCreateView(String name, Context context, AttributeSet attrs) { |
65 | if (name.equalsIgnoreCase( "com.android.internal.view.menu.IconMenuItemView" )) { |
67 | LayoutInflater f = getLayoutInflater(); |
68 | final View view = f.createView(name, null , attrs); |
69 | new Handler().post( new Runnable() { |
72 | view.setBackgroundResource(R.drawable.menu_background); |
76 | } catch (InflateException e) { |
78 | } catch (ClassNotFoundException e) { |
/res/menu/menu.xml
view source print ?
1 | <?xml version= "1.0" encoding= "utf-8" ?> |
2 | <menu xmlns:android= "http://schemas.android.com/apk/res/android" > |
3 | <item android:id= "@+id/menu_add" android:title= "Add" android:icon= "@drawable/menu_add" ></item> |
4 | <item android:id= "@+id/menu_delete" android:title= "Delete" android:icon= "@drawable/menu_delete" ></item> |
5 | <item android:id= "@+id/menu_home" android:title= "Home" android:icon= "@drawable/menu_home" ></item> |
6 | <item android:id= "@+id/menu_help" android:title= "Help" android:icon= "@drawable/menu_help" ></item> |
转自:http://www.eoeandroid.com/code/2012/0803/2007.html
- Android重写返回按钮退回桌面(根据源码查看)
- Android录音功能和播放录音功能的示例源码
- Android 6.0源码编译环境搭建
- android apk源码
- 基于Android的推箱子小游戏 源码
- Android实现简单计算器源码
- 从源码中查看当前android版本
- Android泡泡聊天界面的源码实现
随机推荐
-
Android(安卓)Duplicate files copied in
-
在Android中查看和管理sqlite数据库
-
Android启动画面
-
Android入门教程 MediaPlayer教程【使用
-
Android中Hook Instrumentation 的实现
-
UnityAndroid(2) Android加载Unity
-
android:inputType 属性及其支持的取值
-
Android的移动存储解决方案之SharedPrefe
-
在Android中使用Html5
-
Android中广播注册与接收流程