Snackbarのaction部分の文字色をstyle.xmlで変えたい
MaterialComponentではSnackbarのAction部分のtextColorはcolorAccentが使われているが、SnackbarのAction部分のtextColorをcolorAccent以外の色に変更したいときのメモ。
Snackbarを表示時する際 setActionTextColor
を使うことで変更は可能だが、style.xml
に定義することでアプリ全体でデザインを変更したいときの方法を記す。
Snackbar.make(...).apply{ setActionTextColor(ContextCompat.getColor(this@HomeActivity, R.color.my_color)) setAction("action"){ ... } }
アプリのTheme AppTheme
から snackbarButtonStyle
を用いて変更する方法があるが、snackbarButtonStyle
の設定のみではAction部分のtextColorは変更されない。
android - How to customize the style of the action button in the Snackbar - Stack Overflow
<!-- Base application theme. --> <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"> <!-- Snackbar --> <item name="snackbarButtonStyle">@style/MaterialSnackbarTextButtonTheme</item> <item name="snackbarTextViewStyle">@style/MaterialSnackbarTextViewTheme</item> </style> <style name="MaterialSnackbarTextButtonTheme" parent="@style/Widget.MaterialComponents.Button.TextButton.Snackbar"> <item name="android:textColor">#ffffff</item> </style> <style name="MaterialSnackbarTextViewTheme" parent="@style/Widget.MaterialComponents.Snackbar.TextView"/>
上のように定義しAction部分のtextColorが反映されるのは同時に snackbarTextViewStyle
も同時に設定されていた場合のみだった。snackbarButtonStyleのみだと反映されなかったので注意が必要そうだ。
android - Style SnackBar in theme app - Stack Overflow
※ MaterialConponent 1.2で検証