How to create a custom PopupMenu in Android

前端 未结 3 1859
花落未央
花落未央 2020-12-14 03:26

How can I replicate something like I made below in Balsamiq?

I made this menu, but it is only displaying the text of the items (not the icons). Is it possible to di

相关标签:
3条回答
  • 2020-12-14 04:05

    I hope my previous answer Here can help you.
    If you just want a similar popup menu, you can use ActionProvider. It's more powerful.
    If you want it as a true menu, you can use custom PopupMenu.

    0 讨论(0)
  • 2020-12-14 04:11

    I resolved this issue by simply putting the create_post_menu inside of the item whose icon is a +.

    For example, I have (using AppCompat):

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <item
                android:id="@+id/action_new"
                android:icon="@drawable/ic_action_new"
                android:title="@string/action_new"
                app:showAsAction="always">
    
                <menu>
    
                    <item
                        android:id="@+id/action_photo"
                        android:icon="@drawable/ic_action_camera"
                        android:title="@string/action_photo"
                        app:showAsAction="always|withText" />
                    <item
                        android:id="@+id/action_video"
                        android:icon="@drawable/ic_action_video"
                        android:title="@string/action_video"
                        app:showAsAction="always|withText" />
                    <item
                        android:id="@+id/action_text"
                        android:icon="@drawable/ic_action_text"
                        android:title="@string/action_text"
                        app:showAsAction="always|withText" />
                    <item
                        android:id="@+id/action_place"
                        android:icon="@drawable/ic_action_place"
                        android:title="@string/action_place"
                        app:showAsAction="always|withText" />
                    <item
                        android:id="@+id/action_more"
                        android:title="@string/action_more"
                        android:visible="false"
                        app:showAsAction="always|withText" />
    
                </menu>
            </item>
            ...(more menu items here)
    </menu>
    

    Without AppCompat, you could just get rid of the XML Namespace app by replacing app with android.

    0 讨论(0)
  • 2020-12-14 04:32
    import android.os.Bundle;  
    import android.app.Activity;  
    import android.view.Menu;  
    import android.view.MenuItem;  
    import android.view.View;  
    import android.view.View.OnClickListener;  
    import android.widget.Button;  
    import android.widget.PopupMenu;  
    import android.widget.Toast;  
    public class MainActivity extends Activity {  
    Button button1;  
    
             @Override  
             protected void onCreate(Bundle savedInstanceState) {  
              super.onCreate(savedInstanceState);  
              setContentView(R.layout.activity_main);  
    
              button1 = (Button) findViewById(R.id.button1);//your created butto
              button1.setOnClickListener(new OnClickListener() {  
    
               @Override  
               public void onClick(View v) {  
                //Creating the instance of PopupMenu  
                PopupMenu popup = new PopupMenu(MainActivity.this, button1);  
                //Inflating the Popup using xml file  
                popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());  
    
                //registering popup with OnMenuItemClickListener  
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
                 public boolean onMenuItemClick(MenuItem item) {  
                  Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();  
                  return true;  
                 }  
                });  
    
                popup.show();//showing popup menu  
               }  
              });//closing the setOnClickListener method  
             }  
        }  
    
    0 讨论(0)
提交回复
热议问题