Android开发高级组件--ProgressBar(进度条组件)

[size=medium][color=gray]1、进度条组件ProgressBar是在某些操作的进度发展情况指示器,为用户呈现操作的进度,操作完成后进度条被填满。进度条能够直观的帮助用户了解等待一定时间的操作所需要的时间。

2、其层次结构如下:
java.lang.Object
android.view.View
android.widget.ProgressBar

3、Android系统提供的进度条有对话框进度条、标题进度条和水平进度条。进度条的样式也分为水平进度条和圆形转动进度条。圆形进度条表示一个运转的过程,如发短信、连接网络等,表示一个过程正在进行中,一般在xml中定义就可以了:[/color][/size]
<ProgressBar
android:id="@+id/pBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>

[size=medium][color=gray] 此时没有指定它的风格,默认是圆形的,一直会旋转的进度条,如果希望是超大号圆形进度条,只需设置style属性,即style="?android:attr/progressBarStyleLarge",小号的对应风格为style="?android:attr/progressBarStyleSmall",标题型对应的风格为style="?android:attr/progressBarStyleSmallTitle",水平进度条对应的为style="?android:attr/progressBarStyleHorizontal"。

4、标题栏进度条创建步骤
1、调用Activity程序的requestWindowFeatures()方法获取进度条
例如:requestWindowFeature(Window.FEATURE_PROGRESS);
2、调用Activity程序的setProgressBarIndeterminateVisibility()方法,显示进度条对话框
例如:setProgressBarVisibility(true);
3、设置进度值
例如:setProgress(myProgressBar.getProgress()*100);
setSecondaryProgress(myProgressBar.getSecondaryProgress()*100);

5、水平进度条创建过程
1、在布局文件中声明ProgressBar:[/color][/size]
   <ProgressBar
android:id="@+id/pBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:progress="50"
android:secondaryProgress="70"/>

[size=medium][color=gray]2、在Activity中获得ProgressBar实例:
private ProgressBar myProgressBar;
myProgressBar = (ProgressBar) findViewById(R.id.pBar);
3、调用Progress的incrementProgressBy()方法增加和减少进度
myProgressBar.incrementProgressBy(5);
myProgressBar.incrementSecondaryProgress(5);

6、使用实例:
新建progressbar_xml.xml文件:[/color][/size]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="大号进度条"/>

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
style="?android:attr/progressBarStyleLarge"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="小号进度条"/>

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
style="?android:attr/progressBarStyleSmall"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="水平进度条"/>

<ProgressBar
android:id="@+id/progress_horizontal"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:max="200"
android:progress="50"
android:secondaryProgress="75"/>

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add"
android:text="增加"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dec"
android:text="减少"/>

</LinearLayout>

</LinearLayout>

[size=medium][color=gray] 新建ProgressBarActivity.java[/color][/size]
package xiao.fuyan.testapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;

/**
* Created by xiao on 2017/1/11.
*/
public class ProgressBarActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progressbar_xml);
//创建标题栏进度条
requestWindowFeature(Window.FEATURE_PROGRESS);
//设置标题栏进度条可见
setProgressBarVisibility(true);

//获取水平进度条
final ProgressBar progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);
setProgress(progressHorizontal.getProgress() * 100);
setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);

Button button = (Button) findViewById(R.id.add);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//每次增加5
progressHorizontal.incrementProgressBy(5);
setProgress(progressHorizontal.getProgress() * 100);
}
});

button = (Button) findViewById(R.id.dec);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressHorizontal.incrementProgressBy(-5);
setProgress(progressHorizontal.getProgress() * 100);
}
});

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值