ProgressBar 是一个展示进度的信息的控件,向用户显示某个比较耗时间的操作完成的百分比。因此进度条可以动态的显示进度,避免长时间地执行某个耗时操作,更好的提高用户界面的友好性。我们来简单学习他的用法
1。新建一个android项目ProgressBarTest,设置相应的参数,点击完成。参见下图。
初始化ProgressBar的资源信息以及两个Button按钮信息。其中ProgressBar的样式
style="?android:attr/progressBarStyleHorizontal" 表示水平ProgressBar
style="?android:attr/progressBarStyleSmallTitle" 标题型圆形ProgressBar
style="?android:attr/progressBarStyleSmall" 小号圆形ProgressBar
其中android:layout_width="fill_parent" 表示,控件宽度要与父节点一致
android:layout_height="wrap_content" 控件高度与自己内容自适应
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ProgressBar
android:id="@+id/ProgressBarT"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
/>
<Button
android:id="@+id/buttonOne"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="主进度增加10"/>
<Button
android:id="@+id/buttonTwo"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="主进度减5"/>
</LinearLayout >
ProgressBar属性介绍
@android:style/Widget.ProgressBar.Horizontal:水平进度条(可以显示刻度,常用)。
@android:style/Widget.ProgressBar.Small:小进度条。
@android:style/Widget.ProgressBar.Large:大进度条。
@android:style/Widget.ProgressBar.Inverse:不断跳跃、旋转画面的进度条。
@android:style/Widget.ProgressBar.Large.Inverse:不断跳跃、旋转动画的大进度条。
@android:style/Widget.ProgressBar.Small.Inverse:不断跳跃、旋转动画的小进度条。
@android:max:设置进度的最大值。
@android:progress:设置当前第一进度值。
@android:secondaryProgress:设置当前第二进度值。
@android:visibility:设置是否显示,默认显示。
4初始化主体函数信息,找到ProgressBar控件,Button控件,给Button控件增加OnClickListener (点击事件),通progressBarT.incrementProgressBy(int),
对应相应的Button对ProgressBar进行相应的进度展示,增加或者减少进度
private ProgressBar progressBarT;
private Button buttonOne;
private Button buttonTwo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progressBarT = (ProgressBar) findViewById(R.id.ProgressBarT);
buttonOne = (Button) findViewById(R.id.buttonOne);
buttonTwo = (Button) findViewById(R.id.buttonTwo);
buttonOne.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
setProgressBarVisibility(true);
progressBarT.incrementProgressBy(10);
}
});
buttonTwo.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
setProgressBarVisibility(true);
progressBarT.incrementProgressBy(-5);
}
});
}
:
我的同一篇文章,发表在http://jingyan.baidu.com/article/948f5924019b72d80ef5f954.html,感兴趣的同学可以看看其他系列