我正在尝试以编程方式将视图添加到
ConstraintLayout
中。问题在于,ConstraintLayout.LayoutParams
忽略了MATCH_CONSTRAINT
,导致我的视图只是居中而没有达到最大宽度。 TextView textView = new TextView(context);
ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(MATCH_CONSTRAINT, WRAP_CONTENT);
lp.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID;
lp.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
lp.rightToLeft = guideline.getId();
lp.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
textView.setLayoutParams(lp);
textView.setText("Title text");
textView.setBackgroundColor(Color.BLUE);
constraintLayout.addView(textView);
LayoutParams
对此无效。 - S Haque