標籤:
ProgressBar
ProgressBar,進度條,是AndroidUI介面中一個非常實用的組件,通常用於向使用者顯示某個耗時操作完成的百分比。因此它需要動態顯示進度,從而避免長時間的執行某個耗時的操作,而讓使用者感覺程式失去了相應,從而提高介面的友好性。
從官方文檔上看,為了適應不同的應用環境,Android內建了幾種風格的進度條,可以通過Style屬性設定ProgressBar的風格。支援如下屬性,後面在樣本中會一一展示:
- @android:style/Widget.ProgressBar.Horizontal:水平進度條(可以顯示刻度,常用)。
- @android:style/Widget.ProgressBar.Small:小進度條。
- @android:style/Widget.ProgressBar.Large:大進度條。
- @android:style/Widget.ProgressBar.Inverse:不斷跳躍、旋轉畫面的進度條。
- @android:style/Widget.ProgressBar.Large.Inverse:不斷跳躍、旋轉動畫的大進度條。
- @android:style/Widget.ProgressBar.Small.Inverse:不斷跳躍、旋轉動畫的小進度條。
只有Widget.ProgressBar.Horizontal風格的進度條,才可以設定進度的遞增,其他的風格展示為一個迴圈的動畫,而設定Widget.ProgressBar.Horizontal風格的進度條,需要用到一些屬性設定遞增的進度,這些屬性都有對應的setter、getter方法,這些屬性如下:
- android:max:設定進度的最大值。
- android:progress:設定當前第一進度值。
- android:secondaryProgress:設定當前第二進度值。
- android:visibility:設定是否顯示,預設顯示。
對於Widget.ProgressBar.Horizontal風格的進度條而言,在代碼中動態設定移動量,除了可以使用setProgress(int)方法外,Android還為我們提供了另外一個incrementProgressBy(int)方法,它與setProgress(int)的根本區別在於,setProgress(int)是直接設定當前進度值,而incrementProgressBy(int)是設定當前進度值的增量(正數為增,負數為減)。與setProgress(int)和incrementProgressBy(int)對應的還有setSecondaryProgress(int)和incrementSecondaryProgressBy(int)方法,用於設定第二進度值。
以下是xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >"
<TextView android:id="@+id/textviewHor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Widget.Progressbar.Horizontal"/>
<ProgressBar
android:id="@+id/progessbarHor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Horizontal"
android:max="100"
android:progress="20"
android:secondaryProgress="40"
android:visibility="visible"/>
<TextView android:id="@+id/textviewSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Widget.Progressbar.Small"/>
<ProgressBar android:id="@+id/progressbarSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Small"/>
<TextView android:id="@+id/textviewLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Widget.Progressbar.Large"/>
<ProgressBar android:id="@+id/progressbarLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Large"/>
<TextView android:id="@+id/textviewInverse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Widget.Progressbar.Inverse"/>
<ProgressBar android:id="@+id/progressbarInverse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@android:style/Widget.ProgressBar.Inverse"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"/>
<Button android:id="@+id/buttonRdc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="-"/>
<Button android:id="@+id/buttonVsb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="Visible"/>
</LinearLayout>
</LinearLayout>
以下是代碼:
package com.example.widget;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
private ProgressBar pbHor, pbLarge;//聲明進度條控制項
private Button btAdd, btReduce, btVisible;//聲明按鈕控制項
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progessbar);
//獲得控制項
pbHor = (ProgressBar)findViewById(R.id.progessbarHor);
pbLarge = (ProgressBar)findViewById(R.id.progressbarLarge);
btAdd = (Button)findViewById(R.id.buttonAdd);
btReduce = (Button)findViewById(R.id.buttonRdc);
btVisible = (Button)findViewById(R.id.buttonVsb);
//點擊消失ProgressBar.Large控制項
btVisible.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(pbLarge.getVisibility() == View.VISIBLE) {
pbLarge.setVisibility(View.GONE);
} else {
pbLarge.setVisibility(View.VISIBLE);
}
}
});
//點擊增加或減少Horizontal進度
Button.OnClickListener buttonListener = new Button.OnClickListener() {
public void onClick(View v) {
switch(v.getId()) {
case R.id.buttonAdd :
if(pbHor.getProgress() < 90) {
pbHor.setProgress((int) (pbHor.getProgress() * 1.2));
}
if(pbHor.getProgress() < 100) {
pbHor.setSecondaryProgress((int) (pbHor.getProgress() * 1.2));
}
break;
case R.id.buttonRdc:
if(pbHor.getProgress() > 10) {
pbHor.setProgress((int) (pbHor.getProgress() / 1.2));
}
if(pbHor.getProgress() > 20) {
pbHor.setSecondaryProgress((int) (pbHor.getProgress() / 1.2));
}
break;
}
}
};
btAdd.setOnClickListener(buttonListener);
btReduce.setOnClickListener(buttonListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
安卓學習————安卓控制項之ProgressBar