weird behavior when applying theme to dialog under api 21 [duplicate]

陌路散爱 提交于 2020-01-04 06:55:26

问题


I am using the theme "Theme.AppCompat.Light.NoActionBar" in my app . I want to make some of my dialogs applying the dark AppCompat theme.

So,i created style for the dialog

 <style name="MyDialogStyle" parent="Theme.AppCompat.Dialog">

 </style>

(same issue when the parent is "Theme.AppCompat.Dialog.Alert") one in xml file without version constrain and the same style in xml file with version api 21 constrain. to invoke the dialog i used this function :

 public void showSimplestDialog(View view) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.MyDialogStyle);
    AlertDialog alertDialog = builder.setTitle("title")
            .setMessage("message ")
            .create();
    alertDialog.show();
}

the result in api 21+ look fine

but in api 17 i got some duplicate background that i cant get rid off (even when i try to apply custom view to the dialog with builder.setView(MyView)


回答1:


Make sure you have to import android.support.v7.app.AlertDialog this thing.

Then Create This way

AlertDialog.Builder builder =
       new AlertDialog.Builder(this, R.style.DialogStyle);
            builder.setTitle("Title");
            builder.setMessage("Abc ...");
            builder.setPositiveButton("OK", null);
            builder.setNegativeButton("Cancel", null);
            builder.show();

and Create style in styles.xml

<style name="DialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="colorAccent">#FFCC00</item>
        <item name="android:textColorPrimary">#FFFFFF</item>
        <item name="android:background">#5fa3d0</item>
    </style>


来源:https://stackoverflow.com/questions/40738437/weird-behavior-when-applying-theme-to-dialog-under-api-21

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