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中的MD5加密
- Android录音功能和播放录音功能的示例源码
- Android开发网站
- android editview与popwindow焦点冲突
- Android源码中的单例模式android.util.Singleton
- Android滑动冲突解决方法
- 杂七杂八
- android关于网络
- Android(安卓)6.0源码编译环境搭建
随机推荐
-
android 如何判断程序是否在前台运行
-
【Android】用AsyncTask实现多线程
-
Android跨进程通信:图文详解 Binder机制
-
Android TextView追加文字
-
Android(java.lang.NullPointerException
-
Android设备抓包命令
-
android下保存用户账号密码和读取账号密
-
Android:使用SpannableString实现图片替
-
android 画图 Matrix
-
Use Android NDK r7 to compile FFmpeg0.