Android 如何以编程方式将视图放在约束布局中其他视图的背面
我使用了约束布局,并在视图中添加了2个按钮 现在我想在其中添加一个背景图像,但我将以编程方式添加 这就是我添加图像所做的Android 如何以编程方式将视图放在约束布局中其他视图的背面,android,performance,android-layout,android-constraintlayout,Android,Performance,Android Layout,Android Constraintlayout,我使用了约束布局,并在视图中添加了2个按钮 现在我想在其中添加一个背景图像,但我将以编程方式添加 这就是我添加图像所做的 ConstraintLayout constraintLayout; constraintLayout= (ConstraintLayout) findViewById(R.id.main_constraint_Layout); ImageView imageView = new ImageView(t
ConstraintLayout constraintLayout;
constraintLayout= (ConstraintLayout) findViewById(R.id.main_constraint_Layout);
ImageView imageView = new ImageView(this);
imageView.setImageDrawable(imageDrawable);
imageView.setId(View.generateViewId());
constraintLayout.addView(imageView);
ConstraintSet set = new ConstraintSet();
set.clone(constraintLayout);
set.connect(imageView.getId(), ConstraintSet.TOP, button2.getId(), ConstraintSet.TOP);
上面的代码所做的是在
按钮2
上添加一个图像,我希望它在按钮2
后面绘制图像,而不是在按钮2
上。如果is是从xml来实现的,这会容易得多,但我无法理解如何通过编程实现这一点 你有没有想过将eg-ViewStub添加到包含背景的xml中?我不知道,只是用谷歌搜索了一下。当然,你也可以将eg-FrameLayout添加为与父级匹配的子级,然后只添加ImageViewinside@Kirmani88你能分享你的预期布局吗
constraintLayout.setBackgroundResource(R.drawable.background);