android动态添加n个进度条

Add n progressbars dynamically in android

我有这个代码:

private void addPartiesBar(ArrayList<PoliticalPartyModel> partyModelArrayList) {
    ConstraintLayout constraintLayout = findViewById(R.id.constraintProgressBar);
    ConstraintSet set = new ConstraintSet();
    int voteBarId = 1000;


                                         //has 3 entries
    for (PoliticalPartyModel singlePartyModel : partyModelArrayList) {
        ProgressBar voteBar = new ProgressBar(this/*, null, android.R.style.Widget_ProgressBar_Horizontal*/);
        voteBarId++;
        voteBar.setId(voteBarId);

        ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, 100);
        layoutParams.startToStart = PARENT_ID;
        layoutParams.endToEnd = PARENT_ID;
        voteBar.setLayoutParams(layoutParams);

        constraintLayout.addView(voteBar, 0);

        set.clone(constraintLayout);
        set.connect(voteBarId, ConstraintSet.TOP, constraintLayout.getId(), ConstraintSet.TOP, 100);
    }
    set.applyTo(constraintLayout);
}

它应该是这样的:

但看起来像这样:/

我做错了什么? 为什么只显示2个进度条?所有进度条(上面的进度条粗一点)都在位置1。抱歉我的英语很差。我希望你知道我想要什么:)


有了 Dak28 的回答,它就像一个魅力。但是我无法设置进度条的高度。

ProgressBar voteBar = new ProgressBar(this/*, null, 
                      android.R.style.Widget_ProgressBar_Horizontal*/);

为什么要评论横向样式部分?

不应该是这样吗?

  ProgressBar voteBar = new ProgressBar(this, null, 
                      android.R.style.Widget_ProgressBar_Horizontal);

要创建水平样式使用此代码,我没有编辑太多样式:

for(x in 0..3) {

        var voteBar =
            ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal)

        val layoutParams = LinearLayout.LayoutParams(
            400, // whatever value you need
            ViewGroup.LayoutParams.WRAP_CONTENT
        )

        voteBar.isIndeterminate = false
        voteBar.visibility = View.VISIBLE
        voteBar.progress = 50

        linearLayout.addView(voteBar)
    }

我没有添加 id 但如果你添加 id 你可以通过访问进度字段更改值