Android的 - AlertDialog造型 [英] Android - AlertDialog styling

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

问题描述

我在应用程序的警告对话框,如下图所示。

我想的标题和分隔标题行 - 邮件正文将在橙色。我该怎么办呢? 我尝试过用是如下图所示的自定义样式。但这并没有工作。

 <样式名称=AboutDialog父=@安卓风格/ Theme.Dialog>
  <项目名称=机器人:文字颜色>#E5492A< /项目>
< /风格>
 

我的提醒对话框code:

  AlertDialog.Builder alertDialog =新AlertDialog.Builder(新ContextThemeWrapper(MainActivity.context,R.style.AboutDialog));
alertDialog.setTitle(样本);
        alertDialog.setMessage(R.string.rate_dialog_text);
        alertDialog.setPositiveButton(R.string.rate_now_text,
                新DialogInterface.OnClickListener(){
                    公共无效的onClick(DialogInterface对话,诠释它){
                        MainActivity.context.startActivity(新意图(
                                Intent.ACTION_VIEW,乌里
                                        .parse(市场://细节ID =
                                                + MainActivity.APP_PNAME)));
                        如果(编辑!= NULL){
                            editor.putBoolean(dontshowagain,真正的);
                            editor.commit();
                        }
                        dialog.dismiss();

                    }
                });

        alertDialog.setNeutralButton(R.string.remind_later_text,
                新DialogInterface.OnClickListener(){
                    公共无效的onClick(DialogInterface对话,诠释它){
                        dialog.dismiss();
                    }
                });

        alertDialog.setNegativeButton(R.string.no_thanks_text,
                新DialogInterface.OnClickListener(){
                    公共无效的onClick(DialogInterface对话,诠释它){
                        如果(编辑!= NULL){
                            editor.putBoolean(dontshowagain,真正的);
                            editor.commit();
                        }
                        dialog.dismiss();
                    }
                });

        返回alertDialog.create();

    }
 

解决方案

相反的:

  AlertDialog.Builder alertDialog =新AlertDialog.Builder(
    新ContextThemeWrapper(MainActivity.context,R.style.AboutDialog));
 

试试这个:

  AlertDialog.Builder alertDialog =新AlertDialog.Builder(这一点,R.style.AboutDialog);
 

请注意:这是仅适用于API 11(的Andr​​oid 3.0)及以上

如果您需要支持Android< 3.0你可能有创建,而不是使用自定义对话框( AlertDialog

修改新增真的低俗基于反射的黑客

如果你真的卡住,不希望实现一个自定义对话框,然后尝试以下操作。

您已经建立的对话,只是你想要的,而不是这回吧,以前后:

 返回alertDialog.create();
 

做到这一点:

  AlertDialog广告= alertDialog.create(); //创建对话框
//添加侦听器,以便我们可以通过修改对话框中,它显示前
ad.setOnShowListener(新DialogInterface.OnShowListener(){
    @覆盖
    公共无效OnShow中(DialogInterface dialogInterface){
        //设置文本颜色在对话框的标题和分隔
        setTextColor(dialogInterface,0xFFE5492A);
    }
});
返回的广告;
 

现在加入这个非常讨厌的基于反射的方法:

 公共无效setTextColor(DialogInterface警报,INT的颜色){
    尝试 {
        类C = alert.getClass();
        现场mAlert = c.getDeclaredField(mAlert);
        mAlert.setAccessible(真正的);
        对象alertController = mAlert.get(警告);
        C = alertController.getClass();
        现场mTitleView = c.getDeclaredField(mTitleView);
        mTitleView.setAccessible(真正的);
        对象dialogTitle = mTitleView.get(alertController);
        TextView的dialogTitleView =(TextView中)dialogTitle;
        //在标题设置文本颜色
        dialogTitleView.setTextColor(颜色);
        //要找到水平分割,第一
        //得到各地标题容器
        ViewGroup中父=(ViewGroup中)dialogTitleView.getParent();
        //然后绕过该容器中的容器
        父=(ViewGroup中)parent.getParent();
        的for(int i = 0; I< parent.getChildCount();我++){
            视图v = parent.getChildAt(ⅰ);
            如果(V的instanceof ImageView的){
                //我们得到了一个ImageView的,这应该是分隔符
                ImageView的IM =(ImageView的)V;
                //设置的滤色器图像上
                im.setColorFilter(颜色);
            }
        }
    }赶上(例外五){
        //忽略任何异常,无论是它的工作原理或不
    }
}
 

我测试了在Android 2.2和Android 4.0和它的作品。它可能不是做你想要什么,所以你需要去尝试。我不能保证它会在所有的设备或在Android未来的版本,因为它取决于很多在路上的 AlertDialog 类实现(如果他们改变,在未来这可能将不再工作)。

只是几个音符的人谁在乎:

  1. 这是我使用的原因 setOnShowListener()是因为你不能真正得到内部查看对象的 AlertDialog 使用,直到他们被夸大后。他们没有立即得到,当你创建一个 AlertDialog ,它发生一段时间后膨胀。使用监听器,我们可以得到控制的布局是在膨胀之后但在对话框表示。

  2. 我使用的是反射在 AlertDialog 实施内部成员变量的访问。一旦我得到访问的TextView 包含标题,我必须猴子四处找水平线用于标题从消息中分离出来。要做到这一点,我得到了布局周围的标题(这布局包含警告图标和标题文本)。然后我得到了布局包围了(这个布局包图标,标题文本和分隔)。然后,我看着周围布局的所有儿童,并设置在那里的所有的ImageView 物体的颜色。该分离器是使用可绘制实现为​​的ImageView ,因此它不可能只是改变颜色。注:另一种使用 setColorFilter()将替换在这里适当的彩色绘制的ImageView的提拉

感谢您的挑战,这是有点好玩摸不着头脑 - D

I have an alert dialog in the app as shown below.

I want the title and the line which separates the title - message body to be in orange colour. how can i do this? What i tried is using custom style as shown below. But this did not work.

<style name="AboutDialog" parent="@android:style/Theme.Dialog">
  <item name="android:textColor">#E5492A</item>
</style>

my alert dialog code:

AlertDialog.Builder alertDialog = new AlertDialog.Builder( new ContextThemeWrapper(MainActivity.context, R.style.AboutDialog));
alertDialog.setTitle("Sample");
        alertDialog.setMessage(R.string.rate_dialog_text);
        alertDialog.setPositiveButton(R.string.rate_now_text,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        MainActivity.context.startActivity(new Intent(
                                Intent.ACTION_VIEW, Uri
                                        .parse("market://details?id="
                                                + MainActivity.APP_PNAME)));
                        if (editor != null) {
                            editor.putBoolean("dontshowagain", true);
                            editor.commit();
                        }
                        dialog.dismiss();

                    }
                });

        alertDialog.setNeutralButton(R.string.remind_later_text,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

        alertDialog.setNegativeButton(R.string.no_thanks_text,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        if (editor != null) {
                            editor.putBoolean("dontshowagain", true);
                            editor.commit();
                        }
                        dialog.dismiss();
                    }
                });

        return alertDialog.create();

    }

解决方案

Instead of:

AlertDialog.Builder alertDialog = new AlertDialog.Builder(
    new ContextThemeWrapper(MainActivity.context, R.style.AboutDialog));

Try this:

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this, R.style.AboutDialog);

NOTE: This is only available for API 11 (Android 3.0) and above.

If you need to to support Android < 3.0 you probably have to create a custom dialog (instead of using AlertDialog

EDIT Added really sleazy reflection-based hack

If you are really stuck and don't want to implement a custom dialog, then try the following.

After you've built the dialog and just before you want to return it, instead of this:

return alertDialog.create();

do this:

AlertDialog ad = alertDialog.create(); // Create the dialog
// Add listener so we can modify the dialog before it is shown
ad.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface dialogInterface) {
        // Set the text color on the dialog title and separator
        setTextColor(dialogInterface, 0xFFE5492A);
    }
});
return ad;

Now add this very nasty reflection-based method:

public void setTextColor(DialogInterface alert, int color) {
    try {
        Class c = alert.getClass();
        Field mAlert = c.getDeclaredField("mAlert");
        mAlert.setAccessible(true);
        Object alertController = mAlert.get(alert);
        c = alertController.getClass();
        Field mTitleView = c.getDeclaredField("mTitleView");
        mTitleView.setAccessible(true);
        Object dialogTitle = mTitleView.get(alertController);
        TextView dialogTitleView = (TextView)dialogTitle;
        // Set text color on the title
        dialogTitleView.setTextColor(color);
        // To find the horizontal divider, first
        //  get container around the Title
        ViewGroup parent = (ViewGroup)dialogTitleView.getParent();
        // Then get the container around that container
        parent = (ViewGroup)parent.getParent();
        for (int i = 0; i < parent.getChildCount(); i++) {
            View v = parent.getChildAt(i);
            if (v instanceof ImageView) {
                // We got an ImageView, that should be the separator
                ImageView im = (ImageView)v;
                // Set a color filter on the image
                im.setColorFilter(color);
            }
        }
    } catch (Exception e) {
        // Ignore any exceptions, either it works or it doesn't
    }
}

I tested this on Android 2.2 and Android 4.0 and it works. It may not do exactly what you want, so you'll need to try it. I can't guarantee it will work on all devices or on future versions of Android, since it depends a lot on the way the AlertDialog class is implemented (if they change that in the future this probably won't work anymore).

Just a few notes for anyone who cares:

  1. The reason that I use setOnShowListener() is because you can't actually get to the internal View objects that AlertDialog uses until after they have been inflated. They don't get inflated immediately when you create an AlertDialog, it happens some time later. Using the listener we can get control after the layout is inflated but before the Dialog is shown.

  2. I'm using reflection to access in internal member variables in the AlertDialog implementation. Once I get access to the TextView that contains the title, I have to monkey around to find the horizontal line that is used to separate the title from the message. To do this I get the Layout that surrounds the title (this Layout contains the alert icon and title text). Then I get the Layout that surrounds that (this Layout wraps the icon, title text and the separator). Then I look at all children of the surrounding layout and set the color on all ImageView objects in there. The separator is implemented as an ImageView using a drawable, therefore it isn't possible to just change the color. Note: An alternative to using setColorFilter() would be to replace the drawable in the ImageView with a suitably colored drawable here.

Thanks for the challenge, it was kinda fun to figure this out :-D

这篇关于Android的 - AlertDialog造型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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