Android編程實現(xiàn)自定義系統(tǒng)菜單背景的方法
更新時間:2016年01月06日 16:09:23 作者:Sodino
這篇文章主要介紹了Android編程實現(xiàn)自定義系統(tǒng)菜單背景的方法,涉及Android菜單menu的實現(xiàn)及背景圖片的相關(guā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); } // 關(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的對象 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打造史上最簡單的側(cè)滑菜單
- Android實現(xiàn)自定義的衛(wèi)星式菜單(弧形菜單)詳解
- Android實現(xiàn)自定義滑動式抽屜菜單效果
- Android自定義ViewGroup實現(xiàn)帶箭頭的圓角矩形菜單
- Android自定義控件簡單實現(xiàn)側(cè)滑菜單效果
- android自定義popupwindow仿微信右上角彈出菜單效果
- Android自定義view實現(xiàn)圓形與半圓形菜單
- Android自定義控件之仿優(yōu)酷菜單
- Android自定義VIew實現(xiàn)衛(wèi)星菜單效果淺析
- Android自定義控件案例匯總2(自定義開關(guān)、下拉刷新、側(cè)滑菜單)
- Android編程自定義菜單實現(xiàn)方法詳解
相關(guān)文章
Android實現(xiàn)QQ新用戶注冊界面遇到問題及解決方法
這篇文章主要介紹了Android實現(xiàn)QQ新用戶注冊界面遇到問題及解決方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09Android Service服務詳細介紹及使用總結(jié)
這篇文章主要介紹了Android Service 服務的詳細資料,網(wǎng)上關(guān)于Android Service 服務的文章比較多,但是不是很全面,不夠細致,畢竟是Android 四大組件之一,重要性不用說,這里總結(jié)下,需要的朋友可以參考下2016-12-12基于Android如何實現(xiàn)將數(shù)據(jù)庫保存到SD卡
有時候為了需要,會將數(shù)據(jù)庫保存到外部存儲或者SD卡中(對于這種情況可以通過加密數(shù)據(jù)來避免數(shù)據(jù)被破解),本文給大家分享Android如何實現(xiàn)將數(shù)據(jù)庫保存到SD卡,對android數(shù)據(jù)庫sd卡相關(guān)知識感興趣的朋友一起學習吧2015-12-12android studio3.0.1無法啟動Gradle守護進程的解決方法
這篇文章主要為大家詳細介紹了android studio3.0.1無法啟動Gradle守護進程的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08flutter InheritedWidget使用方法總結(jié)
這篇文章主要為大家介紹了flutter InheritedWidget使用方法總結(jié)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11