java - AlertDialog按钮区域背景颜色Android

标签 java android android-studio

如何更改警报对话框中按钮区域的背景颜色。 我已经完成了标题背景,但找不到更改颜色或向按钮区域添加分隔线的解决方案。

这就是现在的样子。

enter image description here

和代码

    new AlertDialog.Builder(MainActivity.this,R.style.MyDialogTheme)
            .setCustomTitle(custom_dialog_header)
            .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int selectedIndex) {
                    selectedItem = selectedIndex;
                }
            })
            .setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int linkID) {
                    String[] files = links.split(",");
                }
            })
            .setNegativeButton("Cancel", null)
            .show();

这是风格

<style name="MyDialogTheme" parent="Theme.AppCompat.DayNight.Dialog.Alert">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
    <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>

    <item name="android:background">#282828</item>
    <item name="android:colorPrimary">#ffffff</item>
    <item name="android:colorAccent">@color/colorSwipe</item>
    <item name="colorAccent">@color/colorSwipe</item>
    <item name="android:fontFamily">@font/roboto</item>
    <item name="android:textSize">16dp</item>
</style>

最佳答案

您需要在构建器对象上调用 create() 而不是 show() 来创建 AlertDialog 对象。 然后,您可以使用 setOnShowListener 更改对话框的任何部分,例如按钮区域。该监听器在创建对话框后调用,因此我们使用它来避免空指针异常。

final AlertDialog dialog = new AlertDialog.Builder(MainActivity.this,R.style.MyDialogTheme)
        .setCustomTitle(custom_dialog_header)
        .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int selectedIndex) {
                int selectedItem = selectedIndex;
            }
        })
        .setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int linkID) {
                String[] files = links.split(",");
            }
        })
        .setNegativeButton("Cancel", null)
        .create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface d) {
        // to change background of positive button
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setBackgroundColor(getResources().getColor(R.color.some_color));
        // to change background of button area
        ButtonBarLayout b = (ButtonBarLayout)(dialog.getButton(AlertDialog.BUTTON_POSITIVE).getParent());
        b.setBackgroundColor(getResources().getColor(R.color.some_color));
    }
});
dialog.show();

关于java - AlertDialog按钮区域背景颜色Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60419886/

相关文章:

java - JMS回滚有多可靠?

java - 没有可用于具有名称的 bean 属性的 getter 方法

java - 我如何使用 GreenDao LazyList 正确处理大量数据 + 排序和过滤

安卓工作室 : how to setup generated Cloud Endpoints sources?

Java Beans Introspector 需要桌面模块

java - PostgreSQL 和 hibernate 按版本号排序 1.1.1.1.1

java - 如何在 Kotlin 中创建类的实例并重写其函数?

java - Android : surfaceView cannot getHolder() in onCreate(), 必须在 onResume() 中

android - 在 android studio 中添加库时出现问题 [未找到名称为 'default' 的配置。]

android - 使括号自动开始新行