【发布时间】:2015-11-08 16:02:13
【问题描述】:
我想动态创建一个 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();
}
});
}
【问题讨论】:
-
请分享一些代码
-
@Roy 在下面查看我的代码
-
@Chairizky - 感谢您的回复,但我想在 AlertDialog 中显示我的自定义布局(以编程方式创建,而不是通过 XML),而不仅仅是在 AlertDialog 上显示一条消息。
标签: android android-linearlayout android-alertdialog android-4.1-jelly-bean