Android笔记——ProgressBar(进度条)
来源:互联网 发布:cf域名注册 编辑:程序博客网 时间:2024/06/06 05:11
进度条可以方便的告诉用户现在执行任务的进度,特别是一个执行程序比较长的时候,没有进度条,用户不知道程序在执行,会以为程序假死强制关闭。
进度条分为:1.有明确进度信息的,将通过一个带刻度的水平条表示
2.是没有明确的进度信息(比如说一个一直转圈的图形)
3.用户自定义进度条(可以自己定义进度条显示的图片,旋转方向等样式)
MainActivity.java
package com.example.progressbar;import android.os.Bundle;import android.app.Activity;import android.app.ProgressDialog;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.ProgressBar;public class MainActivity extends Activity implements OnClickListener{private Button btnIncrement,btnDecrement;private ProgressBar pb;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//设置标题进度条必须在setContentView方法之前requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);setContentView(R.layout.activity_main);//设置为可见setProgressBarIndeterminateVisibility(true);btnIncrement = (Button) findViewById(R.id.increment);btnDecrement = (Button) findViewById(R.id.decrement);btnIncrement.setOnClickListener(this);btnDecrement.setOnClickListener(this);pb = (ProgressBar) findViewById(R.id.progressBar4);}public boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;} //按钮较多时,而且都要设置按钮的监听器,这时候最简单的方法就是:令Activity实现OnClickListener()接口, //然后通过setOnClickListener将Activity的实例注册到Buttonpublic void onClick(View v) {switch (v.getId()) {case R.id.increment :pb.incrementProgressBy(5);break;case R.id.decrement:pb.incrementProgressBy(-5);break;default:break;}} public void show(View v){//创建一个进度条的对话框组件ProgressDialog pd = new ProgressDialog(this);pd.setTitle("对话框");pd.setMessage("下载中。。。");//设置为水平对话框pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);pd.show();}}activity_main.xml
<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" android:orientation="vertical"> <ProgressBar android:id="@+id/progressBar1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ProgressBar android:id="@+id/progressBar2" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ProgressBar android:id="@+id/progressBar3" style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ProgressBar android:id="@+id/progressBar4" style="?android:attr/progressBarStyleHorizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:max="100" android:progress="50" android:secondaryProgress="70"/> <Button android:id="@+id/increment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增加" /> <Button android:id="@+id/decrement" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="减少" /><Button android:id="@+id/button_show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示对话框进度条" android:onClick="show" /> <ProgressBar android:id="@+id/progressBar5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminateDrawable="@drawable/pb_custom"/></LinearLayout>pb_custom.xml
主要设置了自定义小机器人旋转的进度条配置,设置从0~360度旋转,旋转中心是图片的中心,根据X,Y设置。
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item > <rotate android:drawable="@drawable/ic_launcher" android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" /> </item></layer-list>
可以看到前三个圆圈是不明确的进度条,一直在转。下来有水平进度条,接着我设置了两个按钮,增加和减少可以对上面的水平进度条的进度进行调节。最后一个小机器人是自定义的对话框,因为没有合适的图片,就选择了系统默认的机器人,但是效果和原理是一样的。
这里设置了对话框按钮
0 0
- Android笔记——ProgressBar(进度条)
- Android—ProgressBar进度条应用
- Android——使用ProgressBar实现进度条
- WPF—ProgressBar进度条
- Android学习笔记之progressBar(进度条)
- 【android控件学习笔记】ProgressBar进度条
- 【13】ProgressBar——进度条
- Android入门(15)——使用ProgressBar实现进度条
- Android基础入门教程——2.3.7 ProgressBar(进度条)
- 深入了解Android进度条——ProgressBar,SeekBar和RatingBar
- 深入了解Android进度条——ProgressBar,SeekBar和RatingBar
- Android开发——ProgressBar进度条进度控制
- Android ProgressBar 进度条
- Android ProgressBar 进度条
- android 进度条(ProgressBar)
- android 进度条(ProgressBar) [
- android------进度条ProgressBar
- android 控件 进度条(ProgressBar)
- django常用操作和问题一页纸
- javascript的this跟jQuery的this、$(this)
- 非UI线程处理Bitmap
- eclipse启动不了,一闪而过
- 浪潮记(8):过年
- Android笔记——ProgressBar(进度条)
- 直接拿来用!最火的Android开源项目(二)
- 高通AR实现增强显示小项目
- 函数指针
- 在UI上显示Bitmap
- 直接拿来用!最火的Android开源项目(一)
- 数组组合
- Single Number
- MySQL Cluster环境下如何恢复root密码