【问题标题】:Styling custom dialog fragment not working样式化自定义对话框片段不起作用
【发布时间】:2015-12-23 09:27:34
【问题描述】:

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

<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

如您所见,单选按钮选择了红色,而您看不到未选择的单选按钮。

【问题讨论】:

  • 除了单选按钮的强调色外,您提供的两个屏幕截图看起来都相似。您究竟想在自定义对话框中更改什么?
  • 我希望单选按钮的颜色匹配,而且我的其他带有时间选择器、日期选择器和微调器的对话框片段由于某种原因与背景融为一体,因此用户将无法见他们。
  • 检查此网址link 红色框是微调器、日期选择器和时间选择器所在的位置

标签: android styling dialogfragment


【解决方案1】:

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

使用这个构造函数来创建你的 AlertDialog:

AlertDialog.Builder(Context context, int theme)

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), theme)

【讨论】:

  • 弗洛里安,这正是我在我的帖子 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppTheme_DialogStyle);
  • 我在尝试使用 ratingBar 制作自定义对话框时遇到了同样的问题。 (如果我记得对话框下有一些特别的东西,也许你必须自定义 ratingBar,我会检查一下)
  • 非常感谢。告诉我。
【解决方案2】:

我认为您需要在对话框样式中再添加一项。 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 以显示您想要显示的颜色。

【讨论】:

  • 尝试将其添加到AppTheme 并检查。
  • 不好。似乎我的 apptheme 中的任何内容都不适用于我的自定义对话框片段中的任何小部件
【解决方案3】:

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

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

【讨论】:

  • 我确实尝试过。我把它放在我的 dialogtheme 和 apptheme 中,但没有用。似乎我的 appTheme 不适用于我的自定义对话框片段中的任何小部件
  • android:textColorSecondaryInverse 怎么样?
  • 不幸的是:(
  • 以防万一,您使用的是appcompat 22.1或更高版本,并从android.support.v7.app.AlertDialog导入?
  • com.android.support:appcompat-v7:23.0.1 是的,我正在从 v7 导入
【解决方案4】:

终于有答案了!!!

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

显然,这是一个片段错误,小部件没有在片段中获取主题材料。他们似乎解决了这个问题,但根据我正在经历的事情,这个问题仍然存在于一个对话片段中。 当您使用传递给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);

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

【讨论】:

  • 呃...不敢相信它仍然没有在 23.4.0 支持库中修复:-(
  • 我将打印出这个问题和答案,并用它来装饰我的房间。谢谢你回来!我很高兴我在一小时后找到了这个,而不是三天......
  • 问题仍未解决,您指定的解决方法有效。虽然我建议直接使用 Activity 中的充气机,而不是使用应用程序智能充气机。
猜你喜欢
  • 2019-12-27
  • 1970-01-01
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多