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