4.Android基础:常见控件----->ProgressBar

来源:互联网 发布:淘宝怎么更改收货地址 编辑:程序博客网 时间:2024/04/28 19:26

ProgressBar为进度条控件


1.重要属性

1.style

1.style="@android:style/Widget.ProgressBar.Horizontal"

水平进度条

2.style="@android:style/Widget.ProgressBar.Inverse" 

中型圆形进度条

3.style="@android:style/Widget.ProgressBar.Large"

大型圆形进度条

4.style="@android:style/Widget.ProgressBar.Large.Inverse" 

大型圆形进度条

5.style="@android:style/Widget.ProgressBar.Small"

小型圆形进度条

6.style="@android:style/Widget.ProgressBar.Small.Inverse"

小型圆形进度条

2.android:indetermimate该值取布尔值,有true和false,取true时进度条显示在循环滚动动画效果,只有在水平进度条有用

3.android:indetermimateOnly

该属性强制进度条的“不确定模式”。取值必须为布尔值

一下是对属性的简单操作

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity"     android:orientation="vertical">     <ProgressBar        android:id="@+id/pb1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        style="@android:style/Widget.ProgressBar.Horizontal"         android:indeterminate="false"        android:indeterminateBehavior="cycle"        android:indeterminateOnly="false"        android:progress="10"                />         <!-- 中型圆形进度条 -->    <ProgressBar        android:id="@+id/pb2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        style="@android:style/Widget.ProgressBar.Inverse"                 />        <!-- 大型圆形进度条 -->    <ProgressBar        android:layout_width="wrap_content"        android:layout_height="wrap_content"        style="@android:style/Widget.ProgressBar.Large"          android:indeterminateBehavior="cycle"        />         <!-- 大型圆形进度条 -->     <ProgressBar        android:layout_width="wrap_content"        android:layout_height="wrap_content"        style="@android:style/Widget.ProgressBar.Large.Inverse" />          <!-- 小型圆形进度条 -->     <ProgressBar        android:layout_width="wrap_content"        android:layout_height="wrap_content"        style="@android:style/Widget.ProgressBar.Small" />          <!-- 小型圆形进度条 -->     <ProgressBar        android:layout_width="wrap_content"        android:layout_height="wrap_content"        style="@android:style/Widget.ProgressBar.Small.Inverse" /></LinearLayout>

重要方法

1.getProgerss

获取进度条进度值

2.setProgerss

设置进度条进度值

3.setMax

设置进度条最大值

4.incrementProgressBy

设置进度条是增加还是减少


以下例子阐述下里面的属性及方法

第一种:简单水平进度条,里面用到子线程来刷新UI界面,使进度条有动态效果


这是布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >     <ProgressBar        android:id="@+id/pb1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        style="@android:style/Widget.ProgressBar.Horizontal"         android:progress="0"                />         <TextView         android:id="@+id/tv1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center_horizontal"        android:text="@string/text_view"        /></LinearLayout>


这个是测试类

public class MainActivity extends Activity {//声明控件对象private ProgressBar bar1;private TextView textview;int progress = 0;//刷新子线程Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {//得到当前进度条textview.setText((long)bar1.getProgress() * 100 / bar1.getMax() + "%");};};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.pb_main);initView(); //初始化控件变量new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubfor (int i = 0; i <= 100; i++) {try {progress = i;Thread.sleep(300);//把上一次下载进度加到进度条中bar1.setProgress(progress);//发送消息,让文本进度条改变handler.sendEmptyMessage(1);} catch (Exception e) {e.printStackTrace(); }}}}).start(); //开启线程}//填充布局public void initView() {//通过ID得到控件对象bar1 = (ProgressBar) findViewById(R.id.pb1);textview = (TextView) findViewById(R.id.tv1);progress = 0;bar1.setIndeterminate(false); //设置为不确定模式bar1.setProgress(progress);bar1.setMax(100); //设置进度条最大值为100}}


第二种:水平进度条与圆形进度条一起实现动态效果

这是布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >     <ProgressBar        android:id="@+id/pb1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        style="@android:style/Widget.ProgressBar.Horizontal"         android:progress="0"                />         <!-- 大型圆形进度条 -->    <ProgressBar        android:id="@+id/pb2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        style="@android:style/Widget.ProgressBar.Large"         />        <TextView         android:id="@+id/tv1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center_horizontal"        android:text="@string/text_view"        /></LinearLayout>
测试类中,在onCreate方法种,开启一个子线程,改变进度条内容;在类中封装了一个填充布局方法,,其中有一个线程获取水平滚动条控件对象,更新进度条内容

public class MainActivity extends Activity {//声明控件对象private ProgressBar bar1;private ProgressBar bar2;private TextView textview;int progress;private Handler handler;static final int STOP = 0;static final int CONTINUE = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.pb_main);initView(); //初始化控件变量new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubfor (int i = 0; i < 20; i++) {try {progress = (i + 1) * 5;Thread.sleep(500);//如果到19,线程停止if (i == 20) {Message msg = new Message();msg.what = STOP;//把停止消息发送广播出去handler.handleMessage(msg);break;} else {//继续加载Message msg = new Message();msg.what = CONTINUE;handler.handleMessage(msg);}} catch (Exception e) {e.printStackTrace();}}}}).start(); //开启线程}//填充布局public void initView() {//通过ID得到控件对象bar1 = (ProgressBar) findViewById(R.id.pb1);bar2 = (ProgressBar) findViewById(R.id.pb2);textview = (TextView) findViewById(R.id.tv1);progress = 0;bar1.setIndeterminate(false); //设置为不确定模式bar1.setProgress(progress);bar1.setMax(100); //设置进度条最大值为100//模式进度条加载handler = new Handler() {public void handleMessage(Message msg) {//得到是什么消息switch (msg.what) {case STOP:bar1.setVisibility(View.GONE); //设置进度条不可见bar2.setVisibility(View.GONE); //设置进度条不可见textview.setText("页面加载完毕"); //更新文本信息Thread.currentThread().interrupt(); //中断线程break;case CONTINUE:if (!Thread.currentThread().isInterrupted()) {bar1.setProgress(progress);}break;}super.handleMessage(msg);}};}}


上面Thread类中使用到方法

1.public Thread(Runnable runnable)

功能:构造方法,创建子线程对象

参数:runnable实现Runnable接口,在其中添加重写run方法

2.public void start()

功能:启动线程

参数:无

3.public static void sleep(long time)
功能:让线程休眠

参数:time、为休眠时间

0 0