ProgressBar 进度条
在某项延续性工作的进展过程中为了不让用户觉得程序死掉了,需要有个活动的进度条,表示此过程正在进行中。Android中使用ProgressBar来实现这一功能:
1、简单的进度条
在xml中添加:
<ProgressBar android:id= @+id/ProgressBar01
android:layout_width= wrap_content
android:layout_height= wrap_content >
</ProgressBar>
就可以看到下面,圆形的、大大的圈圈:
2、各种各样的圆圈:
注意标题栏上也有一个进度条,下面是代码:
package android.basic.lesson11;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class MainHelloProgressBar extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//在标题条里放置进度条。请求窗口特色风格,这里设置成不明确的进度风格
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
//设置标题栏中的不明确的进度条是否可以显示,当你需要表示处理中的时候设置为True,处理完毕后设置为false
setProgressBarIndeterminateVisibility(true);
}
}
下面试main.xml中的代码,大家注意黑体字部分的内容,看看不同样式的不同的表现:
<?xml version= 1.0encoding= utf-8 ?>
<LinearLayout xmlns:android= http://schemas.android.com/apk/res/android
android:orientation= vertical
android:background= #003399
android:layout_width= fill_parent
android:layout_height= fill_parent
>
<ProgressBar android:id= @+id/ProgressBar01
android:layout_width= wrap_content
android:layout_height= wrap_content >
</ProgressBar>
<ProgressBar android:id= @+id/ProgressBar02
style= ?android:attr/progressBarStyleLarge大圆圈
android:layout_width= wrap_content
android:layout_height= wrap_content >
</ProgressBar>
<ProgressBar android:id= @+id/ProgressBar03
style= ?android:attr/progressBarStyleSmall小圆圈
android:layout_width= wrap_content
android:layout_height= wrap_content >
</ProgressBar>
<ProgressBar android:id= @+id/ProgressBar03
style= ?android:attr/progressBarStyleSmallTitle标题条的样式
android:layout_width= wrap_content
android:layout_height= wrap_content >
</ProgressBar>
</LinearLayout>
3、长条状的进度条
<ProgressBar android:id= @+id/ProgressBar04
style= ?android:attr/progressBarStyleHorizontal
android:layout_marginLeft= 10dp
android:layout_marginRight= 10dp
android:max= 100最大刻度按100算
android:progress= 30第一进度是30
android:secondaryProgress= 80第二进度是80
android:layout_width= fill_parent进度条的显示长度是铺满父窗口
android:layout_height= wrap_content >
</ProgressBar>
<?xml version= 1.0encoding= utf-8 ?>
<LinearLayout xmlns:android= http://schemas.android.com/apk/res/android
android:orientation= vertical
android:background= #003399
android:layout_width= fill_parent
android:layout_height= fill_parent
>
<ProgressBar android:id= @+id/ProgressBar04
style= ?android:attr/progressBarStyleHorizontal
android:layout_marginTop= 10dp
android:layout_marginLeft= 10dp
android:layout_marginRight= 10dp
android:max= 100
android:progress= 30
android:secondaryProgress= 80
android:layout_width= fill_parent
android:layout_height= wrap_content >
</ProgressBar>
</LinearLayout>
java代码:
package android.basic.lesson11;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class MainHelloProgressBar extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置窗口进度条特性风格
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
//设置进度条可见性
setProgressBarVisibility(true);
//设置进度条进度值,要乘以100的
setProgress(60*100);
setSecondaryProgress(80*100);
}
}
运行效果:
同学们可以使用下面的代码对,进度条进行一些操作练习:
private ProgressBar pb;//定义ProgressBar
pb = (ProgressBar) findViewById(R.id.ProgressBar01);
pb.incrementProgressBy(5);//ProgressBar进度值增加5
pb.incrementProgressBy(-5);//ProgressBar进度值减少5
pb.incrementSecondaryProgressBy(5);//ProgressBar第二个进度条 进度值增加5
pb.incrementSecondaryProgressBy(-5);//ProgressBar第二个进度条 进度值减少5