ProgressBar 进度条

设置动态进度条

  • activity_main.xml
 <!-- 布局文件 -->
<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>
  • MainActivity.java
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();
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容