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

问题描述 投票:0回答:2

我正在尝试显示 TextView 数组,但只显示最后一个 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);
}
android arrays textview android-constraintlayout
2个回答
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);
}

0
投票

谢谢兄弟,非常有帮助 我的滑块点代码

public void setIndicator(){

        dots = new ImageView[4];
        ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.WRAP_CONTENT
        );
        layoutParams.setMargins(0,0,0,0);

        for (int i = 0; i < dots.length; i++) {

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

            dots[i] = new ImageView(this);

            dots[i].setId(123 + i);
            dots[i].setLayoutParams(params);
            dots[i].setImageDrawable(ContextCompat.getDrawable(
                    getApplicationContext(),
                    R.drawable.page_inactive

            ));
            mDotLayout.addView(dots[i]);

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

            if(i==0){
                constraintSet.connect(dots[i].getId(), ConstraintSet.LEFT, R.id.Constraint3, ConstraintSet.LEFT);
            }
            else {
                constraintSet.connect(dots[i-1].getId(), ConstraintSet.RIGHT, dots[i].getId(), ConstraintSet.LEFT);
                constraintSet.connect(dots[i].getId(), ConstraintSet.LEFT, dots[i - 1].getId(), ConstraintSet.RIGHT);
            }
            if(i==dots.length-1){
                constraintSet.connect(dots[i].getId(), ConstraintSet.RIGHT, R.id.Constraint3, ConstraintSet.RIGHT);
            }
            constraintSet.applyTo(mDotLayout);
        }
    }

    public void setCurrentIndicator(int position){
        System.out.println(position);
        int sz = 4;
        for(int i = 0 ; i < sz ; i++)
        {

            ImageView imageView = (ImageView) mDotLayout.getChildAt(i);
            if(i==position)
            {
                imageView.setImageDrawable(
                        ContextCompat.getDrawable(getApplication(),R.drawable.page_active)
                );
            }else{
                imageView.setImageDrawable(
                        ContextCompat.getDrawable(getApplication(),R.drawable.page_inactive)
                );
            }
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.