android - 在android中动态添加n个进度条

标签 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);
}

它应该看起来像这样:

enter image description here

但看起来像这样:/

enter image description here

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


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

enter image description here

最佳答案

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,您可以通过访问进度字段更改值

关于android - 在android中动态添加n个进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59158701/

相关文章:

android - 谷歌地图实用程序 : how to get all markers from ClusterManager<? >?

java - 将 json 值键配对转换为 hashmap

android - 使用 Android MediaStore 管理媒体

java - Android 6.0 运行时异常 : Fail to connect to camera service

java - 使用 AES256 对称分组密码的图像加密/解密

java - Android Image Masking 未按预期呈现

android - 游标适配器数据绑定(bind)问题

android - 添加 View 时工具栏标题消失

Android Eclipse android.view.InflateException 二进制 XML 文件第 2 行错误膨胀类

android - Kivy:在 Android 上无延迟地播放声音