Android 约束集问题,无法解决问题
我扩展了ConstraintLayout,并通过编程方式添加视图,然后为每个视图设置约束。但有些人认为它不起作用,只有第一个视图出现了。我想不出哪里出了错Android 约束集问题,无法解决问题,android,android-constraintlayout,constraintset,dynamiclayout,Android,Android Constraintlayout,Constraintset,Dynamiclayout,我扩展了ConstraintLayout,并通过编程方式添加视图,然后为每个视图设置约束。但有些人认为它不起作用,只有第一个视图出现了。我想不出哪里出了错 addViews(){ titleInfoView = new TextView(getContext()); titleInfoView.setId(ViewCompat.generateViewId()); addView(titleInfoView); editBoxVi
addViews(){
titleInfoView = new TextView(getContext());
titleInfoView.setId(ViewCompat.generateViewId());
addView(titleInfoView);
editBoxView = new EditText(contextWrapper);
editBoxView.setId(ViewCompat.generateViewId());
addView(editBoxView);
notInfoView = new TextView(getContext());
notInfoView.setId(ViewCompat.generateViewId());
addView(notInfoView);
errorMessageView = new TextView(getContext());
errorMessageView.setId(ViewCompat.generateViewId());
addView(errorMessageView);
}
然后向每个视图添加约束
private void addConstraintToViews() {
ConstraintSet set = new ConstraintSet();
set.clone(this);
//connect title view
set.connect(titleInfoView.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
set.connect(titleInfoView.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);
set.connect(titleInfoView.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);
//connect edit box
set.connect(editBoxView.getId(), ConstraintSet.TOP, titleInfoView.getId(), ConstraintSet.BOTTOM, R.dimen.margin_10);
set.connect(editBoxView.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);
set.connect(editBoxView.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);
//connect info title
set.connect(notInfoView.getId(), ConstraintSet.TOP, editBoxView.getId(), ConstraintSet.BOTTOM, R.dimen.margin_10);
set.connect(notInfoView.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);
set.connect(notInfoView.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);
//connect error messag
set.connect(errorMessageView.getId(), ConstraintSet.TOP, notInfoView.getId(), ConstraintSet.BOTTOM, R.dimen.margin_10);
set.connect(errorMessageView.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);
set.connect(errorMessageView.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);
set.connect(errorMessageView.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
set.applyTo(this);
}
只有第一项显示为标题,如图所示。我想一个接一个地添加每个视图,这就是我编写constraintset的方式。有人能指出哪里出了问题吗。
我终于找到了问题所在。我为每个小部件正确设置了布局参数,但当我在父约束布局下为每个小部件设置约束时,我在末尾添加了边距 这似乎是个问题,可能是约束布局上的一些错误。刚刚删除了该页边距,现在显示视图。尝试设置为
EditText
。