我有一個片段。其中我有一個溢出菜單(工具欄上有三個點)。 我需要通過Material Showcase來突出顯示它們。如何獲取菜單ID?
我面對的麻煩是我無法獲得這些「三點」ID。 如何獲取或設置這些三點菜單的ID?
我有一個片段。其中我有一個溢出菜單(工具欄上有三個點)。 我需要通過Material Showcase來突出顯示它們。如何獲取菜單ID?
我面對的麻煩是我無法獲得這些「三點」ID。 如何獲取或設置這些三點菜單的ID?
剛查找源代碼,此按鈕沒有ID,它是根據需要添加的代碼。如果您只是想突出顯示它,我強烈建議您僅使用樣式或主題更改圖標或其顏色。
如果仍想獲得這一觀點,那麼你可以試試我的(未經測試)代碼:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); // That's your toolbar here
try {
// Toolbar has a private field mMenuView. Get it.
Field menuViewField = Toolbar.class.getDeclaredField("mMenuView");
menuViewField.setAccessible(true);
ActionMenuView menuView = (ActionMenuView) menuViewField.get(toolbar);
// ActionMenuView has a private field mPresenter. Get it.
Field menuPresenterField = ActionMenuView.class.getDeclaredField("mPresenter");
menuPresenterField.setAccessible(true);
Object menuPresenter = menuPresenterField.get(menuView);
// ActionMenuPresenter has a private field mOverflowButton. Go get your button.
Class<?> menuPresenterClass = Class.forName("android.widget.ActionMenuPresenter");
Field overflowBtnField = menuPresenterClass.getDeclaredField("mOverflowButton");
overflowBtnField.setAccessible(true);
ImageButton overflowBtn = (ImageButton) menuPresenterField.get(menuPresenter);
} catch (Exception e) {
Log.d(TAG, "Couldn't find overflow button", e);
}
可能會失敗,但試試也無妨。僅適用於工具欄,而不是ActionBar。如果它有效,你可能不應該使用它,因爲如果字段名稱在新版本中改變或者在其他API中不同,它將不起作用。另外,如果您嘗試獲取不存在的溢出按鈕,將會失敗。 (仍然值得讚賞的努力:)
祝你好運。
卡住「無法解析符號」ActionMenuPresenter'「。沒有任何建議,導入谷歌.. –
我改變了我的代碼。確保爲工具欄和ActionMenuView導入v7類 –
,你應該能夠保持從
private View view;
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem actionViewItem = menu.findItem(R.id.your_menu_item);
view = MenuItemCompat.getActionView(actionViewItem);
return super.onPrepareOptionsMenu(menu);
}
到溢出視圖的引用則應以展示
new MaterialShowcaseView.Builder(this)
.setTarget(view)
.show();
這會返回我java.lang.NullPointerException:嘗試在空對象引用上調用虛擬方法'void android.view.View.getLocationInWindow(int [])'。 –
is展示生成器在onPrepareOptionsMenu之前調用? –
的三個點這一觀點被稱爲溢出圖標。請參閱[這個問題](http://stackoverflow.com/questions/9733312/changing-overflow-icon-in-the-action-bar), 和[this one](http://stackoverflow.com/questions/ 29038861/how-to-change-toolbar-navigation-and-overflow-menu-icons-appcompat-v7)如何更改圖標,這正是我所瞭解的你想要做的。 –
@NicolasMaltais這不是一個圖標。這是關於overlow id。我需要它ID來找到它的ViewById。 –
發佈菜單xml和分片代碼 – FAT