带有动态布局的自定义对话框显示不正确

Custom Dialog with Dynamic Layout is not displaying properly

我正在尝试创建一个自定义对话框,以根据搜索结果显示动态文本字段。此自定义对话框是从 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 或小部件可供参考 to.So 我认为我应该只通过编码来完成所有事情。这样想有错吗?这可能是问题所在吗?

我使用 here, here & here 作为指南来创建动态线性布局,然后再将其应用到我的自定义对话框中。我的自定义对话框可能有什么问题?

移除

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

如果你不想有透明对话框。