Child 可滚动的表格布局未显示在我的警报对话框中。为什么?

Child scrollable tablelayout isn't showing in my alertdialog. Why?

我正在尝试在我的应用程序的自定义提醒对话框中显示 table 数据。

我的自定义提醒xml:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*">
<TableRow
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:text="Page" />
    <TextView
        android:text="ENo" />
    <TextView
        android:text="QNo" />
    <TextView
        android:text="P_date" />
    <TextView
        android:text="P_read" />
    <TextView
        android:text="C_read" />
</TableRow>
<TableRow>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="450dp"
        android:scrollbars="vertical"
        android:layout_weight="1"
        >
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/meter_details"
            android:stretchColumns="*">
        </TableLayout>
    </ScrollView>
</TableRow>
</TableLayout>

我的java填充childtable的代码(ID:meter_details)

AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
AlertDialog ad;
builder.setIcon(R.drawable.ic_power);

builder.setTitle("Meter entry");
builder.setMessage("Please enter the details");

Context dialogContext = builder.getContext();
LayoutInflater inflater = LayoutInflater.from(dialogContext);
View dialogView = inflater.inflate(R.layout.meter_entry_dialog, null);
builder.setView(dialogView);

TableLayout tbl = dialogView.findViewById(R.id.meter_details);

for(int i=0;i<arr.size();i++){
   TableRow row = new TableRow(getContext());
   row.setBackground(getResources().getDrawable(R.drawable.border));
   TextView pg = new TextView(getContext());
   TextView empno = new TextView(getContext());
   TextView qtrno = new TextView(getContext());
   TextView prev_date = new TextView(getContext());
   EditText prev_read = new EditText(getContext());
   EditText curr_read = new EditText(getContext());

   try {
      pg.setText(arr.get(i).get(7).toString());
      empno.setText(arr.get(i).get(1).toString());
      qtrno.setText(arr.get(i).get(2).toString());
      prev_date.setText(arr.get(i).get(5).toString());
      prev_read.setText(arr.get(i).get(4).toString());

      row.addView(pg);
      row.addView(empno);
      row.addView(qtrno);
      row.addView(prev_date);
      row.addView(prev_read);
      row.addView(curr_read);

      tbl.addView(row);
  } catch (JSONException e) {
      e.printStackTrace();
  }
}
builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
         dialogInterface.dismiss();
    }
});
builder.setCancelable(false);
ad = builder.create();
ad.show();
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(ad.getWindow().getAttributes());
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
ad.getWindow().setAttributes(layoutParams);

只有我的 parent table 布局的第一行显示在我的 alertdialog 中。我的其余数据(我从数组中获取并动态创建 table 行并存储它们)没有显示。

我不知道为什么会这样。我什至检查了我的 arr,它具有所有需要的值。请帮助我

我认为你应该为 ScrollView 设置固定高度。

您应该将膨胀的 dialogView 设置为对话框的视图。
将此行 builder.setView(R.layout.meter_entry_dialog); 更改为 builder.setView(dialogView); 并将其移动到 dialogView 的初始化之后。
此外,您可以使用 getContext() 布局充气器的方法。

AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
AlertDialog ad;
builder.setIcon(R.drawable.ic_power);

builder.setTitle("Meter entry");
builder.setMessage("Please enter the details");

LayoutInflater inflater = LayoutInflater.from(getContext());

View dialogView = inflater.inflate(R.layout.meter_entry_dialog, null);
builder.setView(dialogView);

TableLayout tbl = dialogView.findViewById(R.id.meter_details);
...