不同的下拉列表视图样式用于不同的微调器样式



在我的应用程序主题中,我有用于整个应用程序的dropDownListViewStylespinnerStyle。但在一个片段中,我需要具有自定义样式dropDownListViewStyle的微调器(我需要更改分隔器)。是否可以将微调器与其他dropDownListViewStyle一起设置在主题中?

在微调器样式或布局中,无法设置下拉分隔符。也不可能在微调器样式或布局中设置dropDownListViewStyle。

我真的很困惑,希望有人能找到答案。

不幸的是,dropDownListViewStyle是用Spinner硬编码的。如果您查看源代码,您会发现DropdownPopup类,它扩展了ListPopupWindow。在ListPopupWindow中,感兴趣的类是DropDownListView,您可以在其中找到构造函数:

public DropDownListView(Context context, boolean hijackFocus) {
    super(context, null, com.android.internal.R.attr.dropDownListViewStyle);
    // ...
}

因此,正如你所评估的,改变这一点的唯一方法是通过主题。考虑到所讨论的Spinner正在需要基本主题的Activity中使用,我只知道一种解决方法。不幸的是,唯一的办法就是改变Fragment的主题。这意味着Fragment中的所有Spinners都将具有替代主题。要在运行时更改Fragment的主题,请在onCreateView中执行以下操作:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // create ContextThemeWrapper from the original Activity Context with the custom theme
        Context context = new ContextThemeWrapper(getActivity(), R.style.My_Custom_Theme);
        // clone the inflater using the ContextThemeWrapper
        LayoutInflater localInflater = inflater.cloneInContext(context);
        // inflate using the cloned inflater, not the passed in default 
        return localInflater.inflate(R.layout.my_layout, container, false);
    }

除此之外,您正在考虑创建一个自定义Spinner,考虑到它是开源的,这并不太难。

最新更新