首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建带有选项图标的Android汉堡按钮?

问如何创建带有选项图标的Android汉堡按钮?
EN

Stack Overflow用户
提问于 2015-08-25 14:26:13
回答 2查看 1.7K关注 0票数 1

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

​

​

我使用了弹出菜单,但是如何在弹出菜单选项中设置图标?

请帮帮我??

EN

回答 2

Stack Overflow用户

发布于 2015-08-25 14:57:41

您可以尝试使用ListPopUpWindow并将其锚定到汉堡按钮(如果您没有使用actionbar)

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

代码语言:javascript
复制
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();

}
票数 0
EN

Stack Overflow用户

发布于 2015-08-25 15:07:29

试试这个: popup_menu

代码语言:javascript
复制
    <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>  

在activity on按钮中,单击add this:

代码语言:javascript
复制
 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  

希望它能有所帮助:)

对于菜单图标,显示添加:

代码语言:javascript
复制
// 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;
                }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32196901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档