在.xml佈局文件中聲明一個progressbar例子:
<?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" >
<!--
style="?android:attr/progressBarStyleHorizontal"
?代表引用系統主題
這裏定義了一個水平的進度條
android:visibility="visible" 設置進度條爲可視
-->
<ProgressBar
android:id="@+id/progress1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:visibility="visible"
/>
<!--
style="?android:attr/progressBarStyle"
?代表引用系統主題
這裏定義了一個環形的進度條
android:visibility="gone" 代表當前進度條是不可視的,讓我們的程序把進度條變成可視
-->
<ProgressBar
android:id="@+id/progress2"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="測試progressbar的button"
android:onClick="addProgress"
/>
</LinearLayout>
操作進度條的代碼示例:
public class ProgressBarActivity extends Activity {
private ProgressBar progress1;
private ProgressBar progress2;
private int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progressbar);
progress1=(ProgressBar) findViewById(R.id.progress1);
progress2=(ProgressBar) findViewById(R.id.progress2);
}
//button->onClick
public void addProgress(View v){
if(count==0){
//設置progress2可見
progress2.setVisibility(View.VISIBLE);
}else if(count<=progress1.getMax()){
//進度條默認進度值100
//如果要設置進度條最大刻度,用setMax(...) 或者在.xml中設置android:max=...
progress1.setProgress(count);
//二級進度
progress1.setSecondaryProgress(count+10);
progress2.setProgress(count);
}
count+=10;
}
}