Android編程實(shí)現(xiàn)自定義系統(tǒng)菜單背景的方法
本文實(shí)例講述了Android編程實(shí)現(xiàn)自定義系統(tǒng)菜單背景的方法。分享給大家供大家參考,具體如下:
不多說,上圖,見代碼。

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);
}
// 關(guān)鍵代碼為重寫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的對(duì)象
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() {
// 設(shè)置背景圖片
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>
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android開發(fā)技巧之我的菜單我做主(自定義菜單)
- android 自定義Android菜單背景的代碼
- Android使用自定義控件HorizontalScrollView打造史上最簡(jiǎn)單的側(cè)滑菜單
- Android實(shí)現(xiàn)自定義的衛(wèi)星式菜單(弧形菜單)詳解
- Android實(shí)現(xiàn)自定義滑動(dòng)式抽屜菜單效果
- Android自定義ViewGroup實(shí)現(xiàn)帶箭頭的圓角矩形菜單
- Android自定義控件簡(jiǎn)單實(shí)現(xiàn)側(cè)滑菜單效果
- android自定義popupwindow仿微信右上角彈出菜單效果
- Android自定義view實(shí)現(xiàn)圓形與半圓形菜單
- Android自定義控件之仿優(yōu)酷菜單
- Android自定義VIew實(shí)現(xiàn)衛(wèi)星菜單效果淺析
- Android自定義控件案例匯總2(自定義開關(guān)、下拉刷新、側(cè)滑菜單)
- Android編程自定義菜單實(shí)現(xiàn)方法詳解
相關(guān)文章
Android實(shí)現(xiàn)QQ新用戶注冊(cè)界面遇到問題及解決方法
這篇文章主要介紹了Android實(shí)現(xiàn)QQ新用戶注冊(cè)界面遇到問題及解決方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
Android Service服務(wù)詳細(xì)介紹及使用總結(jié)
這篇文章主要介紹了Android Service 服務(wù)的詳細(xì)資料,網(wǎng)上關(guān)于Android Service 服務(wù)的文章比較多,但是不是很全面,不夠細(xì)致,畢竟是Android 四大組件之一,重要性不用說,這里總結(jié)下,需要的朋友可以參考下2016-12-12
基于Android如何實(shí)現(xiàn)將數(shù)據(jù)庫(kù)保存到SD卡
有時(shí)候?yàn)榱诵枰瑫?huì)將數(shù)據(jù)庫(kù)保存到外部存儲(chǔ)或者SD卡中(對(duì)于這種情況可以通過加密數(shù)據(jù)來避免數(shù)據(jù)被破解),本文給大家分享Android如何實(shí)現(xiàn)將數(shù)據(jù)庫(kù)保存到SD卡,對(duì)android數(shù)據(jù)庫(kù)sd卡相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-12-12
android獲取附近藍(lán)牙設(shè)備并計(jì)算距離的實(shí)例代碼
下面小編就為大家分享一篇android獲取附近藍(lán)牙設(shè)備并計(jì)算距離的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
android studio3.0.1無法啟動(dòng)Gradle守護(hù)進(jìn)程的解決方法
這篇文章主要為大家詳細(xì)介紹了android studio3.0.1無法啟動(dòng)Gradle守護(hù)進(jìn)程的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
flutter InheritedWidget使用方法總結(jié)
這篇文章主要為大家介紹了flutter InheritedWidget使用方法總結(jié)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

