在Android中自定义警告对话框

6
我想在警告对话框上显示黑色文本,背景为浅色。但我不知道该如何实现。请帮帮我。
谢谢。

看一下这篇帖子,对我很有帮助。 - jzafrilla
2个回答

7
您可以像为Activity创建布局一样,在XML视图中创建自己的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout_root"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="10dp"
              >
    <ImageView android:id="@+id/image"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_marginRight="10dp"
               />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#FFF"
              />
</LinearLayout>

然后你可以通过在对话框上调用 setContentView(View) 来使用这个视图:

Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);

dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");

TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

正如示例所示,您需要在声明内容视图后设置一些值。
示例提供自http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

根据谷歌的文档,应该避免直接实例化Dialog类。最好使用DialogFragment - Mahdi-Malv

6

2
那是一个自定义对话框。我们不能修改警告对话框本身吗? - Greenhorn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接