gpt4 book ai didi

android - 无法更改 AlertDialog 按钮的背景颜色

转载 作者:搜寻专家 更新时间:2023-11-01 08:17:38 26 4
gpt4 key购买 nike

我正在尝试使用可能在 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))
}

这是在 styles.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>

关于如何解决这个问题有什么想法吗?

最佳答案

如果您使用的是 AppCompat 主题,您可以使用如下内容:

<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<!-- Used for the buttons -->
<item name="colorAccent">#FFCC00</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">#FFFFFF</item>
<!-- Used for the background -->
<item name="android:background">#5fa3d0</item>
</style>

enter image description here

有了新的Material components for android library你可以使用新的 androidx.appcompat.app.AlertDialog .

只需使用类似的东西:

new MaterialAlertDialogBuilder(context)
.setTitle("Dialog")
.setMessage("Lorem ipsum dolor ....")
.setPositiveButton("Ok", /* listener = */ null)
.setNegativeButton("Cancel", /* listener = */ null)
.show();

您可以使用以下内容自定义您的主题:

  <!-- Alert Dialog -->
<style name="MyThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
<item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
<item name="buttonBarNegativeButtonStyle">@style/Widget.MaterialComponents.Button.TextButton.Dialog</item>
</style>

和:

  <style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
<item name="android:textColor">#FFFFFF</item>
<item name="backgroundTint">@color/selector_bt</item>
</style>

enter image description here

关于android - 无法更改 AlertDialog 按钮的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57687214/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com