Для моего 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);
}
}
Но это не работает ... Я пробовал разные стили, но мой фон всегда черный. Как установить светлую тему?
Изучив исходный код и стили 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.