问题
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