How can I create my own theme for alertDialog?

后端 未结 5 876
有刺的猬
有刺的猬 2021-01-16 13:21

I want to create a different theme for all of alertDialog instances. I need my own title view instead of the usual black title background. All text should have a blue color,

5条回答
  •  难免孤独
    2021-01-16 14:27

    Here one example For Dialog:

    button = (Button) findViewById(R.id.button1);
    
                button.setOnClickListener(new OnClickListener() {
    
                  @Override
                  public void onClick(View arg0) {
    
                    // custom dialog
                    final Dialog dialog = new Dialog(context);
                    dialog.setContentView(R.layout.custom);
                    dialog.setTitle("Give a Title...");
    
                    // set the custom dialog components - text, image and button
                    TextView text = (TextView) dialog.findViewById(R.id.text);
                    text.setText("Put here any custom text!");
                    ImageView image = (ImageView) dialog.findViewById(R.id.image);
                    image.setImageResource(R.drawable.ic_launcher);
    
                    Button dialogButton = (Button) dialog.findViewById(R.id.ButtonOK);
                    // if button is clickedthen dialog will closed
                    dialogButton.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
    
                    dialog.show();
                  }
                });
    

    Custom AlertDialog

        AlertDialog.Builder builder;
        AlertDialog alertDialog;
        Context mContext;
        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
        View urlayoutfile = inflater.inflate(R.layout.custom_dialog_xmlfile,(ViewGroup)findViewById(R.id.Yourlayout_id));
        builder = new AlertDialog.Builder(this);
        builder.setView(urlayoutfile);
        alertDialog = builder.create(); 
        alertDialog.show();
    

提交回复
热议问题