简体   繁体   中英

How to change the background of Android Alert Dialog Title

I want to change the background of the Alert dialog. I have tried following code piece to do so:

 <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 Code:

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

My app is showing dialog like this:

在此输入图像描述

while i want it to look like:

在此输入图像描述

Please suggest, what i am doing wrong.

Use this code when creating a dialog:

 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);   

create your own layout As many customise done with title then set your layout

  dialog.setContentView(R.layout.yourlayout);

NOTE: use

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

otherwise it give error.

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

在这一行中使用而不是“ this ”使用ActivityName.this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM