样式自定义对话框片段不起作用

Styling custom dialog fragment not working

我正在尝试设置所有对话框片段的样式,使其在我的应用程序中看起来相同。来自我的设置片段的对话框的样式完全符合我想要的方式。对于我的自定义对话框片段,样式相似但不完全相同。出于某种原因,我的自定义对话框片段中的微调器、时间选择器、日期选择器、单选按钮和编辑文本小部件不会采用相同的样式。事实上,小部件与白色背景融为一体,您看不到它们的存在。我做错了什么?

<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);

我的设置对话框如下所示(正是我想要的):

Settings Dialog Fragment

我的自定义对话框片段如下所示:

Custom Dialog Fragment

如您所见,单选按钮选中的颜色为红色,您看不到未选中的单选按钮。

我认为您需要在实际的 Dialog 而不是 Fragment 上设置主题

使用此构造函数创建您的 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 为您想要显示的颜色。

看看这是否有帮助 - Android appcompat-v7:21.0.0 change material checkbox colors

简而言之,尝试设置 android:textColorSecondary

终于有答案了!!!

这是 AppCompat 22+ 的问题或错误。 Check out link here

显然这是一个片段错误,小部件没有在片段中获得 material 主题。他们似乎解决了这个问题,但这个问题仍然存在于基于我正在经历的对话片段中。 当您使用传递给 Fragment#onCreateView() 的 inflater 实例时,问题就来了。目前的解决方法是根据 google.

使用 getActivity().getLayoutInflater() 中的 LayoutInflater

所以我将代码更改为:

View view = getActivity().getLayoutInflater().inflate(R.layout.dialog, null);

来自:

View view = LayoutInflater.from(getActivity().getApplicationContext()).inflate(R.layout.dialoge, null);

我所有的小部件现在都有主题了。感谢大家。希望这对其他人有帮助。