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

时间:2016-10-28 09:47:48

标签: android xml android-layout material-design

我正在尝试在菜单按钮上单击弹出菜单,如下面的屏幕所示:

enter image description here

我尝试过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();
    }
}

但我得到以下结果: enter image description here

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

我是如何创建这样的弹出菜单的?任何帮助将不胜感激

1 个答案:

答案 0 :(得分:5)

您必须使用PopupMenu代替PopupWindow

enter image description here enter image description here

示例代码:

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_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="horizontal">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="#D6D7D7">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            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>