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

匿名 (未验证) 提交于 2019-12-03 01:32:01

问题:

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.support.v7.app.AlertDialog package in order to use it.

But how can I style it? For example change the positive/negative button colors, title color, message color and background color?

回答1:

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

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.



回答2:

To use a theme for all the application, and don't use the second parameter to style your Dialog

On my app using a color accent in theme don't show the alertDialog's buttons with the theme colorAccent I have to add a dialog style in the theme.



回答3:

If you want to use the new android.support.v7.app.AlertDialog and have different colors for the buttons and also have a custom layout then have a look at my https://gist.github.com/JoachimR/6bfbc175d5c8116d411e

@NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) {      View v = inflater.inflate(R.layout.custom_layout, null);      initDialogUi(v);      final AlertDialog d = new AlertDialog.Builder(activity, R.style.AppCompatAlertDialogStyle)             .setTitle(getString(R.string.some_dialog_title))             .setCancelable(true)             .setPositiveButton(activity.getString(R.string.some_dialog_title_btn_positive),                     new DialogInterface.OnClickListener() {                         @Override                         public void onClick(DialogInterface dialog, int which) {                             doSomething();                             dismiss();                         }                     })             .setNegativeButton(activity.getString(R.string.some_dialog_title_btn_negative),                     new DialogInterface.OnClickListener() {                         @Override                         public void onClick(DialogInterface dialog, int which) {                             dismiss();                         }                     })             .setView(v)             .create();      // change color of positive button              d.setOnShowListener(new DialogInterface.OnShowListener() {         @Override         public void onShow(DialogInterface dialog) {             Button b = d.getButton(DialogInterface.BUTTON_POSITIVE);             b.setTextColor(getResources().getColor(R.color.colorPrimary));         }     });      return d; } 



回答4:

Follow @reVerse answer but in my case, I already had some property in my AppTheme like

So my dialog will look like

I solved it by

1) Change the import from android.app.AlertDialog to android.support.v7.app.AlertDialog
2) I override 2 property in AppTheme with null value

.

AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.MyAlertDialogStyle); 

Hope it help another people



回答5:

    @color/white@color/white@color/gray@color/gray@color/gray@color/white#30FFFFFF


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!