ProgressBar滾動體在安卓程序中使用也計較多。
ProgressBar的幾個常用屬性和方法
android:max="200" 滾動條最大值
android:progress="0" 滾動條當前值
android:visibility="visible" 滾動條是否可見
setProgress(int) 設置當前值
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text"
android:textSize="25sp" />
<EditText
android:id="@+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_toRightOf="@id/text"
android:textSize="15sp" />
<!-- 定義滾動條
sytle滾動條樣式:progressBarStyleHorizontal一個長條形
max 滾動條最大值
progress 滾動條當前值
visibility 是否可見
-->
<ProgressBar
android:id="@+id/firstBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/text"
android:layout_below="@+id/text"
android:max="200"
android:maxHeight="48dp"
android:minHeight="48dp"
android:progress="0"
android:visibility="visible" />
<TextView
android:id="@+id/text2"
android:layout_below="@id/firstBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text"
android:textSize="25sp" />
<EditText
android:id="@+id/value2"
android:layout_below="@id/firstBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_toRightOf="@id/text"
android:textSize="15sp" />
<!-- 定義滾動條
sytle滾動條樣式:progressBarStyleLarge一個大圓形樣式
-->
<ProgressBar
android:id="@+id/firstBar2"
style="?android:attr/progressBarStyleLarge"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/text2"
android:layout_below="@+id/text2"
android:max="200"
android:progress="0"
android:visibility="visible" />
<!-- 定義一個標題欄的滾動條
-->
<ProgressBar
android:id="@+id/firstBar3"
style="?android:attr/progressBarStyleSmallTitle"
android:layout_width="200dp"
android:layout_height="wrap_content"/>
</RelativeLayout>
MainActivity:
處理動態加載滾動條
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//請求設置窗口標題欄滾動條
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
pb = (ProgressBar)findViewById(R.id.firstBar);
value = (EditText)findViewById(R.id.value);
//設置滾動條可見
setProgressBarIndeterminateVisibility(true);
//創建一個Handler
mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//處理消息
switch (msg.what) {
case MSG:
//設置滾動條和text的值
pb.setProgress(pro);
value.setText(Integer.toString(pro));
break;
default:
break;
}
}
};
start();
}
private void start()
{
new Thread(new Runnable() {
@Override
public void run() {
int max = pb.getMax();
try {
//子線程循環間隔消息
while (pro < max) {
pro += 10;
Message msg = new Message();
msg.what = MSG;
mHandler.sendMessage(msg);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
效果圖: