gpt4 book ai didi

android - Android 上带图标的弹出菜单

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:55:25 24 4
gpt4 key购买 nike

我的菜单 xml 代码menu.xml:

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Single menu item
Set id, icon and Title for each menu item
-->

<item android:id="@+id/back"
android:icon="@drawable/back1"
android:showAsAction="never"
android:title="Back" />

<item android:id="@+id/My_Profile"
android:icon="@drawable/myprofile"
android:showAsAction="never"
android:title="My Profile" />

<item android:id="@+id/Job_Alert"
android:icon="@drawable/jobalert4"
android:showAsAction="never"
android:title="Job Alert !" />

<item android:id="@+id/saved_job"
android:icon="@drawable/jobapplied"
android:title="Saved Jobs"
/>


<item android:id="@+id/Logout"
android:icon="@drawable/logout"
android:title="Logout" />
</menu>

我这样调用菜单xml

     PopupMenu popup = new PopupMenu(getBaseContext(), v);
popup.getMenuInflater().inflate(R.menu.menu, popup.getMenu());
popup.show();

但它不显示图标。

如何在弹出菜单上设置图标?

最佳答案

您可以使用 MenuBuilderMenuPopupHelper 创建带图标的弹出菜单。

MenuBuilder menuBuilder =new MenuBuilder(this);
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.menu, menuBuilder);
MenuPopupHelper optionsMenu = new MenuPopupHelper(this, menuBuilder, view);
optionsMenu.setForceShowIcon(true);

// Set Item Click Listener
menuBuilder.setCallback(new MenuBuilder.Callback() {
@Override
public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
switch (item.getItemId()) {
case R.id.opt1: // Handle option1 Click
return true;
case R.id.opt2: // Handle option2 Click
return true;
default:
return false;
}
}

@Override
public void onMenuModeChange(MenuBuilder menu) {}
});

optionsMenu.show();

菜单.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/opt1"
android:icon="@mipmap/ic_launcher"
android:title="option 1" />
<item
android:id="@+id/opt2"
android:icon="@mipmap/ic_launcher"
android:title="option 2" />
</menu>

enter image description here

关于android - Android 上带图标的弹出菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20836385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com