日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 自定义menu背景,Android编程实现自定义系统菜单背景的方法

發布時間:2023/12/9 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 自定义menu背景,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:sodinoopen@hotmail.com

* @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.

* context - The context the view is being created in.

* attrs - Inflation attributes as specified in XML file.

*/

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

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

總結

以上是生活随笔為你收集整理的android 自定义menu背景,Android编程实现自定义系统菜单背景的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。