一、ProgressBar的分类java
能够精确显示进度(能够显示刻度或者精确百分比)android
不能够精确显示精度(一直转,相似于一个过场动画)app
二、关键属性和方法ide
指定ProgressBar显示风格函数
style="?android:attr/progressBarStyleLarge" 大环形进度条布局
style="?android:attr/progressBarStyleSmall" 小环形进度条动画
style="?android:attr/progressBarStyleHorizontal" 水平进度条this
ProgressBar的关键属性code
android:max="100" 最大显示进度xml
android:progress="50" 第一显示进度
android:secondaryProgress="80" 第二显示进度
android:indenterminate="true" 设置是否精确显示(true表示不精确显示进度,false表示精确显示进度)
ProgressBar的关键方法
setProgress(int) 设置第一进度
setSecondaryProgress(int) 设置第二进度
setProgress() 获取第一进度
getSecondaryProgress() 获取第二进度
incrementProgressBy(int) 增长或减小第一进度
incrementSecondaryProgressBy(int) 增长或减小第二进度
getMax() 获取最大进度
三、标题栏中的ProgressBar
package com.example.myandroidprogressbar; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.Window; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //一、启用窗口特征,启用带进度何不带进度的两种进度条 requestWindowFeature(Window.FEATURE_PROGRESS); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.activity_main); //显示两种进度条 setProgressBarVisibility(true); setProgressBarIndeterminateVisibility(true); //Max=10000 setProgress(600); } }
四、界面中四种不一样形式的进度条
修改activity_main.xml文件以下
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> <ProgressBar android:id="@+id/progressBar2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/progressBar1" android:layout_toRightOf="@+id/progressBar1" /> <ProgressBar android:id="@+id/progressBar3" style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/horiz" android:layout_toRightOf="@+id/progressBar2" /> <ProgressBar android:id="@+id/horiz" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/progressBar1" android:layout_below="@+id/progressBar1" android:max="100" android:progress="50" android:secondaryProgress="80" /> </RelativeLayout>
五、经过按钮改变进度条的数值并动态改变
修改activity_main.xml文件
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ProgressBar android:id="@+id/horiz" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="50" android:secondaryProgress="80" /> <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/horiz" android:layout_below="@+id/horiz" android:text="@string/add" /> <Button android:id="@+id/reduce" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/add" android:layout_below="@+id/add" android:text="@string/reduce" /> <Button android:id="@+id/reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/reduce" android:layout_below="@+id/reduce" android:text="@string/reset" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/reset" android:layout_alignRight="@+id/horiz" android:layout_below="@+id/reset" android:layout_marginTop="16dp" /> </RelativeLayout>
修改MainActivity.java文件
package com.example.myandroidprogressbar; import android.os.Bundle; import android.R.integer; import android.app.Activity; 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; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener{ //一、声明部分控件 private ProgressBar progress; private Button add; private Button reduce; private Button reset; private TextView text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //二、初始化控件 init(); } private void init() { // TODO Auto-generated method stub //三、绑定 progress=(ProgressBar) findViewById(R.id.horiz); add=(Button) findViewById(R.id.add); reduce=(Button) findViewById(R.id.reduce); reset=(Button) findViewById(R.id.reset); text=(TextView) findViewById(R.id.text); //四、获取各进度条进度 //获取第一进度条的进度 //int first=progress.getProgress(); //湖区第二进度条的进度 //int second=progress.getSecondaryProgress(); //获取进度条的最大进度 //int max=progress.getMax(); //五、将数据显示到textView中 //text.setText("第一进度的百分比:"+(int)(first/(float)max*100)+ //"% 第二进度的百分比:"+(int)(second/(float)max*100)+"%"); add.setOnClickListener(this); reduce.setOnClickListener(this); reset.setOnClickListener(this); } //六、设置按钮点击响应事件,改变进度条数据 @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()){ case R.id.add: //增长第一进度和第二进度10个刻度 progress.incrementProgressBy(10); progress.incrementSecondaryProgressBy(10); break; case R.id.reduce: //减小第一进度和第二进度10个刻度 progress.incrementProgressBy(-10); progress.incrementSecondaryProgressBy(-10); break; case R.id.reset: //重置 progress.setProgress(50); progress.setSecondaryProgress(80); break; } //七、由于每一次点击都会调用onClick这个函数,而且必定会执行switch这个判断。 //因此不必在每个分支中刷新textView的内容,只须要在switch分支外面写就行了, //以前的向textView中写入数据的应该注释掉 text.setText("第一进度的百分比:"+(int)(progress.getProgress()/(float)progress.getMax()*100)+ "% 第二进度的百分比:"+(int)(progress.getSecondaryProgress()/(float)progress.getMax()*100)+"%"); } }
六、对话框形式的进度条(ProgressDialog)
修改activity_main.xml文件
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="86dp" android:text="显示进度条对话框" /> </RelativeLayout>
修改MainActivity.java文件
package com.example.myandroidprogressdialog; import android.os.Bundle; import android.app.Activity; import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{ //一、声明progressDialog的对象以及按钮 private ProgressDialog prodialog; private Button show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { // TODO Auto-generated method stub //二、绑定 show=(Button) findViewById(R.id.show); //三、设置监听器 show.setOnClickListener(this); } @Override public void onClick(View arg0) { // TODO Auto-generated method stub //四、设置页面显示风格 //新建progressDialog对象 prodialog=new ProgressDialog(MainActivity.this); //给对话框设置显示风格 prodialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //设置标题 prodialog.setTitle("哈哈哈"); //设置对话框里的而文字信息 prodialog.setMessage("我好聪明啊"); //设置图标 prodialog.setIcon(R.drawable.ic_launcher); //五、设定关于progressBar的一些属性 //设定最大进度 prodialog.setMax(100); //设定初始化进度 prodialog.incrementProgressBy(50); //进度条是明确显示进度的 prodialog.setIndeterminate(false); //六、设定一个肯定按钮 prodialog.setButton(DialogInterface.BUTTON_POSITIVE, "肯定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this,"快点爱上我", Toast.LENGTH_SHORT); } }); //七、是否能够经过返回按钮退出对话框 prodialog.setCancelable(true); //八、显示progressDialog prodialog.show(); } }
、
七、自定义ProgressBar的样式
首先咱们须要知道系统预约义的水平进度条的样式是如何加载的。这样咱们须要回到activity_main.xml文件中来。
咱们注意到下面一行代码:
style="?android:attr/progressBarStyleHorizontal"
事实上,他真正的加载文件位于
style="@android:style/Widget.ProgressBar.Horizontal"
咱们经过ctrl+鼠标左键点击引号中的位置,就能够看到系统自带的横向进度条的样式
<style name="Widget.ProgressBar.Horizontal"> <item name="android:indeterminateOnly">false</item> <item name="android:progressDrawable">@android:drawable/progress_horizontal</item> <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item> <item name="android:minHeight">20dip</item> <item name="android:maxHeight">20dip</item> <item name="android:mirrorForRtl">true</item> </style>
其中的android:progressDrawable能够去加载一个本身自定义的布局样式的一个文件。咱们一样用ctrl+鼠标左键打开这里的样式文件,看看系统是如何设定的
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!--背景 --> <item android:id="@android:id/background"> <shape> <!--圆角 --> <corners android:radius="5dip" /> <!--渐变色 --> <gradient <!--起始颜色 --> android:startColor="#ff9d9e9d" <!--中间颜色 --> android:centerColor="#ff5a5d5a" android:centerY="0.75" <!--终止颜色 --> android:endColor="#ff747674" android:angle="270" /> </shape> </item> <!--第二进度条 --> <item android:id="@android:id/secondaryProgress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#80ffd300" android:centerColor="#80ffb600" android:centerY="0.75" android:endColor="#a0ffcb00" android:angle="270" /> </shape> </clip> </item> <!--第一进度条 --> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#ffffd300" android:centerColor="#ffffb600" android:centerY="0.75" android:endColor="#ffffcb00" android:angle="270" /> </shape> </clip> </item> </layer-list>
不难发现,咱们能够本身对他的颜色进行修改,以达到自定义的目的。这里我简单把颜色修改了一下
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!--背景 --> <item android:id="@android:id/background"> <shape> <!--圆角 --> <corners android:radius="5dip" /> <!--渐变色 --> <gradient <!--起始颜色 --> android:startColor="#B9A4FF" <!--中间颜色 --> android:centerColor="#C6B7FF" android:centerY="0.75" <!--终止颜色 --> android:endColor="#C3B2FF" android:angle="270" /> </shape> </item> <!--第二进度条 --> <item android:id="@android:id/secondaryProgress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#57E8FF" android:centerColor="#74EBFF" android:centerY="0.75" android:endColor="#8EEFFF" android:angle="270" /> </shape> </clip> </item> <!--第一进度条 --> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:startColor="#ffffd300" android:centerColor="#ffffb600" android:centerY="0.75" android:endColor="#ffffcb00" android:angle="270" /> </shape> </clip> </item> </layer-list>
运行便可获得自定义进度条的结果,可自行与本文以前的截图对比,能够看到很明显的差异,尽管颜色丑了点。
但愿你们能以小见大,作一些更有意思的尝试。