Android AlertDialog in Activity Dialog.Theme

2019-08-16 02:44发布

问题:

I am trying to create an AlertDialog like so:

Context context = AttributeDescription.this;
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Add Extra");
final EditText base = new EditText(builder.getContext());
final EditText value = new EditText(builder.getContext());
base.setHint("Name");
value.setHint("Value");
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
base.setInputType(InputType.TYPE_CLASS_TEXT);
value.setInputType(InputType.TYPE_CLASS_TEXT);
layout.addView(base);
layout.addView(value);
builder.setView(layout);
builder.setMessage("")
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int id) {

         }
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
        // User cancelled the dialog
        }
});
// Create the AlertDialog object and return it
AlertDialog dialog = builder.create();
dialog.show();

I keep getting You need to use a Theme.AppCompat theme (or descendant) with this activity.

My manifest is:

android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/AppTheme"

I have the theme for the pop up that extends Activity. And the Activity that hosts the pop up extends Fragment, I am able to create AlertDialogs on the fragment, but I can't seem to open the AlertDialogon the pop up.

<activity
   android:name=".Activties.InventoryDescription"
   android:theme="@android:style/Theme.Dialog" />
<activity

回答1:

Check the theme you using for that Activity in style resource and change the parent theme to Theme.AppCompat.Light I.e

Or

remove this line from your activity in your manifest

android:theme="@android:style/Theme.Dialog"