1、ProgressBar的顯示風格
大的環形progressBar: style="?android:attr/progressBarStyleLarge"
中的環形progressBar: style默認
小的環形progressBar: style="?android:attr/progressBarStyleSmall"
水平progressBar: style="?android:attr/progressBarStyleHorizontal"
這里用 style="@android:attr/progressBarStyleHorizontal"結果顯示的還是圈,所以改用了 style="@android:style/Widget.DeviceDefault.ProgressBar.Horizontal",小白不懂原理~~
<ProgressBar
style="@android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="false"
android:progress="50"
android:secondaryProgress="80" />
<ProgressBar
style="@android:style/Widget.DeviceDefault.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="100"
android:progress="50"
android:secondaryProgress="80" />
@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);
//設置進度,總值10000
setProgress(9999);
}
效果:
progress.gif
2、自定義進度條樣式:
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>
main.xml
<ProgressBar
style="@android:style/Widget.DeviceDefault.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="100"
android:progress="50"
android:progressDrawable="@drawable/progress_bar"
android:secondaryProgress="80" />
效果:
Paste_Image.png
對話框形式的進度條:
MainActivity.java
package com.example.day101902;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private ProgressDialog progressDialog;
private Button button;
@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);
//設置進度,總值10000
setProgress(9999);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//初始化
progressDialog = new ProgressDialog(MainActivity.this);
//描述
progressDialog.setMessage("顯示進度");
//標題
progressDialog.setTitle("進度條");
//樣式
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//最大進度
progressDialog.setMax(100);
//已經達到的進度
progressDialog.incrementProgressBy(50);;
//圖標
progressDialog.setIcon(R.drawable.ic_launcher);
//明確顯示進度
progressDialog.setIndeterminate(false);
//設置按鈕
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "返回頁面", Toast.LENGTH_SHORT).show();
}
});
//設置取消
progressDialog.setCancelable(true);
//顯示progressDialog
progressDialog.show();
}
});
}
}
效果:
pb.gif