Android編程實現(xiàn)自定義系統(tǒng)菜單背景的方法
更新時間:2016年01月06日 16:09:23 作者:Sodino
這篇文章主要介紹了Android編程實現(xiàn)自定義系統(tǒng)菜單背景的方法,涉及Android菜單menu的實現(xiàn)及背景圖片的相關操作技巧,需要的朋友可以參考下
本文實例講述了Android編程實現(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);
}
// 關鍵代碼為重寫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程序設計有所幫助。
您可能感興趣的文章:
- Android開發(fā)技巧之我的菜單我做主(自定義菜單)
- android 自定義Android菜單背景的代碼
- Android使用自定義控件HorizontalScrollView打造史上最簡單的側滑菜單
- Android實現(xiàn)自定義的衛(wèi)星式菜單(弧形菜單)詳解
- Android實現(xiàn)自定義滑動式抽屜菜單效果
- Android自定義ViewGroup實現(xiàn)帶箭頭的圓角矩形菜單
- Android自定義控件簡單實現(xiàn)側滑菜單效果
- android自定義popupwindow仿微信右上角彈出菜單效果
- Android自定義view實現(xiàn)圓形與半圓形菜單
- Android自定義控件之仿優(yōu)酷菜單
- Android自定義VIew實現(xiàn)衛(wèi)星菜單效果淺析
- Android自定義控件案例匯總2(自定義開關、下拉刷新、側滑菜單)
- Android編程自定義菜單實現(xiàn)方法詳解
相關文章
Studio 編譯報錯:compileSdkVersion ''android-24'' requires JDK 1.
今天小編就為大家分享一篇關于Studio編譯報錯:compileSdkVersion 'android-24' requires JDK 1.8 or later to compile.的解決辦法,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
Android入門之使用RecyclerView完美實現(xiàn)瀑布流界面詳解
網(wǎng)上充滿著不完善的基于RecyclerView的瀑布流實現(xiàn),要么根本是錯的、要么就是只知其一不知其二。本文就來用RecyclerView完美實現(xiàn)瀑布流界面,希望大家有所幫助2023-02-02

