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

时间:2015-07-01 11:57:23

标签: android dialog

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

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

有谁能告诉我哪里弄错了,拜托? 这是我的活动:

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

1 个答案:

答案 0 :(得分:0)

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

这是最好用的,因为你可以像片段一样管理它,因为它有自己的UI

as

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