Android之自訂系統功能表欄背景

來源:互聯網
上載者:User

關鍵代碼為重寫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>   

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.