【问题标题】:how to create Android Burger Button with options icon?如何使用选项图标创建 Android 汉堡按钮?
【发布时间】:2015-11-18 17:43:23
【问题描述】:

在 android 中创建一个带有多个自定义选项的汉堡按钮。当我单击按钮时,我希望它显示带有相应图标的选项(如图所示)。

我使用了 Popupmenu,但是如何在 popupmenu 选项中设置图标?

请帮帮我??

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 你希望将它添加到操作栏中还是不添加操作栏?
  • 我想让选项按钮如图所示。
  • 您可以使用带有自定义适配器的弹出菜单来显示带有文本的图标。对于那个汉堡包图标,请查看此链接stackoverflow.com/questions/26300480/…
  • @Neha 没有操作栏

标签: android xml android-layout


【解决方案1】:

您可以尝试使用 ListPopUpWindow 并将其锚定到汉堡按钮(以防您不使用操作栏)

http://developer.android.com/reference/android/widget/ListPopupWindow.html

String [] items = new String[]{"Item1" , "Item2", "Item3"};

View anchor= (Button)findViewById(R.id.BurgerButton);

 anchor.setOnClickListener(new OnClickListener(){

     showpopup();

});


public void showpopup()
{


ListPopupWindow popup = new ListPopupWindow(this);
//Replace this  adapter with a custom adapter and layout for a custom menu item
popup.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1,items));
popup.setAnchorView(anchor);
popup.setWidth(200);
popup.show();

}

【讨论】:

    【解决方案2】:

    试试这个: 弹出菜单

        <menu xmlns:androclass="http://schemas.android.com/apk/res/android" >  
    
            <item  
                android:id="@+id/one"  
                android:title="One"
                android:icon="@drawable:ic_launcher"/>  
    
            <item  
                android:id="@+id/two"  
                android:title="Two"/>  
    
            <item  
                android:id="@+id/three"  
                android:title="Three"/>  
    
        </menu>  
    

    在您的活动按钮上单击添加:

     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  
    

    希望对你有帮助:)

    对于要显示的菜单图标添加:

    // Force icons to show
                    Object menuHelper;
                    Class[] argTypes;
                    try {
                        Field fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
                        fMenuHelper.setAccessible(true);
                        menuHelper = fMenuHelper.get(popup);
                        argTypes = new Class[] { boolean.class };
                        menuHelper.getClass().getDeclaredMethod("setForceShowIcon", argTypes).invoke(menuHelper, true);
                    } catch (Exception e) {
                        // Possible exceptions are NoSuchMethodError and NoSuchFieldError
                        //
                        // In either case, an exception indicates something is wrong with the reflection code, or the
                        // structure of the PopupMenu class or its dependencies has changed.
                        //
                        // These exceptions should never happen since we're shipping the AppCompat library in our own apk,
                        // but in the case that they do, we simply can't force icons to display, so log the error and
                        // show the menu normally.
    
                        Log.w("error", "error forcing menu icons to show", e);
                        popup.show();
                        return;
                    }
    

    【讨论】:

    • 我已经尝试过这段代码...... /跨度>
    • 是的,这段代码可以工作,但我想要带有图标的弹出选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    • 1970-01-01
    • 2018-10-12
    • 2021-12-14
    相关资源
    最近更新 更多