英文:
How to show Icon in drop down menu of PopupMenu
问题
Sure, here's the translated code portion:
showMenuButton = findViewById(R.id.btn_long_press);
// Initialize popup menu
final PopupMenu popupMenu = new PopupMenu(
this, // the context
showMenuButton // UI view where to click to show the popup menu
);
// Add menu XML to our popup menu
popupMenu.getMenuInflater().inflate(R.menu.pop_menu, popupMenu.getMenu());
// Handle popup menu item clicks
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
// Get id of menu item clicked
int id = menuItem.getItemId();
// Handle clicks
if (id == R.id.settings_menu) {
// Settings selected
Toast.makeText(MainActivity.this, "Settings Selected", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, Main2Activity.class));
getTitleColor();
return true;
} else if (id == R.id.manual_menu) {
// Manual selected
Toast.makeText(MainActivity.this, "User Manual Selected", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, Main3Activity.class));
return true;
} else if (id == R.id.about_menu) {
// About selected
Toast.makeText(MainActivity.this, "About Selected", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, Main4Activity.class));
return true;
}
return false;
}
});
// Handle button click to show menu
showMenuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupMenu.show();
}
});
And here's the translated XML for the menu:
<item
android:id="@+id/settings_menu"
android:title="设置"
android:icon="@drawable/ic_settings"
app:showAsAction="always|withText" />
<item
android:id="@+id/manual_menu"
android:title="用户手册"
android:icon="@drawable/ic_developer" />
<item
android:id="@+id/about_menu"
android:title="关于"
android:icon="@drawable/ic_about"/>
Feel free to let me know if you need any further assistance!
英文:
I want to add some icon in the drop-down menu of a popupMenu i created. please help me. I am so much bothered on how to create popupMenu to show dropdown menu items with icon when clicked. I have tried a lot of things which did not work. I tried to set showAsAction to always|withtext, but it didn't work.
Please help me. Any help will be much appreciated!
Below is the Java
showMenuButton = findViewById(R.id.btn_long_press);
//Init popup menu
final PopupMenu popupMenu = new PopupMenu(
this, //the context
showMenuButton //UI view where to click to show the popup menu
);
//add menu xml to our popup menu
popupMenu.getMenuInflater().inflate(R.menu.pop_menu, popupMenu.getMenu());
//handle popup menu item clicks
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
//get id of menu item clicked
int id = menuItem.getItemId();
//handle clicks
if (id==R.id.settings_menu){
//settings selected
Toast.makeText(MainActivity.this, "Settings Selected", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, Main2Activity.class));
getTitleColor();
return true;
}
else if (id==R.id.manual_menu){
//Manual selected
Toast.makeText(MainActivity.this, "User Manual Selected", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, Main3Activity.class));
return true;
}
else if (id==R.id.about_menu){
//about selected
Toast.makeText(MainActivity.this, "About Selected", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, Main4Activity.class));
return true;
}
return false;
}
});
//handle button click to show menu
showMenuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupMenu.show();
}
});
this is the menu XML
<item
android:id="@+id/settings_menu"
android:title="Settings"
android:icon="@drawable/ic_settings"
app:showAsAction="always|withText" />
<item
android:id="@+id/manual_menu"
android:title="User Manual"
android:icon="@drawable/ic_developer" />
<item
android:id="@+id/about_menu"
android:title="About"
android:icon="@drawable/ic_about"/>
答案1
得分: 2
我从我的研究中发现,要显示带有菜单项图标的下拉弹出菜单需要深入的处理。而且这个过程不够清晰。然而,我找到了一个解决方案。创建子菜单似乎是唯一的方法来获得我们想要的效果。我鼓励任何寻求创建菜单图标的人都要遵循我目前找到的这种方法,同时等待Google的进一步改进。
<?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">
<item
android:id="@+id/action_more"
android:icon="@android:drawable/ic_menu_more"
android:orderInCategory="1"
android:title="更多选项 »"
app:showAsAction="always">
<menu>
<item
android:id="@+id/settings_menu"
android:title="设置"
android:icon="@drawable/ic_settings"/>
<item
android:id="@+id/manual_menu"
android:title="用户手册"
android:icon="@drawable/ic_developer" />
<item
android:id="@+id/about_menu"
android:title="关于"
android:icon="@drawable/ic_about"/>
</menu>
</item>
</menu>
英文:
I discovered from my research that to show drop-down popup menu with menu item icon takes deep process. And the process is not clean.
However, i have found a solution. Creating sub menu seems to be the only lee-way to get what we want. And I encourage anyone seeking to create menu icon to follow this way I founded for now while we wait further improvement from google.
<?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">
<item
android:id="@+id/action_more"
android:icon="@android:drawable/ic_menu_more"
android:orderInCategory="1"
android:title="More Options »"
app:showAsAction="always">
<menu>
<item
android:id="@+id/settings_menu"
android:title="Settings"
android:icon="@drawable/ic_settings"/>
<item
android:id="@+id/manual_menu"
android:title="User Manual"
android:icon="@drawable/ic_developer" />
<item
android:id="@+id/about_menu"
android:title="About"
android:icon="@drawable/ic_about"/>
</menu>
</item>
</menu>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论