2017-12-02 92 views

回答

1

要麼你可以使用這樣

<ProgressBar 
    android:id="@+id/simpleProgressBar" 
    style="@android:style/Widget.Holo.Light.ProgressBar.Horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="70dp" 
    android:max="100" 
    android:progress="0" 
    android:indeterminateOnly="true" /> 

默認進度條或很少有庫也https://github.com/DreaminginCodeZH/MaterialProgressBar你可以嘗試都和使用。

你可以用setProgress()方法設置progess。

樣品的完整代碼: - 按照這個

public class MainActivity extends AppCompatActivity { 

int progress = 0; 
ProgressBar simpleProgressBar; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // initiate progress bar and start button 
    simpleProgressBar = (ProgressBar) findViewById(R.id.simpleProgressBar); 
    Button startButton = (Button) findViewById(R.id.startButton); 
    // perform click event on button 
    startButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // call a function 
      setProgressValue(progress); 

     } 
    }); 
} 

private void setProgressValue(final int progress) { 

    // set the progress 
    simpleProgressBar.setProgress(progress); 
    // thread is used to change the progress value 
    Thread thread = new Thread(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      setProgressValue(progress + 10); 
     } 
    }); 
    thread.start(); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

}

要更新進度的顏色

創建繪製一個XML文件 - > custom_progress.xml

<item> 
    <shape> 
     <gradient 
      android:endColor="#fff" 
      android:startColor="#1f1" 
      android:useLevel="true" /> 
    </shape> 
</item> 

然後我們需要添加到我們的陸侃欄這個像下面,它會工作: -

<ProgressBar 
    android:id="@+id/simpleProgressBar" 
    style="@style/Widget.AppCompat.ProgressBar.Horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="70dp" 
    android:max="100" 
    android:progress="0" 
    android:progressDrawable="@drawable/custom_progress" /> 
+0

無法設置進度 –

+0

檢查我添加了一個完整的工作示例類使用此並嘗試 – Thunder

+0

有沒有辦法改變顏色? –