【问题标题】:How to apply color in text message in alert dialog box in Android?如何在 Android 的警报对话框中的文本消息中应用颜色?
【发布时间】:2011-05-23 21:50:31
【问题描述】:

我正在开发安卓应用。我正在使用警报对话框。怎样才能提醒消息颜色?

 AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
              builder.setMessage("...........congratulations...........");
                   builder.setCancelable(false)
                .setPositiveButton("Move Next Level",
                        new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) 
                                    {


                                    }
                                });

                AlertDialog alert = builder.create();
               alert.setTitle(".");
               alert.setIcon(R.drawable.newmovw);      
                alert.show();   

显示消息,文本如何在 Android 中应用颜色。

【问题讨论】:

  • 您想要整个文本使用相同的颜色还是只是其中的一部分?
  • 仅“...........恭喜............”此文本适用于绿色

标签: android text colors android-alertdialog


【解决方案1】:

您可以创建警报对话框内容的自定义视图。 APIDemos 本身的代码中对此进行了清楚的解释。检查其中的case DIALOG_TEXT_ENTRY。它后面的类是LayoutInflater。请也检查一下。

例如:

LayoutInflater factory = LayoutInflater.from(this);
            final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);// this layout can created by yourself whatever you want.
            return new AlertDialog.Builder(AlertDialogSamples.this)
                .setIcon(R.drawable.alert_dialog_icon)
                .setTitle(R.string.alert_dialog_text_entry)
                .setView(textEntryView)
                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked OK so do some stuff */
                    }
                })
                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked cancel so do some stuff */
                    }
                })
                .create();

希望对你有帮助。

编辑:

布局名称:main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent" android:textColor="#663355"
        android:layout_height="wrap_content" android:hint="@string/hello" />
</LinearLayout>

如果布局名称是main.xml,那么您必须更改如下代码:

factory.inflate(R.layout.main, null);

【讨论】:

  • 在警报对话框中应用颜色的文本中在 APIDemos 中不清楚,以防 DIALOG_TEXT_ENTRY
  • 我的意思是你必须创建自己的布局。应该有一个textviewandroid:textColor 属性。例如检查我提到的布局。
  • 我正在动态实现警报对话框
猜你喜欢
  • 2016-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-23
  • 1970-01-01
  • 2016-10-25
相关资源
最近更新 更多