【发布时间】:2019-10-16 15:21:10
【问题描述】:
我正在尝试在运行时在约束布局中将 TextViews 添加到另一个下方。但我总是只得到一个文本视图,其余的隐藏在它后面。我尝试了几件事,包括链接视图,但似乎没有任何效果。
private void method(int position)
{
ConstraintSet set = new ConstraintSet();
TextView textView = new TextView(getContext());
int textViewId = 100 + position;
//previousTextViewId = textViewId;
textView.setId(textViewId);
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(0, WRAP_CONTENT);
layoutParams.rightToRight = PARENT_ID;
layoutParams.leftToLeft = guideline_60.getId(); //Vertical GuideLine of 60%
layoutParams.rightMargin = 8;
textView.setLayoutParams(layoutParams);
if (Build.VERSION.SDK_INT < 23)
{
textView.setTextAppearance(getContext(), R.style.textStyle);
}
else
{
textView.setTextAppearance(R.style.textStyle);
}
textView.setBackgroundColor(backgroundColor);
textView.setText(categoryName);
textView.setGravity(Gravity.CENTER);
//markerLayout is the ConstraintLayout
markerLayout.addView(textView, position);
set.clone(markerLayout);
//set.addToVerticalChain(textView.getId(),previousTextViewId,PARENT_ID);
set.connect(textView.getId(), ConstraintSet.TOP, markerLayout.getId(), ConstraintSet.TOP, 60);
set.applyTo(markerLayout);
}
我期待看到这样的东西 -
【问题讨论】:
标签: android android-layout android-constraintlayout