这里我有一些代码,它以编程方式向ConstraintLayout添加一些元素
public void AddCategory(final String label, String img){
final float scale = getResources().getDisplayMetrics().density;
//Create a new constraint layout
ConstraintLayout constraintLayout = new ConstraintLayout(getContext());
servicesLayout.addView(constraintLayout);
constraintLayout.setId(View.generateViewId());
android.support.v7.widget.GridLayout.LayoutParams layoutParams = new android.support.v7.widget.GridLayout.LayoutParams(android.support.v7.widget.GridLayout.spec(android.support.v7.widget.GridLayout.UNDEFINED, 1f), android.support.v7.widget.GridLayout.spec(android.support.v7.widget.GridLayout.UNDEFINED, 1f));
layoutParams.width = 0;
layoutParams.height = (int) scale * 200;
layoutParams.setMargins((int) scale * 15,(int) scale * 15,(int) scale * 15,(int) scale * 15);
constraintLayout.setLayoutParams(layoutParams);
constraintLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
//Place ImageView in layout
ImageView imageView = new ImageView(getContext());
constraintLayout.addView(imageView);
imageView.setId(View.generateViewId());
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.connect(imageView.getId(), ConstraintSet.TOP, constraintLayout.getId(), ConstraintSet.TOP);
constraintSet.connect(imageView.getId(), ConstraintSet.BOTTOM, constraintLayout.getId(), ConstraintSet.BOTTOM);
constraintSet.connect(imageView.getId(), ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.LEFT);
constraintSet.connect(imageView.getId(), ConstraintSet.RIGHT, constraintLayout.getId(), ConstraintSet.RIGHT);
constraintSet.applyTo(constraintLayout);
if (getContext() != null){
GlideApp.with(this)
.load(img)
.centerCrop()
.transition(DrawableTransitionOptions.withCrossFade())
.placeholder(R.color.colorGray)
.error(R.drawable.title)
.into(imageView);
}
//Place label background
ImageView captionImageView = new ImageView(getContext());
constraintLayout.addView(captionImageView);
captionImageView.setId(View.generateViewId());
ConstraintSet captionConstraintSet = new ConstraintSet();
captionConstraintSet.connect(captionImageView.getId(), ConstraintSet.BOTTOM, constraintLayout.getId(), ConstraintSet.BOTTOM);
captionConstraintSet.connect(captionImageView.getId(), ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.LEFT);
captionConstraintSet.connect(captionImageView.getId(), ConstraintSet.RIGHT, constraintLayout.getId(), ConstraintSet.RIGHT);
captionConstraintSet.applyTo(constraintLayout);
captionImageView.getLayoutParams().width = ConstraintLayout.LayoutParams.MATCH_CONSTRAINT;
captionImageView.getLayoutParams().height = (int) scale * 50;
captionImageView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
captionImageView.setAlpha(0.7f);
//Place text in front of the label backgroudn
TextView textView = new TextView(getContext());
constraintLayout.addView(textView);
textView.setId(View.generateViewId());
ConstraintSet labelConstraintSet = new ConstraintSet();
labelConstraintSet.connect(textView.getId(), ConstraintSet.BOTTOM, captionImageView.getId(), ConstraintSet.BOTTOM);
labelConstraintSet.connect(textView.getId(), ConstraintSet.LEFT, captionImageView.getId(), ConstraintSet.LEFT);
labelConstraintSet.connect(textView.getId(), ConstraintSet.RIGHT, captionImageView.getId(), ConstraintSet.RIGHT);
labelConstraintSet.applyTo(constraintLayout);
textView.getLayoutParams().width = ConstraintLayout.LayoutParams.WRAP_CONTENT;
textView.getLayoutParams().height = ConstraintLayout.LayoutParams.WRAP_CONTENT;
textView.setTextColor(getResources().getColor(R.color.colorWhite));
textView.setText(label);
}
前3个代码块工作得很好:以编程方式添加ConstraintLayout和两个ImageView.问题在于尝试约束TextView元素.它只是在尝试将其约束到其中一个需要的ImageView时无法正常工作.将它约束到ConstraintLayout时它会起作用,但这是不可取的.相反,它将忽略所有constraintSet.connect代码并将其放置在布局的默认右上角位置,如图所示
最佳答案 更改TextView的constraintSet的endId,如下所示: –
ConstraintSet labelConstraintSet = new ConstraintSet();
labelConstraintSet.connect(textView.getId(), ConstraintSet.BOTTOM, constraintLayout.getId(), ConstraintSet.BOTTOM,(int)scale*15);
labelConstraintSet.connect(textView.getId(), ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.LEFT);
labelConstraintSet.connect(textView.getId(), ConstraintSet.RIGHT, constraintLayout.getId(), ConstraintSet.RIGHT);