PopupMenu样式修改

一、修改PopupMenu的背景颜色

编写样式

然后在主题中加入样式

@style/PopupMenuBgStyle

二、修改PopupMenu字体颜色大小

编写样式

然后在主题中加入样式

@style/PopupMenuTextStyle
@style/PopupMenuTextStyle

三、Item的高度设置

在主题中加入以下item

35dp

设置分界线

编写样式

然后在主题中加入样式

@style/PopupMenuDivier

五、修改PopupMenu显示的位置

关键代码

MenuPopupHelper helper = (MenuPopupHelper) field.get(popupMenu);    
helper.show(x,y);

具体实现

PopupMenu popupMenu = new PopupMenu(this,view);
popupMenu.inflate(R.menu.menu_applist_right_click);
try {    
    Field field = popupMenu.getClass().getDeclaredField("mPopup");
    field.setAccessible(true);    
    MenuPopupHelper helper = (MenuPopupHelper) field.get(popupMenu);    
    helper.show(x,y);
} catch (IllegalAccessException e) {    
    e.printStackTrace();
} catch (NoSuchFieldException e) {    
    e.printStackTrace();
} catch (ClassNotFoundException e) {    
    e.printStackTrace();
}

六、修改Menutem点击后颜色

关键代码

menuPopupWindow.getListView().setSelector(new ColorDrawable(Color.TRANSPARENT));

具体实现

PopupMenu popupMenu = new PopupMenu(this,view);
popupMenu.inflate(R.menu.menu_applist_right_click);
try {    
    Field field = popupMenu.getClass().getDeclaredField("mPopup");
    field.setAccessible(true);    
    MenuPopupHelper helper = (MenuPopupHelper) field.get(popupMenu);
    @SuppressLint("RestrictedApi")    
    Object standardMenuPopup = helper.getPopup();    
    Field mPopupField = Class.forName("androidx.appcompat.view.menu.StandardMenuPopup")
        .getDeclaredField("mPopup");    
    mPopupField.setAccessible(true);    
    MenuPopupWindow menuPopupWindow = (MenuPopupWindow) mPopupField.get(standardMenuPopup);    
    popupMenu.show();    
    menuPopupWindow.getListView().setSelector(new ColorDrawable(Color.TRANSPARENT));
} catch (IllegalAccessException e) {    
    e.printStackTrace();
} catch (NoSuchFieldException e) {    
    e.printStackTrace();
} catch (ClassNotFoundException e) {    
    e.printStackTrace();
}

七、修改鼠标悬停(hover)状态时的背景色

关键代码

int index = menu.findItemIndex(item.getItemId());
menuPopupWindow.getListView().getChildAt(index).setBackgroundColor(Color.TRANSPARENT);

具体实现

PopupMenu popupMenu = new PopupMenu(this,view);
popupMenu.inflate(R.menu.menu_applist_right_click);
try {
    Field field = popupMenu.getClass().getDeclaredField("mPopup");
    field.setAccessible(true);
    MenuPopupHelper helper = (MenuPopupHelper) field.get(popupMenu);
    @SuppressLint("RestrictedApi")
    Object standardMenuPopup = helper.getPopup();
    Field mPopupField = Class.forName("androidx.appcompat.view.menu.StandardMenuPopup")
        .getDeclaredField("mPopup");
    mPopupField.setAccessible(true);
    MenuPopupWindow menuPopupWindow = (MenuPopupWindow) 
        mPopupField.get(standardMenuPopup);
    menuPopupWindow.setHoverListener(new MenuItemHoverListener() {
        @Override
        public void onItemHoverExit(@NonNull MenuBuilder menu, @NonNull MenuItem item) {
            int index = menu.findItemIndex(item.getItemId());                 
            menuPopupWindow.getListView().getChildAt(index)
                .setBackgroundColor(Color.TRANSPARENT);
        }

        @Override
        public void onItemHoverEnter(@NonNull MenuBuilder menu, @NonNull MenuItem item) {
            int index = menu.findItemIndex(item.getItemId());
            menuPopupWindow.getListView().getChildAt(index)
                .setBackgroundColor(getColor(R.color.gridview_hovered));
        }
    });
    popupMenu.show();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (NoSuchFieldException e) {
    e.printStackTrace();
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

 

 

 

 

 

 

 

你可能感兴趣的:(PopupMenu样式修改)