2020年11月19日木曜日

MaterialAlertDialogBuilder のボタンの色を変更する

non-Bridge なテーマで MaterialAlertDialogBuilder を使うと、ダイアログのボタンの色は colorPrimary になります。

そのため colorPrimary に黒っぽい色を指定した DayNight テーマだと、Dark Mode のときにボタンの文字が見えないという状態になってしまいます。
  1. <resources>  
  2.   
  3.     <style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">  
  4.         <item name="colorPrimary">#212121</item>  
  5.     </style>  
  6.   
  7. </resources>  




DayNight テーマの colorPrimary は変えずに Dark Mode のときだけダイアログのボタンの色を変えるには、materialAlertDialogTheme 属性を指定します。

res/values/themes.xml
  1. <resources>  
  2.   
  3.     <style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">  
  4.         <item name="colorPrimary">#212121</item>  
  5.   
  6.         <item name="materialAlertDialogTheme">@style/ThemeOverlay.MyApp.MaterialAlertDialog</item>  
  7.     </style>  
  8.   
  9.     <style name="ThemeOverlay.MyApp.MaterialAlertDialog" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog" />  
  10.   
  11. </resources>  
res/values-night-v8/themes.xml
  1. <resources>  
  2.   
  3.     <style name="ThemeOverlay.MyApp.MaterialAlertDialog" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">  
  4.         <item name="colorPrimary">#ffffff</item>  
  5.     </style>  
  6.   
  7. </resources>  




0 件のコメント:

コメントを投稿