2011-01-24 68 views
3

我想在警告对话框的浅色背景上以深色显示文本。但我无法弄清楚如何做到这一点。请帮帮我。在Android中自定义警报对话框

谢谢。

+0

提供

实例看看[此帖] [1],对我来说非常有帮助 [1]:http://stackoverflow.com/a/10599154/599993 – jzafrilla 2012-12-03 10:05:22

回答

6

您可以在XML视图中创建自己的布局,就像你会为一个活动:

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); 

<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)使用此视图在对话

如示例所示,您必须在声明内容视图后设置一些值。从http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

+0

根据谷歌的文档,你应该避免直接实例化`Dialog`类。最好使用`DialogFragment` – Skillson 2017-08-08 12:44:05