ProgressBar的简单使用

ProgressBar滚动体在安卓程序中使用也计较多。

ProgressBar的几个常用属性和方法

android:max="200"    滚动条最大值
android:progress="0" 滚动条当前值
android:visibility="visible"  滚动条是否可见

setProgress(int) 设置当前值


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text"
        android:textSize="25sp" />
    
    <EditText
        android:id="@+id/value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:layout_toRightOf="@id/text"
        android:textSize="15sp" />
    
    <!-- 定义滚动条
    	sytle滚动条样式:progressBarStyleHorizontal一个长条形
    	max 滚动条最大值
    	progress 滚动条当前值
    	visibility 是否可见
     -->
    <ProgressBar
        android:id="@+id/firstBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/text"
        android:layout_below="@+id/text"
        android:max="200"
        android:maxHeight="48dp"
        android:minHeight="48dp"
        android:progress="0"
        android:visibility="visible" />

    <TextView
        android:id="@+id/text2"
        android:layout_below="@id/firstBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text"
        android:textSize="25sp" />
    
    <EditText
        android:id="@+id/value2"
        android:layout_below="@id/firstBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        android:layout_toRightOf="@id/text"
        android:textSize="15sp" />
    
    <!-- 定义滚动条
    	sytle滚动条样式:progressBarStyleLarge一个大圆形样式
     -->
    <ProgressBar
        android:id="@+id/firstBar2"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/text2"
        android:layout_below="@+id/text2"
        android:max="200"
        android:progress="0"
        android:visibility="visible" />
    
     <!-- 定义一个标题栏的滚动条
     -->
    <ProgressBar
        android:id="@+id/firstBar3"
        style="?android:attr/progressBarStyleSmallTitle"
        android:layout_width="200dp"
        android:layout_height="wrap_content"/>
</RelativeLayout>

MainActivity:

处理动态加载滚动条

protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//请求设置窗口标题栏滚动条
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.activity_main);
		pb = (ProgressBar)findViewById(R.id.firstBar);
		value = (EditText)findViewById(R.id.value);
		//设置滚动条可见
		setProgressBarIndeterminateVisibility(true);
		
		//创建一个Handler
		mHandler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				//处理消息
				switch (msg.what) {
					case MSG:
						//设置滚动条和text的值
						pb.setProgress(pro);
						value.setText(Integer.toString(pro));
						break;
					default:
						break;
				}
			}
		};
		start();
	}
	
	private void start()
	{
		new Thread(new Runnable() {
			@Override
			public void run() {
				int max = pb.getMax();
				try {
					//子线程循环间隔消息
					while (pro < max) {
						pro += 10;
						Message msg = new Message();
						msg.what = MSG;
						mHandler.sendMessage(msg);
						Thread.sleep(1000);
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

效果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值