创建自定义弹出对话框菜单

Posted

技术标签:

【中文标题】创建自定义弹出对话框菜单【英文标题】:Creating a custom popup dialog menu 【发布时间】:2017-03-11 04:33:06 【问题描述】:

我目前正在尝试在菜单按钮单击时实现弹出菜单,如下图所示:

我尝试了 popupwindow 方法,但无法达到确切的情况。 这就是我正在尝试的方式:

   private View.OnClickListener showPopupWindow() 
    return new View.OnClickListener() 

        @Override
        public void onClick(View v) 
            PopupWindow popUp = popupWindowsort();
            popUp.showAsDropDown(v, 1, 1); // show popup like dropdown list
        
    ;


private PopupWindow popupWindowsort() 

    // initialize a pop up window type
    popupWindow = new PopupWindow(context);

    ArrayList<String> sortList = new ArrayList<String>();
    sortList.add("VIEW FULL");
    sortList.add("REPORT");
    sortList.add("ADD TO LIST");
    sortList.add("ADD TO CART");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.drop_down_line,
            sortList);
    // the drop down list is a list view
    ListView listViewSort = new ListView(context);

    // set our adapter and pass our pop up window contents
    listViewSort.setAdapter(adapter);

    // set on item selected
    listViewSort.setOnItemClickListener(onItemClickListener());

    // some other visual settings for popup window
    popupWindow.setFocusable(true);
    popupWindow.setWidth(300);
    popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.background));
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);


    // set the listview as popup content
    popupWindow.setContentView(listViewSort);

    return popupWindow;


private AdapterView.OnItemClickListener onItemClickListener() 
    return new AdapterView.OnItemClickListener() 

        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) 
            if (position == 0) 

                // adapter.notifyDataSetChanged();
             else if (position == 1) 
                report_lay.setVisibility(View.VISIBLE);
                // adapter.notifyDataSetChanged();
             else 

                // adapter.notifyDataSetChanged();
                Log.i(TAG, "position2 " + position);
            
            dismissPopup();
        
    ;


private void dismissPopup() 
    if (popupWindow != null) 
        popupWindow.dismiss();
    

但我得到以下结果:

这也会导致棉花糖出现问题。

如何创建这样的弹出菜单?

【问题讨论】:

【参考方案1】:

您必须使用PopupMenu 而不是PopupWindow

示例代码:

public class MainActivity extends AppCompatActivity 

    private Context context;
    private ImageView img;

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        img = (ImageView) findViewById(R.id.imageView);
        img.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, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
                        return true;
                    
                );
                popup.show();//showing popup menu
            
        );
    

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/base"
    android:layout_
    android:layout_
    android:background="@color/white"
    android:orientation="horizontal">


    <RelativeLayout
        android:layout_
        android:layout_
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="#D6D7D7">

        <ImageView
            android:id="@+id/imageView"
            android:layout_
            android:layout_
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:layout_marginEnd="20dp"
            android:gravity="center"
            android:src="@mipmap/more" />
    </RelativeLayout>
</RelativeLayout>

菜单/pop_up.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/add"
        android:title="@string/one" />
    <item
        android:id="@+id/sub"
        android:title="@string/two" />
    <item
        android:id="@+id/mul"
        android:title="@string/three" />
    <item
        android:id="@+id/div"
        android:title="@string/four" />
</menu>

更新:菜单背景颜色变化

在您应用的主题中使用此样式。

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!--Add modified themes-->
        <item name="android:popupMenuStyle">@style/PopupMenu</item>
        <item name="popupMenuStyle">@style/PopupMenu</item>
        <item name="android:itemTextAppearance">@style/itemTextStyle.AppTheme</item>
    </style>

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

    <style name="itemTextStyle.AppTheme" parent="@android:style/TextAppearance.Widget.IconMenu.Item">
        <item name="android:textColor">@color/white</item>
    </style>
</resources>

【讨论】:

可以加图片详细说明 我应该在哪里添加截图

以上是关于创建自定义弹出对话框菜单的主要内容,如果未能解决你的问题,请参考以下文章

如何使用自定义布局创建对话框弹出窗口?

word中怎样自定义编号

Android IME:显示一个自定义弹出对话框(如 Swype 键盘),可以在 TextView 中输入文本

access 查询条件 能否用自定义的函数

优化自定义Dialog的弹出速度

SDI-通过选定的弹出菜单项显示对话框