我正在尝试设置所有对话框片段的样式,使其在我的应用程序中看起来相同。来自我的设置片段的对话框的样式完全按照我想要的方式设置。对于我的自定义对话框片段,样式相似但不完全相同。由于某种原因,我的自定义对话框片段中的微调器、时间选择器、日期选择器、单选按钮和编辑文本小部件不会采用相同的样式。事实上,这些小部件与白色背景融为一体,您看不到它们的存在。我做错了什么?
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"/>
<style name="Theme.Base" parent="AppTheme">
<item name="colorPrimary">@color/PrimaryBackgroundColor</item>
<item name="colorPrimaryDark">@color/SecondaryBackgroundColor</item>
<item name="colorAccent">@color/ColorBackgroundAccent</item>
<item name="android:textColorPrimary">@color/PrimaryTextColor</item>
<item name="android:alertDialogTheme">@style/AppTheme.DialogStyle</item>
</style>
<style name="AppTheme.DialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColorPrimary">@color/PrimaryBackgroundColor</item>
<item name="colorAccent">@color/ColorBackgroundAccent</item>
</style>
我将主题应用到我的自定义对话框片段,如下所示:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppTheme_DialogStyle);
如您所见,单选按钮选择了红色,您看不到未选中的单选按钮。
终于有答案了!!!
这是 AppCompat 22+ 的问题或错误。 查看这里的链接
显然这是一个片段错误,小部件没有获取片段中的主题材质。他们似乎解决了这个问题,但根据我正在经历的情况,该问题仍然存在于对话框片段中。 当您使用传递给
Fragment#onCreateView()
的充气器实例时,问题就出现了。目前的解决方法是根据谷歌的说法使用 LayoutInflater
中的 getActivity().getLayoutInflater()
。
所以我将代码更改为:
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog, null);
来自:
View view = LayoutInflater.from(getActivity().getApplicationContext()).inflate(R.layout.dialoge, null);
我所有的小部件现在都有主题了。感谢大家。希望这对其他人有帮助。
我相信您需要在实际对话框而不是片段上设置主题
使用此构造函数创建您的 AlertDialog:
AlertDialog.Builder(Context context, int theme)
即
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), theme)
我认为您需要在对话框的样式中添加一项。
android:textColorSecondary
将显示未选中复选框的颜色。
按照您的风格添加它。
</style>
<style name="AppTheme.DialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:textColorPrimary">@color/PrimaryBackgroundColor</item>
<item name="colorAccent">@color/ColorBackgroundAccent</item>
<item name="android:textColorSecondary">#000000</item>
</style>
它将使未选中的复选框或切换按钮边缘颜色变为黑色。您需要更改
#000000
来为您想要显示的颜色。