摘要:本文将带你了解Android应用开发之Android开发高级组件--ProgressBar(进度条组件),希望本文对大家学Android有所帮助
本文将带你了解Android应用开发之Android开发高级组件--ProgressBar(进度条组件),希望本文对大家学Android有所帮助
1、进度条组件ProgressBar是在某些操作的进度发展情况指示器,为用户呈现操作的进度,操作完成后进度条被填满。进度条能够直观的帮助用户了解等待一定时间的操作所需要的时间。
2、其层次结构如下:
java.lang.Object
android.view.View
android.widget.ProgressBar
3、Android系统提供的进度条有对话框进度条、标题进度条和水平进度条。进度条的样式也分为水平进度条和圆形转动进度条。圆形进度条表示一个运转的过程,如发短信、连接网络等,表示一个过程正在进行中,一般在xml中定义就可以了:
Xml代码 <ProgressBar android:id="@+id/pBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical"/> <ProgressBar
android:id="@+id/pBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
此时没有指定它的风格,默认是圆形的,一直会旋转的进度条,如果希望是超大号圆形进度条,只需设置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:
Xml代码 <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"/> <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"/>
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文件:
Xml代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="//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> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//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>
新建ProgressBarActivity.java
Java代码 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); } }); } } 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);
}
});
}
}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号