Android 中menu同時(shí)顯示圖標(biāo)和文字的實(shí)現(xiàn)
Android 中menu同時(shí)顯示圖標(biāo)和文字的實(shí)現(xiàn)
前言:
Activity中的menu條目,在設(shè)置其showAsAction=”never”時(shí),默認(rèn)只顯示文字title,而不會(huì)顯示圖標(biāo)icon,可以在Activity中重寫onMenuOpened(),通過反射使其圖標(biāo)可見。
實(shí)現(xiàn)效果圖:

// 讓菜單同時(shí)顯示圖標(biāo)和文字
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (menu != null) {
if (menu.getClass().getSimpleName().equalsIgnoreCase("MenuBuilder")) {
try {
Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
method.setAccessible(true);
method.invoke(menu, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return super.onMenuOpened(featureId, menu);
}
下面貼下我的menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="@+id/menu_search"
android:orderInCategory="100"
android:title="搜索"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"
/>
<item
android:id="@+id/scan_local_music"
android:icon="@mipmap/lv"
android:orderInCategory="100"
android:title="掃描本地音樂"
app:showAsAction="never"
/>
<item
android:id="@+id/select_sort_way"
android:icon="@mipmap/lt"
android:orderInCategory="100"
android:title="選擇排序方式"
app:showAsAction="never"
/>
<item
android:id="@+id/get_cover_lyrics"
android:icon="@mipmap/lq"
android:orderInCategory="100"
android:title="獲取封面歌詞"
app:showAsAction="never"
/>
<item
android:id="@+id/imporve_tone_quality"
android:icon="@mipmap/lw"
android:orderInCategory="100"
android:title="升級音質(zhì)"
app:showAsAction="never"
/>
</menu>
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Android中menu使用詳解
- Android標(biāo)題欄上添加多個(gè)Menu按鈕的實(shí)例
- Android中PopupMenu組件的使用實(shí)例
- Android動(dòng)態(tài)修改ToolBar的Menu菜單示例
- Android制作微信app頂部menu菜單(ActionBar)
- Android編程實(shí)現(xiàn)自定義PopupMenu樣式示例【顯示圖標(biāo)與設(shè)置RadioButton圖標(biāo)】
- Android UI實(shí)現(xiàn)SlidingMenu側(cè)滑菜單效果
- Android學(xué)習(xí)教程之圓形Menu菜單制作方法(1)
相關(guān)文章
Android?Studio實(shí)現(xiàn)購買售賣系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)購買售賣系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Android 實(shí)現(xiàn)帶字母索引的側(cè)邊欄功能
這篇文章主要介紹了Android 實(shí)現(xiàn)帶字母索引的側(cè)邊欄功能,需要的朋友可以參考下2017-08-08
Android無需讀寫權(quán)限通過臨時(shí)授權(quán)讀寫用戶文件詳解
這篇文章主要為大家介紹了Android無需讀寫權(quán)限通過臨時(shí)授權(quán)讀寫用戶文件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android Studio實(shí)現(xiàn)搜索欄
這篇文章主要為大家詳細(xì)介紹了Android Studio實(shí)現(xiàn)搜索欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
Android自定義View實(shí)現(xiàn)簡易畫板功能
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)簡易畫板功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android應(yīng)用中圖片瀏覽時(shí)實(shí)現(xiàn)自動(dòng)切換功能的方法詳解
這篇文章主要介紹了Android應(yīng)用中圖片瀏覽時(shí)實(shí)現(xiàn)自動(dòng)切換功能的方法,文中還講解了一個(gè)觸摸大圖進(jìn)行圖片切換的深入功能,需要的朋友可以參考下2016-04-04
Android實(shí)現(xiàn)長按圓環(huán)動(dòng)畫View效果的思路代碼
這篇文章主要介紹了Android實(shí)現(xiàn)長按圓環(huán)動(dòng)畫View效果,本文給大家分享實(shí)現(xiàn)思路,通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09

