【发布时间】:2020-01-01 08:59:59
【问题描述】:
我正在尝试使用 stackoverflow 中为此问题定义的所有解决方案来更改 AlertDialog 的颜色,但它仍然无法正常工作。
这是我用来实现这一目标的代码:
val vacationDialog = AlertDialog.Builder(fragment.context,R.style.DialogTheme)
val factory = LayoutInflater.from(OtrosDetailFragment.fragment.context);
var view = factory.inflate(R.layout.sample, null)
[...]
vacationDialog.setView(view)
val window = vacationDialog.create()
vacationDialog.setPositiveButton("Cerrar"){dialog, which ->
dialog.dismiss()
}
/**Listener called when the AlertDialog is shown**/
vacationDialog.show()
window.setOnShowListener {
/**Get the positive button from the AlertDialog**/
val positiveButton = window.getButton(DialogInterface.BUTTON_POSITIVE)
/**Set your color to the positive button**/
positiveButton.setBackgroundColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
positiveButton.setTextColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
positiveButton.setHintTextColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
positiveButton.setLinkTextColor(ContextCompat.getColor(fragment.context!!, R.color.colorPrimary))
}
这是 style.xml 中定义的样式 DialogTheme,顺便说一下,我找不到更改默认按钮颜色(黑色)的方法,并且似乎覆盖了从代码尝试的任何更改。
<style name="DialogTheme" parent="android:Theme.Holo">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<!-- No backgrounds, titles or window float -->
<item name="android:windowBackground">@color/white</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:gravity">center</item>
</style>
对如何解决这个问题有任何想法吗?
【问题讨论】:
标签: android material-design android-alertdialog android-appcompat material-components-android