多式样ProgressBar

普通圆形ProgressBar

1.png


2.png




该类型进度条也就是一个表示运转的过程,例如发送短信,连接网络等等,表示一个过程正在执行中。

一般只要在XML布局中定义就可以了。

  1. <progressBar android:id="@+id/widget43"
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:layout_gravity="center_vertical">
  5. </ProgressBar>
复制代码

此时,没有设置它的风格,那么它就是圆形的,一直会旋转的进度条。

各大小样式圆形ProgressBar


超大号圆形ProgressBar


3.png



此时,给设置一个style风格属性后,该ProgressBar就有了一个风格,这里大号ProgressBar的风格是:

  1. style="?android:attr/progressBarStyleLarge"
复制代码


完整XML定义是:

  1. <progressBar android:id="@+id/widget196"
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. style="?android:attr/progressBarStyleLarge">
  5. </ProgressBar>
复制代码

小号圆形ProgressBar

4.png

2011-4-24 09:04:58 上传 下载附件(1.21 KB)




小号ProgressBar对应的风格是:

  1. style="?android:attr/progressBarStyleSmall"
复制代码

标题型圆形ProgressBar

5.png


6.png


标题型ProgressBar对应的风格是:

  1. style="?android:attr/progressBarStyleSmallTitle"
复制代码

完整XML定义是:

  1. <progressBar android:id="@+id/widget110"
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. style="?android:attr/progressBarStyleSmallTitle">
  5. </ProgressBar>
复制代码

代码中实现:

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. // TODO Auto-generated method stub
  4. super.onCreate(savedInstanceState);
  5. requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
  6. //请求窗口特色风格,这里设置成不明确的进度风格
  7. setContentView(R.layout.second);
  8. setProgressBarIndeterminateVisibility(true);
  9. //设置标题栏中的不明确的进度条是否可以显示
  10. }
复制代码

长形进度条

布局中的长形进度条

7.png

①首先在XML进行布局

  1. <progressBar android:id="@+id/progressbar_updown"
  2. android:layout_width="200dp"
  3. android:layout_height="wrap_content"
  4. style="?android:attr/progressBarStyleHorizontal"
  5. android:layout_gravity="center_vertical"
  6. android:max="100"
  7. android:progress="50"
  8. android:secondaryProgress="70" >
复制代码

讲解: 111.png


②代码中运用

  1. private ProgressBar myProgressBar;
  2. //定义ProgressBar
  3. myProgressBar = (ProgressBar) findViewById(R.id.progressbar_updown);
  4. //ProgressBar通过ID来从XML中获取
  5. myProgressBar.incrementProgressBy(5);
  6. //ProgressBar进度值增加5
  7. myProgressBar.incrementProgressBy(-5);
  8. //ProgressBar进度值减少5
  9. myProgressBar.incrementSecondaryProgressBy(5);
  10. //ProgressBar背后的第二个进度条 进度值增加5
  11. myProgressBar.incrementSecondaryProgressBy(-5);
  12. //ProgressBar背后的第二个进度条 进度值减少5
复制代码

页面标题中的长形进度条

8.png



代码实现:
①先设置一下窗口风格特性

  1. requestWindowFeature(Window.FEATURE_PROGRESS);
  2. //请求一个窗口进度条特性风格
  3. setContentView(R.layout.main);
  4. setProgressBarVisibility(true);
  5. //设置进度条可视
复制代码

②然后设置进度值

  1. setProgress(myProgressBar.getProgress() * 100);
  2. //设置标题栏中前景的一个进度条进度值
  3. setSecondaryProgress(myProgressBar.getSecondaryProgress() * 100);
  4. //设置标题栏中后面的一个进度条进度值
  5. //ProgressBar.getSecondaryProgress() 是用来获取其他进度条的进度值
复制代码


ProgressDialog

ProgressDialog中的圆形进度条
9.png


10.png



ProgressDialog一般用来表示一个系统任务或是开启任务时候的进度,有一种稍等的意思。
代码实现:

  1. ProgressDialog mypDialog=new ProgressDialog(this);
  2. //实例化
  3. mypDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  4. //设置进度条风格,风格为圆形,旋转的
  5. mypDialog.setTitle("Google");
  6. //设置ProgressDialog 标题
  7. mypDialog.setMessage(getResources().getString(R.string.second));
  8. //设置ProgressDialog 提示信息
  9. mypDialog.setIcon(R.drawable.android);
  10. //设置ProgressDialog 标题图标
  11. mypDialog.setButton("Google",this);
  12. //设置ProgressDialog 的一个Button
  13. mypDialog.setIndeterminate(false);
  14. //设置ProgressDialog 的进度条是否不明确
  15. mypDialog.setCancelable(true);
  16. //设置ProgressDialog 是否可以按退回按键取消
  17. mypDialog.show();
  18. //让ProgressDialog显示
复制代码

ProgressDialog中的长形进度条


11.png

12.png








代码实现:

  1. ProgressDialog mypDialog=new ProgressDialog(this);
  2. //实例化
  3. mypDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  4. //设置进度条风格,风格为长形,有刻度的
  5. mypDialog.setTitle("地狱怒兽");
  6. //设置ProgressDialog 标题
  7. mypDialog.setMessage(getResources().getString(R.string.second));
  8. //设置ProgressDialog 提示信息
  9. mypDialog.setIcon(R.drawable.android);
  10. //设置ProgressDialog 标题图标
  11. mypDialog.setProgress(59);
  12. //设置ProgressDialog 进度条进度
  13. mypDialog.setButton("地狱曙光",this);
  14. //设置ProgressDialog 的一个Button
  15. mypDialog.setIndeterminate(false);
  16. //设置ProgressDialog 的进度条是否不明确
  17. mypDialog.setCancelable(true);
  18. //设置ProgressDialog 是否可以按退回按键取消
  19. mypDialog.show();
  20. //让ProgressDialog显示
复制代码



AlertDialog.Builder

AlertDialog中的圆形ProgressBar
13.png





①先来设计一个Layout,待会儿作为一个View,加入AlertDialog.Builder

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_gravity="center_horizontal"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content">
  6. <LinearLayout android:id="@+id/LinearLayout01"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content">
  9. </LinearLayout>
  10. <ProgressBar android:layout_gravity="center_vertical|center_horizontal"
  11. android:layout_height="wrap_content"
  12. android:progress="57"
  13. android:id="@+id/myView_ProgressBar2"
  14. android:layout_width="wrap_content">
  15. </ProgressBar>
  16. </LinearLayout>
复制代码

②代码罗

  1. private AlertDialog.Builder AlterD,AlterD2;
  2. //定义提示对话框
  3. private LayoutInflater layoutInflater;
  4. //定义布局过滤器
  5. private LinearLayout myLayout;
  6. //定义布局
  7. layoutInflater2=(LayoutInflater) getSystemService(this.LAYOUT_INFLATER_SERVICE);
  8. //获得系统的布局过滤服务
  9. myLayout2=(LinearLayout) layoutInflater2.inflate(R.layout.roundprogress, null);
  10. //得到事先设计好的布局
  11. AlterD2.setTitle(getResources().getString(R.string.RoundO));
  12. //设置对话框标题
  13. AlterD2.setIcon(R.drawable.ma);
  14. //设置对话框图标
  15. AlterD2.setMessage(getResources().getString(R.string.ADDView));
  16. //设置对话框提示信息
  17. AlterD2.setView(myLayout2);
  18. //设置对话框中的View
  19. AlterD2.show();
  20. //让对话框显示
复制代码

AlertDialog中的长形ProgressBar(可控制)
14.png



①先来设计一个Layout,待会儿作为一个View,加入AlertDialog.Builder

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_gravity="center_horizontal"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content">
  6. <Button
  7. android:layout_height="wrap_content"
  8. android:text="-"
  9. android:layout_width="50dp"
  10. android:id="@+id/myView_BT_Down">
  11. </Button>
  12. <ProgressBar
  13. android:layout_gravity="center_vertical"
  14. android:layout_height="wrap_content"
  15. style="?android:attr/progressBarStyleHorizontal"
  16. android:id="@+id/myView_ProgressBar"
  17. android:progress="57"
  18. android:layout_width="178dp">
  19. </ProgressBar>
  20. <Button android:layout_height="wrap_content"
  21. android:text="+"
  22. android:layout_width="50dp"
  23. android:id="@+id/myView_BT_Up">
  24. </Button>
  25. </LinearLayout>
复制代码

②代码罗

  1. private AlertDialog.Builder AlterD,AlterD2;
  2. //定义提示对话框
  3. private LayoutInflater layoutInflater;
  4. //定义布局过滤器
  5. private LinearLayout myLayout;
  6. //定义布局
  7. layoutInflater=(LayoutInflater) getSystemService(this.LAYOUT_INFLATER_SERVICE);
  8. //获得系统的布局过滤服务
  9. myLayout=(LinearLayout) layoutInflater.inflate(R.layout.myview, null);
  10. //得到事先设计好的布局
  11. myup=(Button) myLayout.findViewById(R.id.myView_BT_Up);
  12. mydown=(Button) myLayout.findViewById(R.id.myView_BT_Down);
  13. mypro=(ProgressBar)myLayout.findViewById(R.id.myView_ProgressBar);
  14. //通过myLayout.findViewById来获取自定义View中的Widget控件元素
  15. myup.setOnClickListener(this);
  16. //设置对话框View中的按钮监听器
  17. mydown.setOnClickListener(this);
  18. //设置对话框View中的按钮监听器
  19. mypro.setProgress(Tag);
  20. //设置一个Tag作为进度值
  21. AlterD.setTitle(getResources().getString(R.string.RectO));
  22. //设置对话框标题
  23. AlterD.setIcon(R.drawable.mb);
  24. //设置对话框图标
  25. AlterD.setMessage(getResources().getString(R.string.ADDView));
  26. //设置对话框提示信息
  27. AlterD.setView(myLayout);
  28. //设置对话框添加的View
  29. AlterD.setPositiveButton("OK", new DialogInterface.OnClickListener(){
  30. @Override
  31. public void onClick(DialogInterface dialog, int which) {
  32. // TODO Auto-generated method stub
  33. MyProgressBar.Tag=mypro.getProgress();
  34. }});
  35. //设置对话框按钮,以及按钮的事件监听器
  36. AlterD.show();
  37. //让对话框显示
复制代码


③进度条进度值的按钮事件

  1. myup.setOnClickListener(this);
  2. //设置对话框View中的按钮监听器
  3. mydown.setOnClickListener(this);
  4. //设置对话框View中的按钮监听器
  5. 对应的代码:
  6. @Override
  7. public void onClick(View button) {
  8. // TODO Auto-generated method stub
  9. SwitchUPorDown(button);
  10. }
  11. private void SwitchUPorDown(View button) {
  12. switch (button.getId()) {
  13. case R.id.myView_BT_Up: {
  14. mypro.incrementProgressBy(1);
  15. }
  16. break;
  17. case R.id.myView_BT_Down: {
  18. mypro.incrementProgressBy(-1);
  19. }
  20. break;
  21. default:
  22. break;
  23. }
  24. }

更多相关文章

  1. Android判断当前网络是否可用--示例代码
  2. 【Android】Android 监听网络状态+源代码
  3. 【Android】代码实现模拟屏幕点击和键盘按键事件
  4. ProGuard代码混淆详细攻略
  5. Android 在代码中同时给控件设置圆角和背景色
  6. Android webview调用js代码无效 webView.loadUrl("javascript:al
  7. Android 将Activity以对话框(Dialog)形式显示

随机推荐

  1. Android---33---四种加载模式
  2. android体系结构介绍
  3. android 显示系统 surfaceflinger 分析
  4. 基于树莓派的 Android(安卓)Things 开发
  5. android关于百度地图显示网格问题
  6. Android系列之网络(二)----获取HTTP请求头
  7. Android运行机制
  8. Mars Android视频教程完整版高清在线观看
  9. Android进阶-Android系统信息与安全机制
  10. ProgressBar(进度条) 分类 Android 基础