棒棒糖v21,FragmentDialog不采用我的活动主题 [英] Lollipop v21, FragmentDialog doesn't take my activity theme

查看:101
本文介绍了棒棒糖v21,FragmentDialog不采用我的活动主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动主题设置如下.

I have my activity theme as below set.

<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:colorButtonNormal">@color/colorPrimary</item>
</style>

该活动具有一个简单的FragmentDialog,该片段使用(科特林代码)开始

The activity have a simple FragmentDialog that is started using (Kotlin code)

    MyDialogFragment().show(supportFragmentManager, MyDialogFragment.TAG)

MyDialogFragment确实有一个按钮.因此,根据主题,我希望Button的颜色为colorPrimary.但是按钮(在v21上)的颜色仅为灰色)

The MyDialogFragment does have a Button. Hence I expect the color of the Button is colorPrimary as per the theme. However the color of the Button (on v21) is only grey)

这适用于棉花糖(即v23),而不适用于棒棒糖(v21).我没有尝试过v22. ...所以我想v21不会自动从活动中继承主题.

This works on Marshmallow (i.e. v23) and not Lollipop (v21). I haven't tried v22. ... So I guess the v21 doesn't automatically inherit the theme from the activity.

对于KitKat及以下版本,这并不适用,因为它不使用'android:colorButtonNormal'

For KitKat and below, this doesn't apply, as it doesn't use 'android:colorButtonNormal'

我应该如何让FragmentDialog获取我在活动中设置的主题?

How should I get my FragmentDialog get the theme that I set on my activity?

推荐答案

我找到了一种方法,它是在定义我的活动主题的基础上,如下显式定义我的FragmentDialog主题.

I found a way to do it, which is to explicitly define my FragmentDialog theme as below, on top of defining my activity theme.

<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:colorButtonNormal">@color/colorPrimary</item>
</style>

<style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:colorButtonNormal">@color/colorPrimary</item>
</style>

然后我需要从FragmentDialog onCreate()显式设置它.

Then I need to explicitly set it from my FragmentDialog onCreate().

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialogTheme)
}

注意:它必须位于 https://stackoverflow.com/a/26582301/3286489

如果有任何答案,我仍然愿意给出更优雅的答案.

I'm still open to more elegant answer if there's any out there.

这篇关于棒棒糖v21,FragmentDialog不采用我的活动主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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