如何更改Android Alert Dialog标题背景

8

我想要更改警告对话框的背景。我已经尝试了以下代码:

 <style name="Theme.AlertDialog" parent="@android:style/Theme.Holo.Light.Dialog">
        <item name="android:background">@android:color/transparent</item>
        <item name="android:textColor">#659F26</item>
        <item name="android:windowTitleStyle">@style/Theme.AlertDialog.Title</item>
    </style>

    <style name="Theme.AlertDialog.Title" parent="@android:style/TextAppearance.DialogWindowTitle">
        <item name="android:background">@drawable/cab_background_top_example</item>
    </style>

Java代码:

ContextThemeWrapper ctw = new ContextThemeWrapper( this, R.style.Theme_AlertDialog);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw);

alertDialogBuilder.setTitle(getResources().getString(R.string.confirmation));   
alertDialogBuilder.setMessage(getResources().getString(R.string.msg));       
alertDialogBuilder.show();

我的应用程序显示如下对话框:

enter image description here

但我希望它看起来像这样:

enter image description here

请建议我做错了什么。


这个回答解决了你的问题吗?如何在对话框中移除标题? - General Grievance
2个回答

6

创建对话框时使用以下代码:

 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);   

创建自己的布局。在许多自定义操作中,可以设置标题并设置您的布局。

  dialog.setContentView(R.layout.yourlayout);

注意:使用

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); before     
dialog.setContentView(R.layout.yourlayout);

否则会出现错误。

0
ContextThemeWrapper ctw = new ContextThemeWrapper( this, R.style.Theme_AlertDialog);

在这行代码中,使用ActivityName.this代替"this"。

1
这不是 OP 问题的解决方案。 - laalto

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