zoukankan      html  css  js  c++  java
  • [Android]自定义系统菜单的背景

    不多说,上图,见代码。

    custom menu background

    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.<br/> 
    59.              * context - The context the view is being created in.<br/> 
    60.              * attrs - Inflation attributes as specified in XML file.<br/> 
    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

    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>  
    7. </menu>  

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

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

  • 相关阅读:
    批量新增百万条数据 十百万条数据
    sqlserver 组内排序
    EF ++属性会更新实体
    Entity Framework Core: A second operation started on this context before a previous operation completed
    abp Cannot access a disposed object. A common cause of this error is disposing
    abp xunit Can not register IHostingEnvironment. It should be a non-abstract class. If not, it should be registered before.”
    hangfire enqueued but not processing(hangfire 定时任务入队列但不执行)
    EF 更新实体 The instance of entity type 'BabyEvent' cannot be tracked because another instance
    datatable to entiy list 不支持可空类型和枚举类型
    webapi 设置不显示接口到swaggerUI
  • 原文地址:https://www.cnblogs.com/LiaoHao/p/3334209.html
Copyright © 2011-2022 走看看