【问题标题】:How to set a light popup theme?如何设置灯光弹出主题?
【发布时间】:2018-05-14 07:36:23
【问题描述】:

我的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);
    }
}

但它不起作用......我尝试了不同的风格,但我的背景总是黑色的。如何设置浅色主题?

【问题讨论】:

    标签: android android-appcompat android-styles


    【解决方案1】:

    在了解了源代码和 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);
        }
    }
    

    当然,您还需要从ThemeOverlay.AppCompat.Light 派生出您的Toolbar 的弹出主题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多