另一篇博文:关于自定义progressBar 简单圆形和水平自定义ProgressBar
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();
- }
效果图: