Lollipop v21, FragmentDialog doesn't take my activity theme

限于喜欢 提交于 2019-12-25 04:48:08

问题


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>

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

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

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)

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.

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

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


回答1:


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>

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

Note: It has to be in onCreate as mentioned in https://stackoverflow.com/a/26582301/3286489

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



来源:https://stackoverflow.com/questions/38681568/lollipop-v21-fragmentdialog-doesnt-take-my-activity-theme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!