AlertDialogs のボタン スタイル
-
22-09-2019 - |
質問
AlertDialog ボタンのデフォルトのスタイルをオーバーライドする方法を知っている人はいますか?Androidのソースを調べてみました テーマ そして スタイル さまざまなことを試しましたが、うまくいく方法を見つけることができませんでした。
以下にあるものは背景を変更するために機能しますが、ボタンには何も機能しません。 myTheme
全体に適用される <application>
マニフェスト経由で。(わかりやすくするために他のいくつかの項目は削除されていますが、それらはタイトル バーにのみ関係します。)
<style name="myTheme" parent="android:Theme">
<item name="android:buttonStyle">@style/customButtonStyle</item>
<item name="android:alertDialogStyle">@style/dialogAlertTheme</item>
</style>
<style name="dialogAlertTheme" parent="@android:style/Theme.Dialog.Alert">
<item name="android:fullDark">@drawable/dialog_loading_background</item>
<item name="android:topDark">@drawable/dialog_alert_top</item>
<item name="android:centerDark">@drawable/dialog_alert_center</item>
<item name="android:bottomDark">@drawable/dialog_alert_bottom</item>
<!-- this last line makes no difference to the buttons -->
<item name="android:buttonStyle">@style/customButtonStyle</item>
</style>
何か案は?
解決
以下の他のすべての回答を参照してください。
正しくない:
自分で実装する必要があると思います Dialog
クラス。
他のヒント
MrSnowflake の推論が不正確であることを考えると、私は自由に別の答えを提供することにします。
そこに何かがある 間違っている とともに マークアップ Steve Haley の質問では、alertDialogStyle とalertDialogTheme が混同されています。これはおそらく、alertDialogTheme が、alertDialogStyle が登場してからずっと後に導入されたという事実が原因です。
したがって、Android プラットフォームで @android:style/Theme.Dialog.Alert が利用できる場合でも、Android プラットフォームがサポートしていない限り、カスタマイズされたバージョンを独自のテーマにフックしてその表現力を活用することはできません。 android:alertDialogテーマ テーマの属性/項目。(このような一貫性のない Android バージョンが存在するかどうかはわかりませんが、確かなことはわかりません。しかし、質問で使用されているマークアップは、そうであることを示唆しています。)
質問のマークアップでは、parent="@android:style/Theme.Dialog.Alert" 何もしません ただし、実際にはアラート ダイアログ スタイルをカスタマイズしているだけなのに、アラート ダイアログ テーマをカスタマイズしているかのような錯覚を引き起こす場合は除きます。
マークアップは次のようになります。すべての Android バージョンがすべての機能をサポートしているわけではありません。
<style name="myTheme" parent="android:Theme">
<item name="android:buttonStyle">@style/customButtonStyle</item>
<item name="android:alertDialogStyle">@style/dialogAlertStyle</item>
<item name="android:alertDialogTheme">@style/dialogAlertTheme</item>
</style>
<style name="dialogAlertStyle" parent="@android:style/AlertDialog">
<item name="android:fullDark">[...]</item>
[...]
</style>
<style name="dialogAlertTheme" parent="@android:style/Theme.Dialog.Alert">
<item name="android:windowBackground">[...]</item>
[...]
</style>
アラートダイアログスタイルのカスタマイズはかなり前から行われていますが、「fullDark」や「topDark」などの(背景)ドローアブルの提供に限定されています。
アラート ダイアログ テーマをカスタマイズすると、windowBackground、windowTitleStyle などの属性を提供するメソッドが開きますが、前述したように、テーマのalertDialogThem 属性/項目をサポートする Android バージョンが必要です。これがいつ導入されたのか正確にはわかりませんが、Android 2.2 ではなかったので、とにかく Eclipse が教えてくれます...
XML でアラート ダイアログ ボタンのスタイルを設定することは不可能であるという MrSnowflake の結論を検証するリソースは私にはありませんが、実際に機能が欠けているという Android のやや厄介な側面に直面しない限り、その可能性は低いと思います。
実際のところ、質問に欠けているのは、この点で最も関連のある部分です。
<style name="customButtonStyle" />
したがって、アラート ダイアログ ボタンが Widget.Button に従っていないという結論は、私の観点からはまだ証明されていません。
総合的な結論: Android では、他のウィジェットから独立してアラート ダイアログのスタイルを設定する機能は制限されていますが、新しいバージョンではこの点が改善され、さらに強力になっています。
これを試してみてください
<item name="buttonBarStyle">@style/Widget.AppCompat.ButtonBar</item>
<item name="buttonBarButtonStyle">@style/AppTheme.Button</item>
<item name="buttonBarPositiveButtonStyle">@style/YoursTheme</item>
<item name="buttonBarNegativeButtonStyle">@style/YoursTheme</item>
<item name="buttonBarNeutralButtonStyle">@style/YoursTheme</item>
あなたが代わりにこのような "のButtonStyle" の "buttonBarButtonStyle" を上書きすることができます:
<style name="dialogAlertTheme" parent="@android:style/Theme.Dialog.Alert">
<item name="android:buttonBarButtonStyle">@style/customButtonStyle</item>
</style>
私は、デフォルトボタンの色や背景を変更します。このような方法を作った。
public static void setDefaultColorTextForDialogButton(AlertDialog alertDialog, Resources r) {
Button b;
b= alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
if(b != null) {
b.setTextColor(r.getColor(R.color.default_text));
b.setBackgroundColor(Color.WHITE);
}
b = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
if(b != null) {
b.setTextColor(r.getColor(R.color.default_text));
b.setBackgroundColor(Color.WHITE);
}
b = alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL);
if(b != null) {
b.setTextColor(r.getColor(R.color.default_text));
b.setBackgroundColor(Color.WHITE);
}
}
これは一部の人のために役に立つかもしれません!!!
あなたがマテリアルのコンポーネントを使用している場合:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:alertDialogTheme">@style/AlertDialogTheme.Light</item>
</style>
<style name="AlertDialogTheme.Light" parent="ThemeOverlay.MaterialComponents.Dialog.Alert" >
<item name="android:buttonBarButtonStyle">@style/InsertCustomButtonStyleHere</item>
</style>
これはあなたのテーマの色を保つが、Widget.MaterialComponents.Button.TextButton
と一致するように、例えば、ボタンのスタイルを置き換えます。