我们如何在Android中添加任意数量的ProgressBars?

时间:2017-03-18 20:26:38

标签: java android

如果我们从EditText收到输入号码。我们如何创建那么多的ProgressBars?例如,如果输入为5. 5,则应创建progressBar小部件。

2 个答案:

答案 0 :(得分:1)

  1. activity_main.xml 中,在scrollView
  2. 中添加linearLayout
    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <LinearLayout
            android:id="@+id/activity_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"/>
    </ScrollView>
    
    1. MainActivity.java 中添加此代码,将progressBar添加到linearLayout

      int wantedNumber = 5; //get that number from the editText.
      for (int i = 0; i < wantedNumber; i++) {
          ProgressBar bar = new ProgressBar(this);
          bar.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
          LinearLayout linearLayout;
          linearLayout.addView(bar);
      }
      
    2. 我只提到了重要的部分,如果你遇到任何问题,请告诉我。

答案 1 :(得分:0)

//Progress bar array.
final ProgressBar[] bars = new ProgressBar[numTAs+1];

//Create and initialize the Progress Bar array.
for(int i = 0; i < numTAs + 1; i++){
   //ProgressBar bar = new ProgressBar(this);
   int style = android.R.style.Widget_ProgressBar_Horizontal;
   ProgressBar bar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
   bars[i] = bar;
}

//Build Adapter
ArrayAdapter<ProgressBar> adapter = new ArrayAdapter<ProgressBar>(this, R.layout.layout, bars ){

            @Override
            public View getView(int position, View convertView, ViewGroup parent){

                return bars[position];
            }
        };

        ListView list = (ListView) findViewById(R.id.listView);
        list.setAdapter(adapter);

    }