在某项延续性工作的进展过程中为了不让用户觉得程序死掉了,需要有个活动的进度条,表示此过程正在进行中。Android中使用ProgressBar来实现这一功能:
1、简单的进度条
在xml中添加:
<ProgressBar android:id=”@+id/ProgressBar01″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</ProgressBar>
就可以看到下面,圆形的、大大的圈圈:
[img]http://dl.iteye.com/upload/attachment/439823/2b53a81e-2ffb-3c67-bff1-9d540b47563c.png[/img]
2、各种各样的圆圈:
[img]http://dl.iteye.com/upload/attachment/439821/8c946adc-9aae-3409-bb25-8c0229d0e531.png[/img]
注意标题栏上也有一个进度条,下面是代码:
view sourceprint?
01 package android.basic.lesson11;
02
03 import android.app.Activity;
04 import android.os.Bundle;
05 import android.view.Window;
06
07 public class MainHelloProgressBar extends Activity {
08 /** Called when the activity is first created. */
09 @Override
10 public void onCreate(Bundle savedInstanceState) {
11 super.onCreate(savedInstanceState);
12 //在标题条里放置进度条。请求窗口特色风格,这里设置成不明确的进度风格
13 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
14 setContentView(R.layout.main);
15 //设置标题栏中的不明确的进度条是否可以显示,当你需要表示处理中的时候设置为True,处理完毕后设置为false
16 setProgressBarIndeterminateVisibility(true);
17 }
18 }
下面试main.xml中的代码,大家注意黑体字部分的内容,看看不同样式的不同的表现:
<?xml version=”1.0″ encoding=”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、长条状的进度条
xml代码:
<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.0″ encoding=”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);
}
}
运行效果:
[img]http://dl.iteye.com/upload/attachment/439824/e2f71617-9dd4-3eb9-9f12-587acc242e2b.png[/img]
同学们可以使用下面的代码对,进度条进行一些操作练习:
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
1、简单的进度条
在xml中添加:
<ProgressBar android:id=”@+id/ProgressBar01″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</ProgressBar>
就可以看到下面,圆形的、大大的圈圈:
[img]http://dl.iteye.com/upload/attachment/439823/2b53a81e-2ffb-3c67-bff1-9d540b47563c.png[/img]
2、各种各样的圆圈:
[img]http://dl.iteye.com/upload/attachment/439821/8c946adc-9aae-3409-bb25-8c0229d0e531.png[/img]
注意标题栏上也有一个进度条,下面是代码:
view sourceprint?
01 package android.basic.lesson11;
02
03 import android.app.Activity;
04 import android.os.Bundle;
05 import android.view.Window;
06
07 public class MainHelloProgressBar extends Activity {
08 /** Called when the activity is first created. */
09 @Override
10 public void onCreate(Bundle savedInstanceState) {
11 super.onCreate(savedInstanceState);
12 //在标题条里放置进度条。请求窗口特色风格,这里设置成不明确的进度风格
13 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
14 setContentView(R.layout.main);
15 //设置标题栏中的不明确的进度条是否可以显示,当你需要表示处理中的时候设置为True,处理完毕后设置为false
16 setProgressBarIndeterminateVisibility(true);
17 }
18 }
下面试main.xml中的代码,大家注意黑体字部分的内容,看看不同样式的不同的表现:
<?xml version=”1.0″ encoding=”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、长条状的进度条
xml代码:
<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.0″ encoding=”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);
}
}
运行效果:
[img]http://dl.iteye.com/upload/attachment/439824/e2f71617-9dd4-3eb9-9f12-587acc242e2b.png[/img]
同学们可以使用下面的代码对,进度条进行一些操作练习:
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