一、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>