Set AlertBox Title Bar Background Color

后端 未结 3 734
囚心锁ツ
囚心锁ツ 2021-02-14 21:11

How can I change the background color for an alertbox\'s title bar?

AlertDialog.Builder alert=new AlertDialog.Builder(getParent());
alert.setTitle(\"sample\");
a         


        
3条回答
  •  深忆病人
    2021-02-14 21:54

    The easiest way is to subclass a dialog by creating a class which extends dialog and implements the constructor which take style as a parameter. Then make your own custom layout to it.

    The Code to show the dialog:

    private void showDialog()
    {
        Custom_Dialog dialog = new Custom_Dialog(this, R.style.myCoolDialog);
    
        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.icon);
    
        dialog.show();  
    }
    

    The code for the subclass:

    package com.stackoverflow;
    
    import android.app.Dialog;
    import android.content.Context;
    
    public class Custom_Dialog extends Dialog {
    
        protected Custom_Dialog(Context context, int theme) {
            super(context, theme);
            // TODO Auto-generated constructor stub
        }
    
    }
    

    the style: myCoolDialog.xml

    
         
    
    

    and last the layout:custom_dialog.xml

    
    
        
        
    
    

提交回复
热议问题