我的PopupWindow
需要一个轻量级主题,因此我尝试使用ContextThemeWrapper
进行更改:
if (R.id.action_filter == item.getItemId()) {
View filterView = getActivity().findViewById(R.id.action_filter);
assert filterView != null;
Context context = new ContextThemeWrapper(filterView.getContext(),
R.style.ThemeOverlay_AppCompat_ActionBar);
FilterWindow menu = new FilterWindow(context); //PopupWindow
menu.showAsDropDown(filterView, 0, -filterView.getHeight());
return true;
}
private static class FilterWindow extends PopupWindow {
FilterWindow(Context context) {
super(context);
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.menu_filter, null);
setContentView(view);
setOutsideTouchable(true);
setFocusable(true);
}
}
但它不起作用......我尝试了不同的风格,但我的背景总是黑的。如何设置灯光主题?
答案 0 :(得分:0)
在了解了源代码和AppCompat样式后,我知道它为什么不起作用。以下是一个有效的变体:
if (R.id.action_filter == item.getItemId()) {
View filterView = getActivity().findViewById(R.id.action_filter);
assert filterView != null;
FilterWindow menu = new FilterWindow(getContext());
menu.showAsDropDown(filterView, 0, -filterView.getHeight());
return true;
}
private static class FilterWindow extends PopupWindow {
FilterWindow(Context context) {
super(context, null, R.attr.popupMenuStyle);
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.menu_filter, null);
setContentView(view);
setOutsideTouchable(true);
setFocusable(true);
}
}
当然,您还需要从Toolbar
派生ThemeOverlay.AppCompat.Light
的弹出式主题。