将ProgressBar以编程方式添加到线性布局中。

huangapple go评论63阅读模式
英文:

Add to Linear Layout ProgressBar programatically

问题

// 我想在复选框的右侧添加一个进度条,复选框是以程序方式创建的

// 这是我以程序方式创建复选框的方法

private void setCheckBoxesCelle(){
    int id_ord_trasp = celle_bsm.getOrdTrasp();
    ArrayList<String> celle_motrice = celle_bsm.getCelleAutomezzo(id_ord_trasp,false,true);
    ArrayList<String> celle_rimorchio = celle_bsm.getCelleAutomezzo(id_ord_trasp,true,true);
    HashMap<String, CheckBox> hm = new HashMap();
    Resources r = getResources();
    float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 110, r.getDisplayMetrics());
    float width_short = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 70, r.getDisplayMetrics());
    float height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 110, r.getDisplayMetrics());
    float height_short = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 70, r.getDisplayMetrics());
    float text_size = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 35, r.getDisplayMetrics());
    float text_size_short = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, r.getDisplayMetrics());
    HashMap<String, TextView> hmt = new HashMap();

    for(int i= 0 ; i<celle_motrice.size(); i++){
        CheckBox cb = new CheckBox(this);
        TextView txt = new TextView(this);
        lmcc = celle_bsm.getLastLogMovimentiCelleCisterne(celle_motrice.get(i));
        LinearLayout appoggio = new LinearLayout(this);
        if(sv_celle_lbl.getTextSize() != getResources().getDimension(R.dimen.fab_margin)){

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int) width, (int) height);
            lp.setMargins(10,5,10,5);
            cb.setLayoutParams(lp);
            cb.setText(celle_motrice.get(i));
            cb.setBackground(ContextCompat.getDrawable(this, R.drawable.checkbox_background));
            cb.setButtonDrawable(null);
            cb.setGravity(Gravity.CENTER);
            cb.setTextSize(text_size);
            cb.setClickable(false);
        }else {

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int)width_short, (int)height_short);
            lp.setMargins(10,5,10,5);
            cb.setLayoutParams(lp);
            cb.setText(celle_motrice.get(i));
            cb.setBackground(ContextCompat.getDrawable(this, R.drawable.checkbox_background));
            cb.setButtonDrawable(null);
            cb.setGravity(Gravity.CENTER);
            cb.setTextSize(text_size_short);
            cb.setClickable(false);
        }
        hm.put(celle_motrice.get(i),cb);

        appoggio.setOrientation(LinearLayout.VERTICAL);
        txt.setGravity(Gravity.CENTER);

        appoggio.addView(cb);
        appoggio.addView(txt);
        hmt.put(celle_motrice.get(i),txt);

        sv_linear_layout_celle.addView(appoggio);
        cb.getRootView();
    }
}

// 我不知道是否可能,我尝试过添加但只是添加了一个空格

// 我想将进度条添加到复选框的右侧,就像图片中的样子

// 如果您需要更多细节,请与我联系

// 现在我想在复选框附近添加进度条,就像这样

这是您提供的代码的翻译部分。如果您需要其他帮助,请随时告诉我。

英文:

I would like to add a progressbar to the right of my checkbox, checkboxes are created programatically

this is how i create the checkboxes programatically

private void setCheckBoxesCelle(){
int id_ord_trasp = celle_bsm.getOrdTrasp();
ArrayList&lt;String&gt; celle_motrice = celle_bsm.getCelleAutomezzo(id_ord_trasp,false,true);
ArrayList&lt;String&gt; celle_rimorchio = celle_bsm.getCelleAutomezzo(id_ord_trasp,true,true);
HashMap&lt;String, CheckBox&gt; hm = new HashMap();
Resources r = getResources();
float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 110, r.getDisplayMetrics());
float width_short = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 70, r.getDisplayMetrics());
float height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 110, r.getDisplayMetrics());
float height_short = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 70, r.getDisplayMetrics());
float text_size = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 35, r.getDisplayMetrics());
float text_size_short = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, r.getDisplayMetrics());
HashMap&lt;String, TextView&gt; hmt = new HashMap();
for(int i= 0 ; i&lt;celle_motrice.size(); i++){
CheckBox cb = new CheckBox(this);
TextView txt = new TextView(this);
lmcc = celle_bsm.getLastLogMovimentiCelleCisterne(celle_motrice.get(i));
LinearLayout appoggio = new LinearLayout(this);
if(sv_celle_lbl.getTextSize() != getResources().getDimension(R.dimen.fab_margin)){
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int) width, (int) height);
lp.setMargins(10,5,10,5);
cb.setLayoutParams(lp);
cb.setText(celle_motrice.get(i));
cb.setBackground(ContextCompat.getDrawable(this, R.drawable.checkbox_background));
cb.setButtonDrawable(null);
cb.setGravity(Gravity.CENTER);
cb.setTextSize(text_size);
cb.setClickable(false);
}else {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int)width_short, (int)height_short);
lp.setMargins(10,5,10,5);
cb.setLayoutParams(lp);
cb.setText(celle_motrice.get(i));
cb.setBackground(ContextCompat.getDrawable(this, R.drawable.checkbox_background));
cb.setButtonDrawable(null);
cb.setGravity(Gravity.CENTER);
cb.setTextSize(text_size_short);
cb.setClickable(false);
}
hm.put(celle_motrice.get(i),cb);
appoggio.setOrientation(LinearLayout.VERTICAL);
txt.setGravity(Gravity.CENTER);
appoggio.addView(cb);
appoggio.addView(txt);
hmt.put(celle_motrice.get(i),txt);
sv_linear_layout_celle.addView(appoggio);
cb.getRootView();
}

i dont know if it's possible, i've tried to add but simply add a blank space

i would add the progressbar to the right of the checkboxes like the photo.

if you need more details please contact me

now i would like add the progressbar near the checkboxes like this
enter image description here

答案1

得分: 0

请尝试下面的代码,并告诉我是否有效:

private void setCheckBoxesCelle(){
    int id_ord_trasp = celle_bsm.getOrdTrasp();
    ArrayList<String> celle_motrice = celle_bsm.getCelleAutomezzo(id_ord_trasp,false,true);
    ArrayList<String> celle_rimorchio = celle_bsm.getCelleAutomezzo(id_ord_trasp,true,true);
    HashMap<String, CheckBox> hm = new HashMap();
    Resources r = getResources();
    float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 110, r.getDisplayMetrics());
    float height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 110, r.getDisplayMetrics());
    float text_size = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 35, r.getDisplayMetrics());
    HashMap<String, TextView> hmt = new HashMap();

    for(int i= 0 ; i<celle_motrice.size(); i++){
        CheckBox cb = new CheckBox(this);
        TextView txt = new TextView(this);
        lmcc = celle_bsm.getLastLogMovimentiCelleCisterne(celle_motrice.get(i));
        LinearLayout appoggio = new LinearLayout(this);
        LinearLayout horizontalLayout = new LinearLayout(this);
        horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);

        ProgressBar progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
        progressBar.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        if(sv_celle_lbl.getTextSize() != getResources().getDimension(R.dimen.fab_margin)){

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int) width, (int) height);
            lp.setMargins(10,5,10,5);
            cb.setLayoutParams(lp);
            cb.setText(celle_motrice.get(i));
            cb.setBackground(ContextCompat.getDrawable(this, R.drawable.checkbox_background));
            cb.setButtonDrawable(null);
            cb.setGravity(Gravity.CENTER);
            cb.setTextSize(text_size);
            cb.setClickable(false);
        }else {

            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int)width, (int)height);
            lp.setMargins(10,5,10,5);
            cb.setLayoutParams(lp);
            cb.setText(celle_motrice.get(i));
            cb.setBackground(ContextCompat.getDrawable(this, R.drawable.checkbox_background));
            cb.setButtonDrawable(null);
            cb.setGravity(Gravity.CENTER);
            cb.setTextSize(text_size);
            cb.setClickable(false);
        }
        hm.put(celle_motrice.get(i),cb);

        appoggio.setOrientation(LinearLayout.VERTICAL);
        txt.setGravity(Gravity.CENTER);

        horizontalLayout.addView(cb);
        horizontalLayout.addView(progressBar);

        appoggio.addView(horizontalLayout);
        appoggio.addView(txt);
        hmt.put(celle_motrice.get(i),txt);

        sv_linear_layout_celle.addView(appoggio);
        cb.getRootView();

    }
}

记得在需要时调用 progressBar.setProgress(int progress) 来更新进度。

英文:

Try the code below and let me know if it works-

    private void setCheckBoxesCelle(){
        int id_ord_trasp = celle_bsm.getOrdTrasp();
        ArrayList&lt;String&gt; celle_motrice = celle_bsm.getCelleAutomezzo(id_ord_trasp,false,true);
        ArrayList&lt;String&gt; celle_rimorchio = celle_bsm.getCelleAutomezzo(id_ord_trasp,true,true);
        HashMap&lt;String, CheckBox&gt; hm = new HashMap();
        Resources r = getResources();
        float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 110, r.getDisplayMetrics());
        float height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 110, r.getDisplayMetrics());
        float text_size = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 35, r.getDisplayMetrics());
        HashMap&lt;String, TextView&gt; hmt = new HashMap();
    
        for(int i= 0 ; i&lt;celle_motrice.size(); i++){
            CheckBox cb = new CheckBox(this);
            TextView txt = new TextView(this);
            lmcc = celle_bsm.getLastLogMovimentiCelleCisterne(celle_motrice.get(i));
            LinearLayout appoggio = new LinearLayout(this);
            LinearLayout horizontalLayout = new LinearLayout(this);
            horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
    
            ProgressBar progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
            progressBar.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    
            if(sv_celle_lbl.getTextSize() != getResources().getDimension(R.dimen.fab_margin)){
    
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int) width, (int) height);
                lp.setMargins(10,5,10,5);
                cb.setLayoutParams(lp);
                cb.setText(celle_motrice.get(i));
                cb.setBackground(ContextCompat.getDrawable(this, R.drawable.checkbox_background));
                cb.setButtonDrawable(null);
                cb.setGravity(Gravity.CENTER);
                cb.setTextSize(text_size);
                cb.setClickable(false);
            }else {
    
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int)width, (int)height);
                lp.setMargins(10,5,10,5);
                cb.setLayoutParams(lp);
                cb.setText(celle_motrice.get(i));
                cb.setBackground(ContextCompat.getDrawable(this, R.drawable.checkbox_background));
                cb.setButtonDrawable(null);
                cb.setGravity(Gravity.CENTER);
                cb.setTextSize(text_size);
                cb.setClickable(false);
            }
            hm.put(celle_motrice.get(i),cb);
    
            appoggio.setOrientation(LinearLayout.VERTICAL);
            txt.setGravity(Gravity.CENTER);
    
            horizontalLayout.addView(cb);
            horizontalLayout.addView(progressBar);
    
            appoggio.addView(horizontalLayout);
            appoggio.addView(txt);
            hmt.put(celle_motrice.get(i),txt);
    
            sv_linear_layout_celle.addView(appoggio);
            cb.getRootView();
    
        }
    }

Remember to call progressBar.setProgress(int progress) to update the progress whenever needed.

huangapple
  • 本文由 发表于 2023年5月29日 16:35:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定