我想动态创建一个LinearLayout。
如何将其设置为在我的 AlertDialog 中显示?
我见过一些例子,其中布局是通过 XML 创建并膨胀显示的,但是当我可以动态地创建 XML 布局时,我不想创建它。
我仅限于 API 16 = Android 4.1.2
这是我活动中的一个按钮...
public void TestOnClick() {
Button test_button = (Button) findViewById(R.id.button_test);
test_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout layout = new LinearLayout(v.getContext());
//Create a TextView to add to layout
TextView textview = new TextView(v.getContext());
textview.setText("My Test");
layout.addView(textview);
//Add abunch of other items to the layout
//blah blah blah
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setView(layout);
builder.setNeutralButton("Done", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}