这里有一些代码,可以通过编程方式向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和两个ImageViews。问题在于试图限制TextView元素。尝试将其约束到需要的ImageViews之一时,它只是根本不起作用。将其约束到ConstraintLayout时它将起作用,但这是不可取的。相反,它将忽略所有constraintSet.connect代码,并将其放置在布局的默认右上角位置,如图所示
答案 0 :(得分:1)
如下所示更改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);