Android Dialog的颜色分析

本博客主要是讲dialog的默认样式换颜色,自定义样式在之前的博客

首先alertDialog有默认的几种样式

AlertDialog.THEME_TRADITIONAL

AlertDialog.THEME_HOLO_DARK

AlertDialog.THEME_HOLO_LIGHT

AlertDialog.THEME_DEVICE_DEFAULT_DARK

AlertDialog.THEME_DEVICE_DEFAULT_LIGHT

/**
     * Special theme constant for {@link #AlertDialog(Context, int)}: use
     * the traditional (pre-Holo) alert dialog theme.
     *
     * @deprecated Use {@link android.R.style#Theme_Material_Dialog_Alert}.
     */
    @Deprecated
    public static final int THEME_TRADITIONAL = 1;

    /**
     * Special theme constant for {@link #AlertDialog(Context, int)}: use
     * the holographic alert theme with a dark background.
     *
     * @deprecated Use {@link android.R.style#Theme_Material_Dialog_Alert}.
     */
    @Deprecated
    public static final int THEME_HOLO_DARK = 2;

    /**
     * Special theme constant for {@link #AlertDialog(Context, int)}: use
     * the holographic alert theme with a light background.
     *
     * @deprecated Use {@link android.R.style#Theme_Material_Light_Dialog_Alert}.
     */
    @Deprecated
    public static final int THEME_HOLO_LIGHT = 3;

    /**
     * Special theme constant for {@link #AlertDialog(Context, int)}: use
     * the device's default alert theme with a dark background.
     *
     * @deprecated Use {@link android.R.style#Theme_DeviceDefault_Dialog_Alert}.
     */
    @Deprecated
    public static final int THEME_DEVICE_DEFAULT_DARK = 4;

    /**
     * Special theme constant for {@link #AlertDialog(Context, int)}: use
     * the device's default alert theme with a light background.
     *
     * @deprecated Use {@link android.R.style#Theme_DeviceDefault_Light_Dialog_Alert}.
     */
    @Deprecated
    public static final int THEME_DEVICE_DEFAULT_LIGHT = 5;

 这是一个需求的样式,采用的是默认样式THEME_DEVICE_DEFAULT_DARK,进行修改。

代码如下

 /**
     * 网络更新提示框
     *
     * @param str
     */
    public void showDialogInternet(String str) {
        m_progressDlg = new ProgressDialog(this,R.style.AppCompatAlertDialogStyle);
        m_progressDlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确
        m_progressDlg.setIndeterminate(false);

        dialogs = new AlertDialog.Builder(MainActivity.this,R.style.AppCompatAlertDialogStyle).setTitle("软件更新").setMessage(str)

                // 设置内容
                .setPositiveButton("更新",// 设置确定按钮
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                m_progressDlg.setTitle("正在下载");
                                m_progressDlg.setMessage("请稍候...");
                                

                            }
                        })

                .setNegativeButton("暂不更新",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int whichButton) {


                                dialogs.dismiss();


                            }
                        }).create();// 创建

        // 显示对话框
        dialogs.show();
        Window window = dialogs.getWindow();


        m_progressDlg.setCanceledOnTouchOutside(false);//防止点击dialog外面触发dialog隐藏
        dialogs.setCanceledOnTouchOutside(false);//防止点击dialog外面触发dialog隐藏
     
    }

其中包含下图的一个进度条,有两个样式,根据style的设置相关颜色,如果不设置颜色会有默认颜色,下图第二个就是没设置颜色用的默认颜色。对于不同版本默认颜色也不一样,最好是自己指定颜色

style文件添加

    <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">@color/blue</item>
        <item name="android:textColorPrimary">@color/white</item>
        <item name="android:textColorSecondary">@color/white</item>
        <item name="android:textColorTertiary">@color/white</item>
        <item name="android:background">@color/black_3</item>
    </style>

可以看到样式是继承Theme.AppCompat.Light.Dialog.Alert来的。

这个时候可以自定义自己的颜色

对于哪个name对于dialog的哪个部位的颜色,可以自己尝试修改颜色确认。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Y_Hungry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值