我有一个线性布局,用户可以动态地向其插入元素以发送到服务器,我计划在用户单击发送按钮后有一个确认对话框。在对话框中,我想再次显示布局。我可以以某种方式克隆或移动视图到新的对话框,而不是再次进行。
答案 0 :(得分:1)
ViewGroup viewRoot = (ViewGroup)LayoutInflater.from(this).inflate(R.layout.dialog_example, null);
//you can measure the width of viewRoot,if the width is specified
int measuredWidth = DimenUtil.getMeasuredWidth(viewRoot);
int measuredHeight = DimenUtil.getMeasuredHeight(viewRoot);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog dialog = builder.create();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCanceledOnTouchOutside(true);
/ ** 你应该在setContentView(int layout)之前执行语句,否则会发生AndroidRuntimeException,因为在添加内容之前必须调用requestFeature(),我也对这个原因感到困惑 ** /
dialog.show();
dialog.setContentView(viewRoot);
Window window = dialog.getWindow();
window.setGravity(gravity);
window.setLayout(measuredWidth, measuredHeight);