圆形进度条和水平进度条

进度条也是UI界面一种非常实用的组件,通常用于向用户显示某个耗时操作完成的百分比,进度条可以动态的显示进度,避免长时间的执行某个耗时操作时,让用户感觉程序失去了相应,从而更好的提高用户界面的友好性。

从样式来看,ProgressBar可以分为两种,一种是简单的不断旋转的圆环形状,一种是条形带进度的,圆环形状的进度条,还可以分为大中小三种。

                    style="@android:style/Widget.ProgressBar.Large"      大
                    style="@android:style/Widget.ProgressBar.Inverse"  中
                    style="@android:style/Widget.ProgressBar.Small"      小
                    style="@android:style/Widget.ProgressBar.Horizontal"                 水平横向

通过代码来看

MainActivity.java

package cn.lixyz.progressbartest;

import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 定义一个大环形进度条 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大环形进度条" /> <ProgressBar
style="@android:style/Widget.ProgressBar.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <!-- 定义一个中等大小的环形进度条 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中等大小环形进度条" /> <ProgressBar
style="@android:style/Widget.ProgressBar.Inverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <!-- 定义一个小环形进度条 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小环形进度条" /> <ProgressBar
style="@android:style/Widget.ProgressBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <!-- 定义一个水平进度条 -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="水平进度条" /> <ProgressBar
android:id="@+id/bar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="80"/> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始" />
</LinearLayout>

运行效果

我们模拟一下进度条加载的过程,通过点击按钮,使得进度条开始加载

MainActivity.java

package cn.lixyz.progressbartest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView; public class MainActivity extends Activity { private ProgressBar progressBar;
private TextView textView;
private int progress = 0;
private Button button; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); progressBar = (ProgressBar) findViewById(R.id.bar);
textView = (TextView) findViewById(R.id.text);
button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread() {
@Override
public void run() {
while (true) {
if (progress > 100) {
break;
} else {
try {
progressBar.setProgress(progress++);
sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}.start();
}
});
}
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 定义一个大环形进度条 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="大环形进度条" /> <ProgressBar
style="@android:style/Widget.ProgressBar.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <!-- 定义一个中等大小的环形进度条 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中等大小环形进度条" /> <ProgressBar
style="@android:style/Widget.ProgressBar.Inverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <!-- 定义一个小环形进度条 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小环形进度条" /> <ProgressBar
style="@android:style/Widget.ProgressBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <!-- 定义一个水平进度条 -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="水平进度条" /> <ProgressBar
android:id="@+id/bar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"/> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始" />
</LinearLayout>

运行效果

显示在标题上的进度条

有一种进度条,可以直接在窗口标题上显示,这种进度条甚至不需要使用ProgressBar组件,它是直接由Activity的方法启用的。为了在窗口上显示进度条,需要经过如下两步:

1)调用Activity的requestWindowFeature()方法,该方法传入的参数可启用特定的窗口特征,例如传入Window.FEATURE_INDETERMINATE_PROGRESS在窗口标题上显示不带进度的进度条;传入Window.FEATURE_ PROGRESS则显示带进度的进度条。

2)调用Activity的setProgressBarVisibility(boolean)或者setProgressBarIndeterMinateVisibility(true)即可控制进度条的显示和隐藏。

MainActivity.java

package cn.lixyz.progressbartest;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener { private Button bt1, bt2, bt3, bt4;
private int i = 0; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //设置窗口特征:启用显示进度的进度条
requestWindowFeature(Window.FEATURE_PROGRESS); //设置窗口特征,启用不显示进度的进度条
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.activity_main); bt1 = (Button) findViewById(R.id.bt1);
bt2 = (Button) findViewById(R.id.bt2);
bt3 = (Button) findViewById(R.id.bt3);
bt4 = (Button) findViewById(R.id.bt4); bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
bt4.setOnClickListener(this); } @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt1:
setProgressBarIndeterminateVisibility(true);
break;
case R.id.bt2:
setProgressBarIndeterminateVisibility(false);
break;
case R.id.bt3:
setProgressBarVisibility(true);
setProgress(8000);
break;
case R.id.bt4:
setProgressBarVisibility(false);
break;
}
}
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用来控制显示进度的进度条" /> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"> <Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示" /> <Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隐藏" /> </LinearLayout> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用来控制不显示进度的进度条" /> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"> <Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示" /> <Button
android:id="@+id/bt4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="隐藏" /> </LinearLayout> </LinearLayout>

运行效果:

Android笔记(二十三) Android中的ProgressBar(进度条)的更多相关文章

  1. Android笔记(六十三) android中的动画——逐帧动画( frame-by-frame animation)

    就好像演电影一样,播放实现准备好的图片,来实现动画效果. 逐帧动画需要用到AnimationDrawable类,该类主要用于创建一个逐帧动画,然后我们把这个动画设置为view的背景即可. androi ...

  2. Android进阶(二十三)Android开发过程之实例讲解

    Android开发过程之实例讲解 前言 回过头来审视之前做过的Android项目,发觉自己重新开发时忽然间不知所措了,间隔了太长时间没有开发导致自己的Android技能知识急剧下降.温故而知新. 废话 ...

  3. Android笔记(七十三) Android权限问题整理 非常全面

    Android权限系统非常庞大,我们在Android系统中做任何操作都需要首先获取Android系统权限,本文记录了所有的Android权限问题,整理一下分享给大家. 访问登记属性 android.p ...

  4. Android学习笔记- ProgressBar(进度条)

    本节引言: 本节给大家带来的是Android基本UI控件中的ProgressBar(进度条),ProgressBar的应用场景很多,比如 用户登录时,后台在发请求,以及等待服务器返回信息,这个时候会用 ...

  5. Android学习笔记_76_Android ProgressBar 进度条

    android 进度条的样式  例1:(默认样式(中等圆形))Xml代码 <ProgressBar      android:id="@+id/progressBar1"   ...

  6. Android零基础入门第51节:进度条ProgressBar

    原文:Android零基础入门第51节:进度条ProgressBar 不知不觉这已经是第51期了,在前面50期我们学了Android开发中使用频率非常高的一些UI组件,当然这些组件还不足够完成所有AP ...

  7. python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码

    python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码 淘宝IP地址库 http://ip.taobao.com/目前提供的服务包括:1. 根据用户提供的 ...

  8. Android——ProgressBar(进度条)

    参考资料来源于菜鸟教程--学的不仅是技术,更是梦想! 学习! 1.常用属性讲解与基础实例 从官方文档,我们看到了这样一个类关系图: ProgressBar继承与View类,直接子类有AbsSeekBa ...

  9. android中SeekBar拖动进度条的使用及事件监听

    下面和大家分享一下android中SeekBar拖动进度条的使用,以及事件监听.拖动进度条的事件监听需要实现SeekBar.OnSeekBarChangeListener接口,调用SeekBar的se ...

  10. ProgressBar(进度条)、SeekBar(拖动条)与星级评分条(RatingBar)

    1.ProgressBar(进度条) (1)介绍 (2)常用属性 (3)xml代码 <ProgressBar android:id="@+id/progressBar2" s ...

随机推荐

  1. 转 ORA-16191 "Primary log shipping client not logged on standby

    ###sample 0 原因未知: 解决办法,重建密码文件 primary db :alter system set log_archive_dest_state_2=defer sid='*' sc ...

  2. DBGrid 单击弹出PickList

    type   myGrid = class(TCustomGrid)   end; type   myInplaceEditList = class(TInplaceEditList)   end; ...

  3. this page isn't working (ERR_EMPTY_RESPONSE)

    特定情况触发了PHP的Call to undefined function(函数不存在)的Fatal error(致命错误),PHP异常终止执行,Apache收到PHP的异常信号时,认为PHP处理请求 ...

  4. python 求交集、并集、差集

    需要用到set类型 交集,两种方法 retA = [i for i in listA if i in listB] retB = setA.intersection(setB) 并集 retC = s ...

  5. Python3之内建模块hashlib

    摘要算法简介 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串( ...

  6. Java基础教程:多线程杂谈——Volatile

    Java基础教程:多线程杂谈——Volatile 引入Volatile Java语言提供了一种稍弱的同步机制,即Volatile变量,用来确保将变量的更新操作通知到其他线程.当把变量声明为Volati ...

  7. Okhttp3基本使用

    https://square.github.io/okhttp/ https://www.jianshu.com/p/da4a806e599b https://www.cnblogs.com/wzk- ...

  8. linux系统卡顿 性能分析

    systemtrap 是一个内核开发者要掌握的工具. linux performance analysis 系统瓶颈性能分析软件

  9. ipv6 地址说明

    开篇我们先简单介绍下ipv4 地址 IPv4 地址: ipv4地址一共32位,用点分十进制表示,每一个部分是8位.子网掩码有两种表示 192.168.1.3 / 24 表示ip的前24位是网络位,后8 ...

  10. 哈希--Hash,“散列”/“哈希”

    哈希 Hash,翻译“散列”,音译为“哈希”,把任意长度的输入,通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映射,也就是散列值的空间通常远小于输入的空间,不同的输入可能会散 ...