Adh*_*ier 2 android android-theme android-alertdialog android-ktx material-components-android
我已按如下方式设置我的应用程序:
构建.gradle
implementation 'com.google.android.material:material:1.1.0'
样式文件
<style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
<item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
<item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
</style>
<style name="NegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:color">@color/colorPrimary</item>
</style>
<style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:color">@color/colorPrimary</item>
</style>
Run Code Online (Sandbox Code Playgroud)
CheckoutFragment.kt
private fun createConfirmOrderDialog() {
val builder = AlertDialog.Builder(requireContext(), R.style.AlertDialogTheme)
builder.setTitle(getString(R.string.confirm_order))
.setMessage(dialogPrompt)
.setPositiveButton(R.string.confirm) { dialog, _ ->
viewModel.placeOrder()
dialog.dismiss()
}
.setNegativeButton(R.string.cancel) { dialog, _ ->
dialog.dismiss()
}
builder.show()
}
Run Code Online (Sandbox Code Playgroud)
颜色文件
<color name="colorAccent">#F1CB1A</color> // I have this added to the base theme
但是,此设置显示了一个对话框,其中按钮文本不可见,因为文本和背景都是白色的。
我怎样才能解决这个问题?
使用MaterialAlertDialogBuilder代替AlertDialog.Builder:
MaterialAlertDialogBuilder(context)
.setTitle("Title")
.setMessage(dialogPrompt)
.setPositiveButton("OK",listener)
.show()
Run Code Online (Sandbox Code Playgroud)
按钮的默认颜色基于colorPrimary颜色。
如果要使用自定义颜色,可以使用:
MaterialAlertDialogBuilder(context,R.style.AlertDialogTheme)
Run Code Online (Sandbox Code Playgroud)
用这种风格
<style name="AlertDialogTheme" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
<item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
</style>
<style name="NegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">@color/.....</item>
</style>
<style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">@color/....</item>
</style>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
542 次 |
| 最近记录: |