一手遮天 Android - view(弹出类): PopupMenu 样式

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

一手遮天 Android - view(弹出类): PopupMenu 样式

示例如下:

/view/flyout/PopupMenuDemo2.java

/**
 * PopupMenu - 弹出式菜单
 *
 * 本例会演示如何自定义 PopupMenu 的样式
 * 需要通过 activity 的主题来指定 PopupMenu 的样式,参见 res/values/styles.xml 中的 MyTheme_MyPopupMenuStyle
 *
 * 注:如果觉得 PopupMenu 实现不了需求,就用 PopupWindow 吧
 */


package com.webabcd.androiddemo.view.flyout;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.appcompat.widget.PopupMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.webabcd.androiddemo.R;

public class PopupMenuDemo2 extends AppCompatActivity {

    private Button mButton1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 指定 activity 的主题,用于修改 PopupMenu 的样式
        setTheme(R.style.MyTheme_MyPopupMenuStyle);

        setContentView(R.layout.activity_view_flyout_popupmenudemo2);

        mButton1 = findViewById(R.id.button1);

        sample();
    }

    private void sample() {
        mButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final PopupMenu popup = new PopupMenu(PopupMenuDemo2.this, mButton1);
                popup.getMenuInflater().inflate(R.menu.menu_view_flyout_popupmenudemo2, popup.getMenu());
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        Toast.makeText(PopupMenuDemo2.this, String.format("id:%d, title:%s", item.getItemId(), item.getTitle()), Toast.LENGTH_SHORT).show();

                        // 如果 item 数据来自 xml 的话,则可以通过此方式来判断用户点击的是哪个 item
                        if (item.getItemId() == popup.getMenu().getItem(1).getItemId()) {

                        }

                        return true;
                    }
                });
                popup.show();
            }
        });
    }
}

/layout/activity_view_flyout_popupmenudemo2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:text="弹出一个自定义样式的 PopupMenu" />

</LinearLayout>

/menu/menu_view_flyout_popupmenudemo2.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
    用于构造 PopupMenu 的选项数据,参见 view/flyout/PopupMenuDemo2.java
-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/item1" android:title="菜单 1" />
    <item android:id="@+id/item2" android:title="菜单 2" />
    <item android:id="@+id/item3" android:title="菜单 3" />
</menu>

项目地址 https://github.com/webabcd/AndroidDemo
作者 webabcd

posted @ 2021-05-31 13:23  webabcd  阅读(440)  评论(0编辑  收藏  举报