形容迪士尼烟花的句子:[Android]自定义系统菜单的背景

来源:百度文库 编辑:偶看新闻 时间:2024/04/29 13:03:26

[Android]自定义系统菜单的背景

2011-01-26 16:44 2374人阅读 评论(12) 收藏 举报

 

不多说,上图,见代码。

 

 

 

 

view plain

  1. package lab.sodino.menutest;  
  2. import android.content.Context;  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.util.AttributeSet;  
  7. import android.view.InflateException;  
  8. import android.view.LayoutInflater;  
  9. import android.view.Menu;  
  10. import android.view.MenuInflater;  
  11. import android.view.MenuItem;  
  12. import android.view.View;  
  13. import android.widget.Toast;  
  14. /** 
  15.  * @author Sodino E-mail:sodinoopen@hotmail.com 
  16.  * @version Time:2011-1-26 下午04:42:04 
  17.  */  
  18. public class MenuAct extends Activity {  
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.     }  
  24.     public boolean onCreateOptionsMenu(Menu menu) {  
  25.         super.onCreateOptionsMenu(menu);  
  26.         MenuInflater inflater = new MenuInflater(getApplicationContext());  
  27.         inflater.inflate(R.menu.menu, menu);  
  28.         setMenuBackground();  
  29.         return true;  
  30.     }  
  31.     public boolean onOptionsItemSelected(MenuItem item) {  
  32.         String info = "";  
  33.         switch (item.getItemId()) {  
  34.         case R.id.menu_add:  
  35.             info = "Add";  
  36.             break;  
  37.         case R.id.menu_delete:  
  38.             info = "Delete";  
  39.             break;  
  40.         case R.id.menu_home:  
  41.             info = "Home";  
  42.             break;  
  43.         case R.id.menu_help:  
  44.             info = "Help";  
  45.             break;  
  46.         default:  
  47.             info = "NULL";  
  48.             break;  
  49.         }  
  50.         Toast toast = Toast.makeText(this, info, Toast.LENGTH_SHORT);  
  51.         toast.show();  
  52.         return super.onOptionsItemSelected(item);  
  53.     }  
  54.     // 关键代码为重写Layout.Factory.onCreateView()方法自定义布局  
  55.     protected void setMenuBackground() {  
  56.         MenuAct.this.getLayoutInflater().setFactory(new android.view.LayoutInflater.Factory() {  
  57.             /** 
  58.              * name - Tag name to be inflated.
     
  59.              * context - The context the view is being created in.
     
  60.              * attrs - Inflation attributes as specified in XML file.
     
  61.              */  
  62.             public View onCreateView(String name, Context context, AttributeSet attrs) {  
  63.                 // 指定自定义inflate的对象  
  64.                 if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {  
  65.                     try {  
  66.                         LayoutInflater f = getLayoutInflater();  
  67.                         final View view = f.createView(name, null, attrs);  
  68.                         new Handler().post(new Runnable() {  
  69.                             public void run() {  
  70.                                 // 设置背景图片  
  71.                                 view.setBackgroundResource(R.drawable.menu_background);  
  72.                             }  
  73.                         });  
  74.                         return view;  
  75.                     } catch (InflateException e) {  
  76.                         e.printStackTrace();  
  77.                     } catch (ClassNotFoundException e) {  
  78.                         e.printStackTrace();  
  79.                     }  
  80.                 }  
  81.                 return null;  
  82.             }  
  83.         });  
  84.     }  
  85. }  

 

 

/res/menu/menu.xml

view plain

  1.   
  2.   
  3.       
  4.       
  5.       
  6.       
  7.   

本文内容归CSDN博客博主Sodino所有

转载请注明出处:http://blog.csdn.net/sodino/archive/2011/01/26/6165132.aspx