在我的应用程序主题中,我有用于整个应用程序的dropDownListViewStyle
和spinnerStyle
。但在一个片段中,我需要具有自定义样式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
,考虑到它是开源的,这并不太难。