控件与三方的依赖

ProgressBar 进度条

2019-04-15  本文已影响1人  穿越平行宇宙

设置动态进度条

 <!-- 布局文件 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp" >

    <ProgressBar 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleInverse"/>
  
    <ProgressBar 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleLarge"/>
    
    <ProgressBar 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="40"
        style="?android:attr/progressBarStyleHorizontal"/>
    
    <ProgressBar 
        android:id="@+id/jd"  
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:max="100"
        android:progress="40"
        android:secondaryProgress="60"
        android:indeterminate="false"/>
    
    
</LinearLayout>
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //通过调用id的方法获得组件的对象
        final ProgressBar jd = (ProgressBar) findViewById(R.id.jd);
        
        //使用线程
        new Thread(){
            //使用线程的方法
            public void run() {
                // 用for循环设置进度条的进度
                // getProgress  已经缓存的进度   getMax 最大进度
                for (int i = jd.getProgress(); i < jd.getMax(); i++) {
                    //循环设置当前的进度值
                    jd.setProgress(i);
                    
                    //循环一次停止500ms
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            //开始使用线程,必须使用
        }.start();
    }
}
上一篇 下一篇

猜你喜欢

热点阅读