2017-12-02 106 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

有没有办法改变颜色? –