2017-08-27 71 views
0

我想创建一个AlertDialog像这样:的Android AlertDialog在活动Dialog.Theme

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(); 

我不断收到You need to use a Theme.AppCompat theme (or descendant) with this activity.

我的清单是:

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

我的主题对于延伸Activity的弹出窗口。主持弹出的活动扩展Fragment,我可以在片段上创建AlertDialog s,但我似乎无法打开弹出的AlertDialog

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

回答

1

检查你使用的风格的资源,活动和主题更改父主题Theme.AppCompat.Light即

或者

删除此行从您的活动您的清单

android:theme =“@ android:style/Theme.Dialog”

+0

是的,这是可行的,但我如何让活动看起来像Theme.Dialog,所以它会是一个弹出窗口? – Torewin

+0

检查这将是帮助:https://stackoverflow.com/questions/35834481/how-to-programatically-theme-an-activity-to-be-like-a-dialog – Prodigy

+0

任何运气与SO链接? – Prodigy