web-dev-qa-db-ja.com

ListPreferenceポップアップダイアログのスタイルを変更するにはどうすればよいですか?

これで見たように、ListPreferenceのポップアップダイアログのスタイルを変更しようとしています answer 。たとえば、ダイアログに別の背景色が必要です。

これまでのところ、カスタムスタイルを次のように適用しようとしました。

<item name="Android:dialogTheme">@style/AlertDialogStyle</item>
<item name="Android:alertDialogTheme">@style/AlertDialogStyle</item>
<item name="Android:alertDialogStyle">@style/AlertDialogStyle</item>
<item name="Android:dialogPreferenceStyle">@style/AlertDialogStyle</item>


<style name="AlertDialogStyle" parent="AlertDialog.AppCompat">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="Android:textColor">@color/lightGrey</item>
    <item name="Android:background">@color/cardBackground</item>
    <item name="Android:popupBackground">@color/cardBackground</item>
    <item name="Android:windowBackground">@color/cardBackground</item>
    <item name="Android:itemBackground">@color/cardBackground</item>
</style>

しかし、私のスタイルはまだ適用されていません/背景色は変更されていません。

これが私のListPreferenceのポップアップダイアログが現在どのように見えるかです:

enter image description here

そして、これは私がアーカイブしたいカラーテーマです(基本的に私が他のダイアログに使用するのと同じテーマ):

enter image description here


私の問題をすばやく再現するには->私のプロジェクトは github

私自身の質問に答えます。結局、それは交換するのと同じくらい簡単でした:

<item name="Android:alertDialogTheme">@style/AlertDialogStyle</item>

<item name="alertDialogTheme">@style/AlertDialogStyle</item>

マークアップの内容を混ぜ合わせていると思います。 alertDialogStyleとalertDialogThemeはどちらも異なります。

アラートダイアログテーマをカスタマイズする必要があります。ダイアログテーマは、おそらく@ Android:style /Theme.Dialog.Alertを拡張する必要があります。

<item name="Android:dialogTheme">@style/dialogAlertTheme</item>
<item name="Android:alertDialogTheme">@style/dialogAlertTheme</item>
<item name="Android:alertDialogStyle">@style/AlertDialogStyle</item>
<item name="Android:dialogPreferenceStyle">@style/AlertDialogStyle</item>

<style name="dialogAlertTheme" parent="@Android:style/Theme.Dialog.Alert">
    <item name="Android:windowBackground">[...]</item>
    [...]
</style>

<style name="AlertDialogStyle" parent="AlertDialog.AppCompat">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="Android:textColor">@color/lightGrey</item>
    <item name="Android:background">@color/cardBackground</item>
    <item name="Android:popupBackground">@color/cardBackground</item>
    <item name="Android:windowBackground">@color/cardBackground</item>
    <item name="Android:itemBackground">@color/cardBackground</item>
</style>

注-1。アラートダイアログスタイルのカスタマイズは、(背景)ドローアブルの提供に限定されています。

注-2。アラートダイアログテーマをカスタマイズすると、windowBackground、windowTitleStyleなどの属性を提供するメソッドが開きますが、Androidバージョン

回答の説明:Android:からAndroid:alertDialogThemeを削除した場合に機能する理由

<item name="alertDialogTheme">@style/AlertDialogStyle</item>

これが、AlertDialogスタイルをオーバーライドする標準的な方法です。 AlertDialogはメインテーマのアクセントカラーを使用しないため、ライブラリの一部でしたが、 v24.2.0. 以降、Android =チームはこの動作を修正しました。

問題の参照:https://github.com/Gericop/Android-Support-Preference-V7-Fix/issues/52#issuecomment -25575929

参照の変更:https://github.com/Gericop/Android-Support-Preference-V7-Fix/commit/a6082cb0a508f5e0305a626c9a2a841e943ef8f6#diff -483bbb12192b1b74adadc9b4076b203b

1
NullPointer