如何将自定义布局传递给PopupMenu? [英] How to pass a custom layout to a PopupMenu?

查看:178
本文介绍了如何将自定义布局传递给PopupMenu?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在android中自定义 popupmenu ,默认的弹出菜单会提供更多空间,所以我试图更改弹出菜单中的自定义布局,但我不知道怎么做.

I want to customize the popupmenu in android, default popup menu gives more space ,so I'm trying to change the custom layout in popup menu but I cant figure out how.

注意:我想做这个小的弹出式设计,所以我使用默认的弹出菜单,但是我想对其进行自定义.

Note: I want to do this small popup design so I go with default popup menu but i want to customize it.

findViewById(R.id.menuclick).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        PopupMenu popupMenu = new PopupMenu(Sample1.this, view);
        popupMenu.setOnMenuItemClickListener(Sample1.this);
        popupMenu.inflate(R.layout.menus_layout);
        popupMenu.show();
    }
});

推荐答案

要通过按钮 onClick 扩展 popupMenu ,请使用以下代码.

To inflate popupMenu from a button onClick, use the following code.

btn = (Button) findViewById(R.id.btn);   
btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PopupMenu popup = new PopupMenu(MainActivity.this, v);
            popup.getMenuInflater().inflate(R.menu.pop_up, popup.getMenu());

            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    Toast.makeText(MainActivity.this, "Some Text" + item.getTitle(), Toast.LENGTH_SHORT).show();
                    return true;
                }
            });
            popup.show();//showing popup menu
        }
    });

编辑

要设置popupMenu的样式,请添加以下样式.

To style the popupMenu, add the following style.

<style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
        <item name="android:popupBackground">#ffffff</item>
    </style>

我注意到您还想在文本旁边添加图标.可以在popupMenu中添加图标.但是,最好使用弹出窗口代替.这是示例代码:

I noticed you also want to add icons next to your text. It is possible to add icons in popupMenu. However it is a better approach to use popup Window instead. Here is a sample code:

PopupWindow mypopupWindow;
setPopUpWindow();  
     btn=(Button)findViewById(R.id.btn);  
     btn.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {              
           mypopupWindow.showAsDropDown(v,-153,0);  
           //showAsDropDown(below which view you want to show as dropdown,horizontal position, vertical position)  
         }  
       }  
     });  
   }  
   private void setPopUpWindow() {  
     LayoutInflater inflater = (LayoutInflater)  
         getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
     view = inflater.inflate(R.layout.popup, null);  

     Start=(RelativeLayout)view.findViewById(R.id.start_btn);  
     Pause=(RelativeLayout)view.findViewById(R.id.pause_btn);  
     Stop=(RelativeLayout)view.findViewById(R.id.stop_btn);

  mypopupWindow = new PopupWindow(view,300, RelativeLayout.LayoutParams.WRAP_CONTENT, true);

弹出式布局

<?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:orientation="vertical" android:layout_width="wrap_content"  
   android:background="@drawable/whitedrawable"  
   android:paddingRight="0dp"  
   android:layout_marginRight="0dp"  
   android:layout_height="wrap_content">  
   <RelativeLayout  
     android:id="@+id/btn1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content">  
     <ImageView  
       android:layout_centerHorizontal="true"  
       android:layout_centerVertical="true"  
       android:layout_alignParentLeft="true"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:src="@drawable/startimg"  
       android:id="@+id/startimg"  
       android:paddingLeft="10dp"  
       android:paddingRight="10dp"  
       android:paddingTop="5dp"  
       android:paddingBottom="5dp"  
       />  
     <TextView  
       android:layout_centerHorizontal="true"  
       android:layout_centerVertical="true"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:paddingRight="0dp"  
       android:text="Start"  
       android:layout_toRightOf="@+id/startimg"  
       />  
 <!-- Continue for other items-->

whitedrawable 可以用来设置您选择的背景.您可以使用9patch获取背景的阴影和圆角.

The whitedrawable can be used to set a background of your choice. You can use 9patch to get the shadow and rounded corners for the background.

要关闭popupWindow,请使用以下代码:

To dismiss the popupWindow, use the following code:

mypopupWindow.getContentView().setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mypopupWindow.dismiss();
    }
});

要使用后退"按钮关闭,请使用:

To dismiss using the back button, use:

@Override
public void onBackPressed() {
    if(mypopupWindow.isShowing()) {
        mypopupWindow.dismiss();
        return;
    }
    super.onBackPressed();
}

这篇关于如何将自定义布局传递给PopupMenu?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆