【问题标题】:How to programmatically put a view on the back of others in constraint layout如何在约束布局中以编程方式将视图放在其他人的后面
【发布时间】:2018-03-27 12:40:25
【问题描述】:

我使用了约束布局并在我的视图中添加了 2 个按钮。

现在我想在其中添加背景图像,但我将以编程方式添加。

这就是我添加图像时所做的。

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

上面的代码所做的是它在button2 上添加了一个图像,我希望它在button2 后面而不是button2 上绘制图像。如果是从 xml 执行,这会容易得多,但我无法理解如何以编程方式实现这一点。

【问题讨论】:

  • 您是否考虑过将例如 ViewStub 添加到包含您的背景的 xml 中?
  • 我不知道,只是用谷歌搜索了这个。
  • 当然你也可以添加例如FrameLayout作为匹配父级的子级,然后在里面添加ImageView
  • @Kirmani88 你能分享你的预期布局吗

标签: android performance android-layout android-constraintlayout


【解决方案1】:
constraintLayout.setBackgroundResource(R.drawable.background);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    相关资源
    最近更新 更多