简单的使用下ProgressBar...
1.设置进度条的可见性通过setVisibility(int v)方法.注意v的取值.如下描述:
View 的setVisibility有三个值可以设置:
VISIBLE--->可见
INVISIBLE--->不可见,但这个View在ViewGroupt中仍保留它的位置,不重新layout
GONE---->不可见,但这个View在ViewGroupt中不保留位置,重新layout,那后面的view就会取代他的位置。
2.设置进度条长度通过setProgress()方法.
创建项目步骤:
step1:新建android工程,命名为ProgressBarTest。
step2:设置布局文件main.xml
main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="@string/hello" /> 11 <!-- 12 设置水平的进度条 style="?android:attr/progressBarStyleHorizontal" 13 设置转圈的进度条style="?android:attr/progressBarStyle" 14 --> 15 <ProgressBar 16 android:id="@+id/firstprogress" 17 style="?android:attr/progressBarStyleHorizontal" 18 android:layout_width="200dp" 19 android:layout_height="wrap_content" 20 android:visibility="gone"/> 21 22 <ProgressBar 23 android:id="@+id/secondprogress" 24 style="?android:attr/progressBarStyle" 25 android:layout_width="200dp" 26 android:layout_height="wrap_content" 27 android:visibility="gone"/> 28 29 <Button 30 android:id="@+id/button" 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" 33 android:text="确定"/> 34 35 </LinearLayout>
step3:设计主类ProgressBarTest.java
ProgressBarTest
1 package com.cb.progressbar; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.View.OnClickListener; 7 import android.widget.Button; 8 import android.widget.ProgressBar; 9 10 public class ProgressBarTest extends Activity { 11 private ProgressBar mProgressBar1; 12 private ProgressBar mProgressBar2; 13 private Button mButton; 14 15 private int i = 0; 16 17 @Override 18 public void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.main); 21 22 mProgressBar1 = (ProgressBar)findViewById(R.id.firstprogress); 23 mProgressBar2 = (ProgressBar)findViewById(R.id.secondprogress); 24 mButton = (Button)findViewById(R.id.button); 25 26 mButton.setOnClickListener(new OnClickListener() { 27 28 @Override 29 public void onClick(View v) { 30 if (i==0) { 31 //设置进度条可见 32 mProgressBar1.setVisibility(View.VISIBLE); 33 mProgressBar2.setVisibility(View.VISIBLE); 34 } 35 else if (i < mProgressBar1.getMax()) { 36 //设置点击进度条时,进度条进度增加 37 mProgressBar1.setProgress(i); 38 //设置第二进度条(浅色的那条) 39 mProgressBar1.setSecondaryProgress(i+10); 40 mProgressBar2.setProgress(i); 41 } 42 else { 43 //当进度条走完时,设置进度条不可见 44 mProgressBar1.setVisibility(View.GONE); 45 mProgressBar2.setVisibility(View.GONE); 46 } 47 i = i+10; 48 } 49 }); 50 51 } 52 }
step4:执行结果如下