如何在弹出菜单的下拉菜单中显示图标

huangapple go评论162阅读模式
英文:

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, &quot;Settings Selected&quot;, 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, &quot;User Manual Selected&quot;, 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, &quot;About Selected&quot;, 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

&lt;item
android:id=&quot;@+id/settings_menu&quot;
android:title=&quot;Settings&quot;
android:icon=&quot;@drawable/ic_settings&quot;
app:showAsAction=&quot;always|withText&quot; /&gt;
&lt;item
android:id=&quot;@+id/manual_menu&quot;
android:title=&quot;User Manual&quot;
android:icon=&quot;@drawable/ic_developer&quot; /&gt;
&lt;item
android:id=&quot;@+id/about_menu&quot;
android:title=&quot;About&quot;
android:icon=&quot;@drawable/ic_about&quot;/&gt;

如何在弹出菜单的下拉菜单中显示图标

答案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="更多选项 &#187;"
        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.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;
&lt;item
android:id=&quot;@+id/action_more&quot;
android:icon=&quot;@android:drawable/ic_menu_more&quot;
android:orderInCategory=&quot;1&quot;
android:title=&quot;More Options &#187;&quot;
app:showAsAction=&quot;always&quot;&gt;
&lt;menu&gt;
&lt;item
android:id=&quot;@+id/settings_menu&quot;
android:title=&quot;Settings&quot;
android:icon=&quot;@drawable/ic_settings&quot;/&gt;
&lt;item
android:id=&quot;@+id/manual_menu&quot;
android:title=&quot;User Manual&quot;
android:icon=&quot;@drawable/ic_developer&quot; /&gt;
&lt;item
android:id=&quot;@+id/about_menu&quot;
android:title=&quot;About&quot;
android:icon=&quot;@drawable/ic_about&quot;/&gt;
&lt;/menu&gt;
&lt;/item&gt;
&lt;/menu&gt;

huangapple
  • 本文由 发表于 2020年8月13日 14:55:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63389683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定