/*
max -- 最大进度
progress -- 第一进度
secondaryProgress -- 第二进度
style -- 样式,大中小水平
*/
<ProgressBar
android:max="100"
android:progress="50"
android:secondaryProgress="80"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_gravity="center_horizontal" />
add_btn = (Button)findViewById(R.id.add_button);
reducer_btn = (Button)findViewById(R.id.reduce_button);
reset_btn = (Button)findViewById(R.id.reset_button);
protext = (TextView)findViewById(R.id.protext);
progressBar = (ProgressBar)findViewById(R.id.progressBar);
show_btn = (Button)findViewById(R.id.dialog_button);
show_btn.setOnClickListener(this);
add_btn.setOnClickListener(this);
reducer_btn.setOnClickListener(this);
reset_btn.setOnClickListener(this);
myProtext();
private void myProtext(){
int first = progressBar.getProgress();
int second = progressBar.getSecondaryProgress();
int max = progressBar.getMax();
protext.setText((int)(first/(float)max*100)+"%"+"=="+(int)(second/(float)max*100)+"%");
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.add_button:{//增加
progressBar.incrementProgressBy(10);
progressBar.incrementSecondaryProgressBy(10);
break;
}
case R.id.reduce_button:{//减少
progressBar.incrementProgressBy(-10);
progressBar.incrementSecondaryProgressBy(-10);
break;
}
case R.id.reset_button:{//充值
progressBar.setProgress(50);
progressBar.setSecondaryProgress(80);
break;
}
case R.id.dialog_button:{//弹出进度弹出框
//初始化
progressDiaog = new ProgressDialog(FourActivity.this);
//显示风格
progressDiaog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//标题
progressDiaog.setTitle("测试");
//对话框内的文字信息
progressDiaog.setMessage("欢迎下载");
//图标
progressDiaog.setIcon(R.drawable.icon);
//最大进度
progressDiaog.setMax(100);
//开始进度
progressDiaog.incrementProgressBy(50);
//明确显示进度精度
progressDiaog.setIndeterminate(false);
//设定按钮
progressDiaog.setButton(DialogInterface.BUTTON_POSITIVE,"确定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(FourActivity.this,"下载结束",Toast.LENGTH_SHORT).show();
}
});
//是否可以通过返回按钮退出对话框
progressDiaog.setCancelable(true);
//显示
progressDiaog.show();
break;
}
}
myProtext();
}
自定义样式
/*
max -- 最大进度
progress -- 第一进度
secondaryProgress -- 第二进度
style -- 样式,大中小水平
默认样式 -- style="?android:attr/progressBarStyleHorizontal"
自定义样式
1.先将style修改为Widget.ProgressBar.Horizontal
2.覆盖其progressDrawable属性,路径为自定义xml
*/
<ProgressBar
android:max="100"
android:progress="50"
android:secondaryProgress="80"
style="@android:style/Widget.ProgressBar.Horizontal"
android:progressDrawable="@drawable/progress_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_gravity="center_horizontal" />
progress_bar.xml
<?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>