我正在尝试创建一个自定义对话框,该对话框根据搜索结果显示动态文本字段。该自定义对话框是从 Activity 中调用的。创建对话框后,它没有按预期显示。不知何故,该对话框看起来不像已创建。
现在创建的对话框:
我期待它是这样的:
下面是我调用对话框的代码:
public class DeleteVehicle extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delete_vehicle);
Button deleteVehicleButton;
deleteVehicleButton = (Button) findViewById(R.id.deleteVehicleBySearchButton);
deleteVehicleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Pattern pattern;
Matcher matcher;
int searchIndex = 0;
boolean result = false;
ArrayList<String> arrayList;
if (!vehicleOwnerEditView.getText().toString().isEmpty()) {
pattern = Pattern.compile(vehicleOwnerEditView.getText().toString());
arrayList = db.fetchDatas(getId,"fullname");
} else {
pattern = Pattern.compile(vehiclePlateNumberEditView.getText().toString());
arrayList = db.fetchDatas(getId,"plate_number");
}
if (arrayList.isEmpty()) {
Log.e("DeleteVehicle","arrayList is 0");
} else {
while (searchIndex != arrayList.size()) {
matcher = pattern.matcher(arrayList.get(searchIndex));
result = matcher.matches();
if (result == true){
Log.e("DeleteVehicle","result true, searchIndex = " + arrayList.get(searchIndex));
searchIndex++;
//Toast.makeText(DeleteVehicle.this, arrayList.get(searchIndex), Toast.LENGTH_SHORT).show();
} else {
Log.e("DeleteVehicle","result false, searchIndex = " + arrayList.get(searchIndex));
arrayList.remove(searchIndex);
Log.e("DeleteVehicle","size after remove = " + arrayList.size());
}
Log.e("DeleteVehicle","searchIndex = " + searchIndex);
}
Related to calling the dialog ---> DeleteVehicleDialog deleteVehicleDialog = DeleteVehicleDialog.getInstanceFor(arrayList);
Related to calling the dialog ---> deleteVehicleDialog.show(getSupportFragmentManager(),"deleteVehicle");
}
}
});
}
}
这就是我创建对话框的方式:
public class DeleteVehicleDialog extends DialogFragment {
private ArrayList<String> list;
public static DeleteVehicleDialog getInstanceFor(ArrayList<String> list) {
DeleteVehicleDialog deleteVehicleDialog = new DeleteVehicleDialog();
deleteVehicleDialog.list=list;
return deleteVehicleDialog;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(getContext());
for(int i=0; i<5; i++){
TextView textView= new TextView(getContext());
linearLayout.setOrientation(LinearLayout.VERTICAL);
textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setText("Testing Textview: " + i);
linearLayout.addView(textView);
}
final Dialog dialog = new Dialog(getContext());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(linearLayout);
dialog.setTitle("deleteVehicle");
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.show();
return dialog;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
}
我没有为此布局创建任何 XML,因为它将是动态显示。没有ID或小部件可供引用。所以我想我应该只从编码开始做所有事情。这样想有错吗?这会是问题所在吗?
我使用here , here & here作为在将动态 Linearlayout 应用到我的自定义对话框之前创建动态 Linearlayout 的指南。我的自定义对话框可能存在什么问题?
最佳答案
删除
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
如果您不想有透明的对话框。
关于java - 具有动态布局的自定义对话框未正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59437643/