带有setAdjustViewBounds的Android ImageView在ConstraintLayout中具有无法解释的行为

【带有setAdjustViewBounds的Android ImageView在ConstraintLayout中具有无法解释的行为】犀渠玉剑良家子,白马金羁侠少年。这篇文章主要讲述带有setAdjustViewBounds的Android ImageView在ConstraintLayout中具有无法解释的行为相关的知识,希望能为你提供帮助。
我在ScrollView中有一个android ConstraintLayout。我在布局中添加了几个按钮和一个ImageView。其中一个按钮应限制在ImageView的底部。
这就出现了问题:只要我将图像设置为调整视图的边界(setAdjustViewBounds),图像就会根据需要调整大小,但是应该跟随ImageView的按钮会在图像的底部绘制,不在它下面。
请参阅以下输出:This is how it looks
当我再添加一个按钮时,其定义必须遵循ImageView定义,那么一切看起来都很好,即使它的约束与图像约束无关。如果我在ImageView定义之前移动附加按钮的定义,我再次得到错误的输出,当然还有一个额外的按钮。
这是正确的输出:And this is how it should look
但是我不想在ImageView之后进行任何额外的声明来使布局工作。
有没有人有想法,这里出了什么问题?
这是我的代码产生button2的错误位置(测试2):

public class MainActivity extends AppCompatActivity {int id = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ScrollView sv = new ScrollView(this); sv.setId(id++); ConstraintLayout mainLayout = new ConstraintLayout(this); mainLayout.setId(id++); mainLayout.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); Button button = new Button(this); button.setId(id++); button.setText("Test"); mainLayout.addView(button); Button button1 = new Button(this); button1.setId(id++); button1.setText("Test 1"); mainLayout.addView(button1); ImageView image = new ImageView(this); image.setId(id++); image.setImageResource(R.drawable.img); image.setBackgroundColor(Color.parseColor("#0000FF")); image.setLayoutParams(new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_CONSTRAINT, ConstraintLayout.LayoutParams.WRAP_CONTENT)); image.setAdjustViewBounds(true); mainLayout.addView(image); Button button2 = new Button(this); button2.setId(id++); button2.setText("TEST 2"); mainLayout.addView(button2); //getFragmentManager().beginTransaction().add(sv.getId(), BlankFragment.newInstance("Inhalt 1"), "someTag1").commit(); sv.addView(mainLayout); ConstraintSet constraintSet = new ConstraintSet(); constraintSet.clone(mainLayout); constraintSet.constrainWidth(button.getId(), constraintSet.MATCH_CONSTRAINT); constraintSet.connect(button.getId(), ConstraintSet.TOP, mainLayout.getId(), ConstraintSet.TOP, 10); constraintSet.connect(button.getId(), ConstraintSet.START, mainLayout.getId(), ConstraintSet.START, 50); constraintSet.connect(button.getId(), ConstraintSet.END, mainLayout.getId(), ConstraintSet.END, 50); constraintSet.constrainWidth(button1.getId(), constraintSet.MATCH_CONSTRAINT); constraintSet.connect(button1.getId(), ConstraintSet.TOP, button.getId(), ConstraintSet.BOTTOM, 10); constraintSet.connect(button1.getId(), ConstraintSet.START, button.getId(), ConstraintSet.START, 0); constraintSet.connect(button1.getId(), ConstraintSet.END, button.getId(), ConstraintSet.END, 700); constraintSet.constrainWidth(image.getId(), constraintSet.MATCH_CONSTRAINT); constraintSet.connect(image.getId(), ConstraintSet.TOP, button1.getId(), ConstraintSet.BOTTOM, 10); constraintSet.connect(image.getId(), ConstraintSet.START, button.getId(), ConstraintSet.START, 100); constraintSet.connect(image.getId(), ConstraintSet.END, button.getId(), ConstraintSet.END, 100); constraintSet.constrainWidth(button2.getId(), constraintSet.MATCH_CONSTRAINT); constraintSet.connect(button2.getId(), ConstraintSet.TOP, image.getId(), ConstraintSet.BOTTOM, 10); constraintSet.connect(button2.getId(), ConstraintSet.START, button.getId(), ConstraintSet.START, 0); constraintSet.connect(button2.getId(), ConstraintSet.END, button.getId(), ConstraintSet.END, 0); constraintSet.connect(button2.getId(), ConstraintSet.BOTTOM, mainLayout.getId(), ConstraintSet.BOTTOM, 10); constraintSet.applyTo(mainLayout); setContentView(sv); }

}
这里是带有附加按钮的代码,它将button2移动到正确的位置。没什么特别的,只是额外的按钮。
公共类MainActivity扩展AppCompatActivity {
int id = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ScrollView sv = new ScrollView(this); sv.setId(id++); ConstraintLayout mainLayout = new ConstraintLayout(this); mainLayout.setId(id++); mainLayout.setLayoutParams(new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); Button button = new Button(this); button.setId(id++); button.setText("Test"); mainLayout.addView(button); Button button1 = new Button(this); button1.setId(id++); button1.setText("Test 1"); mainLayout.addView(button1); ImageView image = new ImageView(this); image.setId(id++); image.setImageResource(R.drawable.img); image.setBackgroundColor(Color.parseColor("#0000FF")); image.setLayoutParams(new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_CONSTRAINT, ConstraintLayout.LayoutParams.WRAP_CONTENT)); image.setAdjustViewBounds(true); mainLayout.addView(image); //Even when button3 is left inside the example, but its declaration (next four lines) are moved above the imageview-initialization, the error occurs Button button3 = new Button(this); button3.setId(id++); button3.setText("Test 3"); mainLayout.addView(button3); Button button2 = new Button(this); button2.setId(id++); button2.setText("TEST 2"); mainLayout.addView(button2); //getFragmentManager().beginTransaction().add(sv.getId(), BlankFragment.newInstance("Inhalt 1"), "someTag1").commit(); sv.addView(mainLayout); ConstraintSet constraintSet = new ConstraintSet(); constraintSet.clone(mainLayout); constraintSet.constrainWidth(button.getId(), constraintSet.MATCH_CONSTRAINT); constraintSet.connect(button.getId(), ConstraintSet.TOP, mainLayout.getId(), ConstraintSet.TOP, 10); constraintSet.connect(button.getId(), ConstraintSet.START, mainLayout.getId(), ConstraintSet.START, 50); constraintSet.connect(button.getId(), ConstraintSet.END, mainLayout.getId(), ConstraintSet.END, 50); constraintSet.constrainWidth(button1.getId(), constraintSet.MATCH_CONSTRAINT); constraintSet.connect(button1.getId(), ConstraintSet.TOP, button.getId(), ConstraintSet.BOTTOM, 10); constraintSet.connect(button1.getId(), ConstraintSet.START, button.getId(), ConstraintSet.START, 0); constraintSet.connect(button1.getId(), ConstraintSet.END, button.getId(), ConstraintSet.END, 700); constraintSet.constrainWidth(image.getId(), constraintSet.MATCH_CONSTRAINT); constraintSet.connect(image.getId(), ConstraintSet.TOP, button1.getId(), ConstraintSet.BOTTOM, 10); constraintSet.connect(image.getId(), ConstraintSet.START, button.getId(), ConstraintSet.START, 100); constraintSet.connect(image.getId(), ConstraintSet.END, button.getId(), ConstraintSet.END, 100); constraintSet.constrainWidth(button3.getId(), constraintSet.MATCH_CONSTRAINT); constraintSet.connect(button3.getId(), ConstraintSet.TOP, button.getId(), ConstraintSet.BOTTOM, 10); constraintSet.connect(button3.getId(), ConstraintSet.START, button.getId(), ConstraintSet.START, 700); constraintSet.connect(button3.getId(), ConstraintSet.END, button.getId(), ConstraintSet.END, 0); constraintSet.constrainWidth(button2.getId(), constraintSet.MATCH_CONSTRAINT); constraintSet.connect(button2.getId(), ConstraintSet.TOP, image.getId(), ConstraintSet.BOTTOM, 10); constraintSet.connect(button2.getId(), ConstraintSet.START, button.getId(), ConstraintSet.START, 0); constraintSet.connect(button2.getId(), ConstraintSet.END, button.getId(), ConstraintSet.END, 0); constraintSet.connect(button2.getId(), ConstraintSet.BOTTOM, mainLayout.getId(), ConstraintSet.BOTTOM, 10); constraintSet.applyTo(mainLayout); setContentView(sv); }

}
答案Cheticamp提示检查版本是解决方案。更新到最新的ConstraintLayout版本,目前1.1.0-beta4,一切看起来都不错。似乎是我的旧版本的问题,即1.0.2。

    推荐阅读