如何使用动态表格布局显示自定义对话框



我想创建在自定义对话框中动态创建的表布局。有人能告诉我怎么做吗?

我做了一些事情,但对话框中没有显示任何内容。我在那个对话框中没有表格布局。

有人能告诉我我在哪里犯的错吗?这是我的活动:

LayoutInflater inflater = getActivity().getLayoutInflater();
View dialog_view = inflater.inflate(R.layout.progressdialog, null);
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
dialog.setView(dialog_view);
TableLayout table_layout = (TableLayout)dialog_view.findViewById(R.id.Table_view_in_dialog);
for (int i = 1; i <= 4; i++) {
    TableRow row = new TableRow(getActivity());
    row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
    LayoutParams.WRAP_CONTENT));
    // inner for loop
    for (int j = 1; j <= 4; j++) {
        TextView tv = new TextView(getActivity());
        tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                                LayoutParams.WRAP_CONTENT));
                        tv.setBackgroundResource(R.drawable.cell_shape);
        tv.setPadding(5, 5, 5, 5);
        tv.setText("R " + i + ", C" + j);
        row.addView(tv);
    }
    table_layout.addView(row);
}
AlertDialog alert = dialog.create();
alert.show();

为什么不使用对话框片段,http://developer.android.com/reference/android/app/DialogFragment.html

这是最好的使用,因为你可以管理这个类似的片段,因为它有自己的UI

作为

public class SearchWeeklyAlertDialog extends DialogFragment 
{
 @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.NewDialog);
    }
@Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dialog_serach_days, container);
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
// your stuff like find ids and perform operations 
}
}

你可以称之为

  SearchWeeklyAlertDialog dialog = new SearchWeeklyAlertDialog();
  dialog.show(getChildFragmentManager(), DIALOG_FRAGMENT);

最新更新