在TextViews数组中,仅显示最后一个TextView

时间:2017-05-19 06:50:39

标签: android arrays textview android-constraintlayout

我正在尝试显示一个TextViews数组,但只显示最后一个TextView。我已经检查了根据i值移动约束边距并发现它们不在彼此的顶部....只显示最后一个。任何人都有任何想法发生了什么?

int baseID = 123;
ConstraintLayout parent = (ConstraintLayout) findViewById(R.id.my_activity_view);
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

TextView[] TV = new TextView[5];
for (int i = 0; i < 5; i++) {
    TV[i] = new TextView(this);

    TV[i].setId(baseID + i);
    TV[i].setLayoutParams(params);
    TV[i].setText("TEST TEXT "+i);
    parent.addView(TV[i]);

    ConstraintSet constraintSet = new ConstraintSet();
    constraintSet.clone(parent);

    constraintSet.connect(TV[i].getId(), ConstraintSet.TOP, R.id.topTV, ConstraintSet.BOTTOM);
    if (i > 0) constraintSet.connect(TV[i].getId(), ConstraintSet.LEFT, TV[i - 1].getId(), ConstraintSet.RIGHT);
    constraintSet.applyTo(parent);
}

1 个答案:

答案 0 :(得分:0)

我终于通过将ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);置于for循环中来实现它。最终代码如下:

int baseID = 123;
ConstraintLayout parent = (ConstraintLayout) findViewById(R.id.my_activity_view);

TextView[] TV = new TextView[5];
for (int i = 0; i < CueList.length; i++) {

    ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);

    TV[i] = new TextView(this);

    TV[i].setId(baseID + i);
    TV[i].setLayoutParams(params);
    TV[i].setText("TEST TEXT "+i);
    parent.addView(TV[i]);

    ConstraintSet constraintSet = new ConstraintSet();
    constraintSet.clone(parent);

    constraintSet.connect(TV[i].getId(), ConstraintSet.TOP, R.id.cuelist, ConstraintSet.BOTTOM);
    if (i > 0) constraintSet.connect(TV[i].getId(), ConstraintSet.LEFT, TV[i - 1].getId(), ConstraintSet.RIGHT);
    constraintSet.applyTo(parent);
}