Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android編程實現自定義系統菜單背景的方法

Android編程實現自定義系統菜單背景的方法

編輯:關於Android編程

本文實例講述了Android編程實現自定義系統菜單背景的方法。分享給大家供大家參考,具體如下:

不多說,上圖,見代碼。

package lab.sodino.menutest;
import android.content.Context;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
/**
 * @author Sodino E-mail:[email protected]
 * @version Time:2011-1-26 下午04:42:04
 */
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>

希望本文所述對大家Android程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved