PopupMenu 使用及自定義樣式


Activity引用的XML樣式配置  android:theme="@style/AppBaseTheme"

<style name="AppBaseTheme" parent="AppTheme">
        <item name="popupMenuStyle">@style/myPopupMenu</item>
        <item name="android:textAppearanceLargePopupMenu">@style/myPopupMenuTextAppearanceLarge
        </item>
        <item name="android:textAppearanceSmallPopupMenu">@style/myPopupMenuTextAppearanceSmall
        </item>
        <item name="android:dropDownListViewStyle">@style/MyListViewStyle</item>
    </style>

    <style name="myPopupMenu" parent="@style/Widget.AppCompat.PopupMenu">
        <item name="android:popupBackground">@color/Cyan1</item>
    </style>

    <style name="myPopupMenuTextAppearanceSmall" parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small">
        <item name="android:textColor">@android:color/white</item>
        <item name="android:textSize">20sp</item>
    </style>

    <style name="myPopupMenuTextAppearanceLarge" parent="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Large">
        <item name="android:textColor">@android:color/white</item>
        <item name="android:textSize">20sp</item>
    </style>

    <style name="MyListViewStyle">
        <item name="android:divider">@color/Green1</item>
        <item name="android:dividerHeight">1px</item>
    </style>
樣式配置

color顏色

<color name="Cyan1">#00FFFF</color>
<color name="Green1">#00FF00</color>
顏色

Menu布局

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu1"
        android:orderInCategory="100"
        android:title="菜單一"
        app:showAsAction="never"></item>
    <item
        android:id="@+id/menu2"
        android:orderInCategory="100"
        android:title="菜單二"
        app:showAsAction="never"></item>
</menu>
menu布局

代碼實現

PopupMenu popupMenu = new PopupMenu(getActivity(), v, Gravity.LEFT);
                popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.menu1:
                                Log.d("CESHI", "MENU1");
                                break;
                            case R.id.menu2:
                                Log.d("CESHI", "MENU2");
                                break;
                        }
                        return false;
                    }
                });
                popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
                    @Override
                    public void onDismiss(PopupMenu menu) {

                    }
                });
                popupMenu.show();
代碼實現

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM