Android - Custom AlertDialog Background Color

前端 未结 3 1941
忘掉有多难
忘掉有多难 2020-12-10 06:32

So I see we can have alertdialogs with gray and white (when setinverse...) background colors.

To learn why I checked themes.xml of the sdk, checking it I was led to

3条回答
  •  萌比男神i
    2020-12-10 06:53

    Instead of using AlertDialog, I ended up using a Dialog. To get a custom look:

    1-Create the Dialog and remove the title area(Otherwise you'll get a blank gray area on top):

    myDialog = new Dialog(this);
    myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    

    2-Design a layout in xml, and set as dialog's content:

    myDialog.setContentView(R.layout.mydialog_layout);
    

    3-If the layout is not a rounded rect, it will intersect with the rounded corners of the dialog box. So, design the layout as a rounded rect:

    in mydialog_layout.xml:

    android:background = "@layout/mydialog_shape"
    

    mydialog_shape.xml:

     
     
          
    
         
    
    

    4-Add listeners to the buttons in your activity:

    Button button = (Button)myDialog.findViewById(R.id.dialogcancelbutton);
    button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        myDialog.cancel();
    }});
    

    That's about it.

提交回复
热议问题