如何更改主题AlertDialog [英] How to change theme for AlertDialog

查看:203
本文介绍了如何更改主题AlertDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果有人可以帮助我。我想创建一个自定义AlertDialog。为了做到这一点,我添加了code以下行styles.xml

I was wondering if someone could help me out. I am trying to create a custom AlertDialog. In order to do this, I added the following line of code in styles.xml

    @绘制/ color_panel_background  

@drawable/color_panel_background

  • color_panel_background.9.png位于绘制文件夹中。这也是在Android SDK中资源文件夹中。

以下是主要活动。

package com.customdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;

public class CustomDialog extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        this.setTheme(R.style.CustomAlertDialog);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("HELLO!");
        builder .setCancelable(false)
          .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               //MyActivity.this.finish();
           }
       })
       .setNegativeButton("No", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               //dialog.cancel();
           }
       });

        AlertDialog alertdialog = builder.create();
        alertdialog.show();
    }
}

在以主题应用到AlertDialog,我必须设置主题,以目前的情况下。

In order to apply the theme to an AlertDialog, I had to set the theme to the current context.

不过,我刚才似乎无法让应用程序显示自定义的AlertDialog。谁能帮我出这一点,非常感谢你提前!

However, I just can't seem to get the app to show customized AlertDialog. Can anyone help me out with this, and thank you very much in advance!

推荐答案

在Dialog.java(安卓SRC)一个ContextThemeWrapper使用。所以,你可以复制的想法,并做一些事情,如:

In Dialog.java (Android src) a ContextThemeWrapper is used. So you could copy the idea and do something like:

AlertDialog.Builder建设者=新AlertDialog.Builder(新ContextThemeWrapper(这一点,R.style.AlertDialogCustom));

和风格,然后像你想要的:

And then style it like you want:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AlertDialogCustom" parent="@android:style/Theme.Dialog">
        <item name="android:textColor">#00FF00</item>
        <item name="android:typeface">monospace</item>
        <item name="android:textSize">10sp</item>
    </style>
</resources>

这篇关于如何更改主题AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆