Android UI之ProgressBar(进度条)

进度条是一个很实用的组件,一般用来显示用户某个耗时操作的进度百分比,首先来看一下Android支持的几种风格的进度条:

style="@android:style/Widget.ProgressBar.Inverse"   普通大小进度条

style="@android:style/Widget.ProgressBar.Large" 大进度条

style="@android:style/Widget.ProgressBar.Large.Inverse" 大进度条

style="@android:style/Widget.ProgressBar.Small"  小进度条
style="@android:style/Widget.ProgressBar.Small.Inverse"  小进度条

style="@android:style/Widget.ProgressBar.Horizontal" 水平进度条

在样式文件中分别添加这六个不同样式的进度条,看看在模拟器上的效果


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="进度条演示" />

    <ProgressBar 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:max="1000"
        android:progress="100"
		android:id="@+id/progressbar1"
        />
    
    <ProgressBar 
        style="@android:style/Widget.ProgressBar.Inverse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="20"
        />
        <ProgressBar 
        style="@android:style/Widget.ProgressBar.Large"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="20"
        />
     <ProgressBar 
        style="@android:style/Widget.ProgressBar.Large.Inverse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="20"
        />
    <ProgressBar 
        style="@android:style/Widget.ProgressBar.Small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="20"
        />
    <ProgressBar 
        style="@android:style/Widget.ProgressBar.Small.Inverse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="20"
        />    
    <ProgressBar 
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_marginTop="30dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="1000"
        android:progress="500"
        android:secondaryProgress="300"
		android:id="@+id/progressbar2"
        />
</LinearLayout>
在模拟器上的效果:


从上到下的六个进度条第一个没有声明样式,他默认的为和第二个一样是普通大小的进度条,第三个第四个分别是大进度条,第五个第六个是小进度条,最后一个是水平进度条

除了样式ProgressBar的几个常用属性:

android::max  设置进度条的最大值(最后一个水平进度条的设置为1000)

android::progress  设置当前的进度(最后一个水平进度条设置当前进度为500)

android::secondaryProgress 设置第二进度条


模拟一个进度条的进度变动:

package cn.class3g.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ProgressBar;

public class ProgressBarDemo extends Activity{
    
	ProgressBar progressbar = null;
	static int i = 0;
	int progressbarMax = 0;

	Handler handler = new Handler();
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progressbar_layout);
        
        findViews();
    }

	private void findViews() {
		progressbar = (ProgressBar) this.findViewById(R.id.progressbar2);
		progressbar.setMax(1000);
		progressbarMax = progressbar.getMax();
		
		
		//利用线程控制进度条的进度
		new Thread(new Runnable(){
			
			public void run(){
				while(i< progressbarMax){
					//doWord()模拟一个任务的进度
					i=doWork();
					
					handler.post(new Runnable(){
						public void run(){
							progressbar.setProgress(i);
						}
					});
					try {
						Thread.sleep(50);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}).start();

	}
	
	public int doWork(){
		Log.d("TAG", String.valueOf(i));
		return ++i;
	}
}
效果:


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值