多式样ProgressBar

普通圆形ProgressBar

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

一般只要在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

此时,给设置一个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

小号ProgressBar对应的风格是:

  1. style="?android:attr/progressBarStyleSmall"

复制代码

完整XML定义是:

  1. <progressBar android:id="@+id/widget108"
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. style="?android:attr/progressBarStyleSmall">
  5. </ProgressBar>

复制代码

标题型圆形ProgressBar

标题型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. }

复制代码

长形进度条

布局中的长形进度条

①首先在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"    >

复制代码

讲解:

style="?android:attr/progressBarStyleHorizontal"    设置风格为长形 
android:max="100"    最大进度值为100
android:progress="50"   初始化的进度值
android:secondaryProgress="70"  初始化的底层第二个进度值 
android:layout_gravity="center_vertical"    垂直居中

②代码中运用

  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

复制代码

页面标题中的长形进度条

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

  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中的圆形进度条
      
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中的长形进度条

代码实现:

  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

①先来设计一个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(可控制)

①先来设计一个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. }

复制代码

App Widget中的进度条

Widget中的圆形ProgressBar

这个很简单,在Widget中没有多大意思,不再敷述。

Widget中的长形ProgressBar(可控制)

Widget的实现就不再重复,假设您已经把Widget布局,相应设置已经设置好了。也可以在桌面加入类似上面图中的样式。
现在我们来实现一下按钮事件,与进度条的交互。
下面还是简单讲解一下Widget的设计与部署。

①设计Widget布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:background="@drawable/widget"
  5. android:layout_height="74dp"
  6. android:layout_width="296dp">
  7. <Button
  8. android:layout_height="wrap_content"
  9. android:text="-"
  10. android:layout_gravity="center_vertical"
  11. android:layout_width="50dp"
  12. android:id="@+id/widget_BT_Down"
  13. android:layout_marginLeft="10dp">
  14. </Button>
  15. <ProgressBar
  16. android:layout_gravity="center_vertical"
  17. android:layout_height="wrap_content"
  18. style="?android:attr/progressBarStyleHorizontal"
  19. android:layout_width="178dp"
  20. android:id="@+id/widget_ProgressBar">
  21. </ProgressBar>
  22. <Button
  23. android:layout_height="wrap_content"
  24. android:text="+"
  25. android:layout_gravity="center_vertical"
  26. android:layout_width="50dp"
  27. android:id="@+id/widget_BT_Up">
  28. </Button>
  29. </LinearLayout>

复制代码

②新增一个.res/xml目录,加入appwidget-provider

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <appwidget-provider
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:initialLayout="@layout/widgetlayout"
  5. android:updatePeriodMillis="8660000"
  6. android:minWidth="296dp"
  7. android:minHeight="74dp">
  8. </appwidget-provider>

复制代码

③实现一个AppWidgetProvider子类

  1. package zyf.test.ProgressBar;
  2. import android.appwidget.AppWidgetManager;
  3. import android.appwidget.AppWidgetProvider;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. public class App extends AppWidgetProvider {
  7. @Override
  8. public void onEnabled(Context context) {
  9. // TODO Auto-generated method stub
  10. super.onEnabled(context);
  11. }
  12. @Override
  13. public void onReceive(Context context, Intent intent) {
  14. // TODO Auto-generated method stub
  15. super.onReceive(context, intent);
  16. }
  17. @Override
  18. public void onUpdate(Context context, AppWidgetManager appWidgetManager,
  19. int[] appWidgetIds) {
  20. // TODO Auto-generated method stub
  21. super.onUpdate(context, appWidgetManager, appWidgetIds);
  22. }
  23. }

复制代码

④配置Manifest,进行注册

  1. <receiver android:name="AppWidget">
  2. <intent-filter>
  3. <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
  4. </intent-filter>
  5. <meta-data
  6. android:resource="@xml/appwidget"
  7. android:name="android.appwidget.provider">
  8. </meta-data>
  9. </receiver>

复制代码

这里实现按钮与进度条的交互。(Widget自己广播发送与接收)

①按钮的消息发送

  1. @Override
  2. public void onUpdate(Context context, AppWidgetManager appWidgetManager,
  3. int[] appWidgetIds) {
  4. // TODO Auto-generated method stub
  5. final int N = appWidgetIds.length;
  6. // Perform this loop procedure for each App Widget that belongs to this provider
  7. for (int i=0; i<N; i++) {
  8. int appWidgetId = appWidgetIds;
  9. RemoteViews views=
  10. new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
  11. Intent UPintent=new Intent("zyf.test.widget.UP");
  12. Intent DOWNintent=new Intent("zyf.test.widget.DOWN");
  13. //实例化 两个带有Action的Intent
  14. PendingIntent pendingIntentUp
  15. =PendingIntent.getBroadcast(context, 0, UPintent, 0);
  16. PendingIntent pendingIntentDown
  17. =PendingIntent.getBroadcast(context, 0, DOWNintent, 0);
  18. //实例化两个以Intent来构造的PendingIntent
  19. views.setOnClickPendingIntent(R.id.widget_BT_Up, pendingIntentUp);
  20. views.setOnClickPendingIntent(R.id.widget_BT_Down, pendingIntentDown);
  21. //给View上的两个按钮绑定事件,这里是广播消息的发送
  22. appWidgetManager.updateAppWidget(appWidgetId, views);
  23. }
  24. }

复制代码

②Widget自身消息接收,使用intent.getAction()来获取Action

  1. @Override
  2. public void onReceive(Context context, Intent intent) {
  3. // TODO Auto-generated method stub
  4. super.onReceive(context, intent);
  5. if(intent.getAction().equals("zyf.test.widget.UP")){
  6. Tag+=5;
  7. if(Tag>100){
  8. Tag=100;
  9. }
  10. views.setProgressBar(R.id.widget_ProgressBar, 100, Tag, false);
  11. appManager.updateAppWidget(thisWidget, views);
  12. }
  13. if(intent.getAction().equals("zyf.test.widget.DOWN")){
  14. Tag-=5;
  15. if(Tag<0){
  16. Tag=0;
  17. }
  18. views.setProgressBar(R.id.widget_ProgressBar, 100, Tag, false);
  19. appManager.updateAppWidget(thisWidget, views);
  20. }
  21. }

复制代码

③进度条的进度值设置

  1. views.setProgressBar(R.id.widget_ProgressBar, 100, Tag, false);
  2. //设置Widget上的进度条的进度值
  3. //第一个参数,Widget上进度条ID
  4. //第二个参数,进度条最大值
  5. //第三个参数Tag,一个int值,就是设置的进度值
  6. //第四个参数,是否是要进度条不确定

复制代码

注意了,Widget自身的onReceive()方法如果要接收其他的Action广播。那就必须在Manifest中,在Intent-filter中添加Action:

  1. <receiver android:name="AppWidget">
  2. <intent-filter>
  3. <action android:name="android.appwidget.action.APPWIDGET_UPDATE"></action>
  4. <action android:name="zyf.test.widget.UP"></action>
  5. <action android:name="zyf.test.widget.DOWN"></action>
  6. </intent-filter>
  7. <meta-data
  8. android:resource="@xml/appwidget"
  9. android:name="android.appwidget.provider">
  10. </meta-data>
  11. </receiver>

复制代码


转载于:https://www.cnblogs.com/wangtianxj/archive/2010/02/20/1669824.html

多式样ProgressBar(转)相关推荐

  1. ProgressBar 各种样式

    多式样ProgressBar 普通圆形ProgressBar 该类型进度条也就是一个表示运转的过程,例如发送短信,连接网络等等,表示一个过程正在执行中. 一般只要在XML布局中定义就可以了. Java ...

  2. Android 自定义ProgressBar 实现进度圆环

    实现的效果如下图 实现效果图demo 的地址 代码很简单自定义ProgressBar 下面直接列举下代码 progressBarView 的代码如下 public class ProgressBarV ...

  3. Android ProgressBar 加载中界面实现(loading 动画) 实现菊花的效果

    实现的效果图如下: 使用方法ProgressBar ,如果感觉 这个动画不是自己想要的,需要根据ui的图片来实现只需要将xml中的animated-rotate 修改为animation-list 贴 ...

  4. Android列表控件选项中添加进度框ProgressBar实现

        今天有时间就学习了下在ListView.GridView列表项中清加ProgressBar,小马用最简单的代码实现可以通用的功能,人人都能看懂,哈哈,直接说下,如果你的适配器getView方法 ...

  5. 使用ASP.NET Atlas编写显示真实进度的ProgressBar(进度条)控件

    当后台在进行某些长时间的操作时,如果能在页面上提供一个显示真实进度的进度条,而不是让用户不知情的等待或是从前的那些简单的估计,将是一个非常难得的出彩之处.现在使用ASP.NET Atlas完全有可能做 ...

  6. Android学习笔记之progressBar(进度条)

    一.说明 <1>在某项延续性工作的进展过程中为了不让用户觉得程序死掉了,需要有个活动的进度条,表示此过程正在进行中. <2>在某些操作的进度中的可视指示器,为用户呈现操作的进度 ...

  7. linux ftp显示进度条,在Python中显示FTP下载进度(ProgressBar)

    我使用以下Python脚本通过FTP下载文件.我想要的是在下载时查看进度的详细信息.为此,我使用了ProgressBar但它没有显示任何内容. 这是我的代码: import re import os ...

  8. android java style_Android 在Java代码中设置style属性--使用代码创建ProgressBar对象

    强烈推荐: 在andriod开发中,很大一部分都要与资源打交道,比如说:图片,布局文件,字符串,样式等等.这给我们想要开发一些公共的组件带来很大的困难,因为公共的组件可能更愿意以jar包的形式出现.但 ...

  9. 安卓设置菊花动画_Android Progressbar自定义菊花效果

    项目中需要用到类似IOS的菊花选中效果.有一些app中是使用第三方的git控件来做直接加载一个gif图片,不过我不喜欢这种方式.为了这么点功能就在项目中添加类库总觉得大材小用了.这里直接为progre ...

  10. 自定义ProgressBar(自定义View和ClipDrawable)

    开发中经常需要自定义ProgressBar,这里用了自定义View和ClipDrawable实现简单的ProgressBar 自定义View效果: public class CustomProgres ...

最新文章

  1. 【点云论文速读】最佳点云分割分析
  2. Spring Cloud Alibaba 基础教程:Nacos 生产级版本 0.8.0
  3. RDKit | 小分子构象的生成和比对
  4. Flex+fluorineFx +ASP.NET开发的IIS部署
  5. Android入门之AlertDialog
  6. Python3基本数据类型快速入门
  7. 初学者Git和GitHub简介(教程)
  8. 安装.Net Standard 2.0, Impressive
  9. PHP之路——Xdebug扩展
  10. UEFI引导修复教程
  11. 【转】傅里叶分析之掐死教程
  12. Presto查询出现error executing query异常
  13. 阿尔伯塔大学 计算机科学,阿尔伯塔大学
  14. ZZNUOJ_C语言1039:n个数求和(完整代码)
  15. 群晖NAS 7.X版搭建博客网站,并内网穿透发布公网可访问 4-8
  16. oracle时间平均,Oracle SQL的平均时间
  17. 聊一聊GNU/Linux 与开源文化的那些人和事
  18. SpringBoot+Mybatis+Vue整合
  19. python课后题答案董付国_python习题01——董付国学习系列
  20. Linux入门学习(基础操作命令)

热门文章

  1. linux 中断程序设计,linux – CPU0被eth1中断淹没
  2. qmake:未找到命令
  3. dontshrink解决ProGuard错误:java.lang.StackOverflowError
  4. C语言把字串转换大小写的函数
  5. JS中,把一个数字转换为字串
  6. 管理新语:会议与问题的关系
  7. You must install signalwire-client-c to build mod_signalwire
  8. 全网首发:LINUX OpenCV编译java/jar版本注意事项
  9. Linux上RandomAccessFile访问FTP文件出错
  10. 使用超时加锁:pthread_mutex_timedlock