一、ProgressBar简介
ProgressBar是进度条组件,通常用于向用户展示某个耗时操作完成的进度,而不让用户感觉是程序失去了相应,从而更好地提升用户界面的友好性
二、指定ProgressBar显示风格
1、水平进度条:可以精确显示进度(可以显示刻度或者百分比)
2、环形进度条:不可以精确显示进度(一直转,类似过场动画)
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
public class MainActivity extends AppCompatActivity {
@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);
setProgress(600);
}
}
三、关键属性和方法
四、使用ProgressBar实现进度条
(@string快捷键:alt+enter)
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener{
private ProgressBar progress;
private Button add;
private Button reduce;
private Button reset;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//启用窗口特征,启用带进度和不带进度条
requestWindowFeature(Window.FEATURE_PROGRESS);//带
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);//不带
setContentView(R.layout.main);
//显示两种进度条
setProgressBarVisibility(true);
setProgressBarIndeterminateVisibility(true);
setProgress(600);
init();
}
private void init() {
progress = (ProgressBar) findViewById(R.id.horiz);
add = (Button) findViewById(R.id.add);
reduce = (Button) findViewById(R.id.reduce);
reset = (Button) findViewById(R.id.reset);
textView = (TextView) findViewById(R.id.text);
//getPrgress()获取第一进度条的进度
int first = progress.getProgress();
//获取第二进度条的进度
int second = progress.getSecondaryProgress();
//获取进度条的最大进度
int max = progress.getMax();
textView.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 view) {
switch (view.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;
}
}textView.setText("第一进度百分比"+(int)(progress.getProgress()/(float)progress.getMax()*100)+"% 第二进度的百分比"+(int)(progress.getSecondaryProgress()/(float)progress.getMax()*100)+"%");
}
}
五、对话框形式进度条
case R.id.show:
{
//新建ProgressDialog对象
prodialog = new ProgressDialog(MainActivity.this);
//设置显示风格
prodialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置标题
prodialog.setTitle("找男友呀");
//设置对话框里的文字信息
prodialog.setMessage("找呀找呀找朋友");
//设置图标
prodialog.setIcon(R.drawable.gun);
/**
* 设定关于ProgressBar的一些属性
*/
//设定最大进度
prodialog.setMax(100);
//设定初始化已经增长的进度
prodialog.incrementProgressBy(50);
//进度条是明确显示进度的
prodialog.setIndeterminate(false);
/**
*设定一个确定按钮
**/
prodialog.setButton(DialogInterface.BUTTON_POSITIVE,"确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,"hahaah",Toast.LENGTH_SHORT).show();
}
});
//是否可以通过返回按钮退出对话框
prodialog.setCancelable(true);
//显示ProgressDialog
prodialog.show();
break;
}
六、自定义ProgressBar样式
android:progressDrawable="@drawable/progress_bar"
1、自定义progress_bar样式
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<solid android:color="#88000000"/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="270"
android:centerColor="#C6B7FF"
android:centerY="0.75"
android:endColor="#C3B2FF"
android:startColor="#B9A4FF" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="270"
android:centerColor="#74EBFF"
android:centerY="0.75"
android:endColor="#8EEFFF"
android:startColor="#57E8FF" />
</shape>
</clip>
</item>
</layer-list>