2017-03-03 21 views
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); 
    } 

我期待看到这样的事情 -

enter image description here

回答

4

你是说所有textviews的顶部在父的顶部连接:

set.connect(textView.getId(), ConstraintSet.TOP, 
     markerLayout.getId(), ConstraintSet.TOP, 60); 

你想说,一个顶部连接到另一个的底部 - 例如:

set.connect(textView.getId(), ConstraintSet.TOP, 
    previousTextView.getId(), ConstraintSet.BOTTOM, 60); 

你的方法叫做方法吗?

+0

我试图概括我的代码,因此将该方法重命名为“方法”:)。如果(位置== 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

+0

抱歉无法设计代码 – sku

+1

我的糟糕的解决方案工作得很好。我有previousTextViewId = textViewId;在set.Connect。之前。我把它移到下面,它工作得很好。谢谢Nick。 – sku

相关问题