0
我试图在约束布局的运行时将TextViews添加到另一个之下。但我总是只有一个文本视图,其余的隐藏在它后面。我尝试了几件事情,包括链接视图,但似乎没有任何工作。Android ConstraintLayout:如何将动态视图添加到另一个下方
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);
}
我期待看到这样的事情 -
我试图概括我的代码,因此将该方法重命名为“方法”:)。如果(位置== 0) Timber.d(“位置 - ”+位置),我仍然有与下面的代码相同的问题; (textView.getId(),ConstraintSet.TOP,markerLayout.getId(),ConstraintSet.TOP,16); } else Timber.d(“Position - ”+ position); (textView.getId(),ConstraintSet.TOP,previousTextViewId,ConstraintSet.BOTTOM,16); } – sku
抱歉无法设计代码 – sku
我的糟糕的解决方案工作得很好。我有previousTextViewId = textViewId;在set.Connect。之前。我把它移到下面,它工作得很好。谢谢Nick。 – sku