样式自定义对话框片段不起作用 [英] Styling custom dialog fragment not working

查看:119
本文介绍了样式自定义对话框片段不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置所有对话框片段的样式,使其在我的应用程序中看起来相同.来自我的设置片段的对话框的样式完全符合我想要的样式.对于我的自定义对话框片段,样式相似但不完全相同.由于某些原因,自定义对话框片段中的微调器,时间选择器,日期选择器,单选按钮和edittext小部件无法采用相同的样式.实际上,这些小部件与白色背景融合在一起,您看不到它们在那里.我在做什么错了?

I'm trying to style all my dialog fragments to look the same in my app. The dialogs coming from my settings fragment are styled exactly the way I want it. For my custom dialog fragments, the style is similar but not exactly the same. For some reason the spinner, timepicker, datepicker, radiobuttons, and edittext widgets inside my custom dialog fragments don't pick up the same style. In fact, the widgets blend in with the white background and you can't see that they are there. What am I doing wrong?

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

我正在将主题应用于我的自定义对话框片段,如下所示:

I'm applying the theme to my custom dialog fragment like this:

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppTheme_DialogStyle);

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

My settings dialog looks like this (Exactly how I want it):

设置"对话框片段

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

My custom dialog fragment looks like this:

自定义对话框片段

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

As you can see, the radio button selected color red and you can't see the unselected radio button.

推荐答案

最后得到了答案!

这是AppCompat 22+的问题或错误.在此处签出链接

It's an issue or bug with AppCompat 22+. Check out link here

显然,这是一个带有片段的错误,并且小部件没有将素材作为片段.看来他们已经解决了这个问题,但是该问题仍然存在于基于我正在经历的对话框片段中.当您使用传递给 Fragment#onCreateView()的充气机实例时,就会出现问题.现在的解决方法是改为根据Google使用 getActivity().getLayoutInflater()中的 LayoutInflater .

Apparently this was a bug with fragments and widgets weren't getting the material themed in a fragment. It seems they fixed this issue, but the issue still holds in a dialog fragment based on what I'm going through. The problem comes when you use the inflater instance passed to Fragment#onCreateView(). The workaround for now is to instead used the LayoutInflater from getActivity().getLayoutInflater() according to google.

所以我将代码更改为:

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

来自:

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

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

All my widgets are now themed. Thanks everyone. Hopes this helps someone else.

这篇关于样式自定义对话框片段不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆