How to use and style new AlertDialog from appCompat 22.1 and above

前端 未结 6 1526
北荒
北荒 2020-11-27 08:59

I am trying to migrate from default android AlertDialog to the new one included in appCompat-22.1 So far I understand you only have to import android.sup

6条回答
  •  清酒与你
    2020-11-27 09:46

    When creating the AlertDialog you can set a theme to use.

    Example - Creating the Dialog

    AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
    builder.setTitle("AppCompatDialog");
    builder.setMessage("Lorem ipsum dolor...");
    builder.setPositiveButton("OK", null);
    builder.setNegativeButton("Cancel", null);
    builder.show();
    

    styles.xml - Custom style

    
    

    Result

    styled alertdialog

    Edit

    In order to change the Appearance of the Title, you can do the following. First add a new style:

    
    

    afterwards simply reference this style in your MyAlertDialogStyle:

    
    

    This way you can define a different textColor for the message via android:textColorPrimary and a different for the title via the style.

提交回复
热议问题