Android ProgressBar 进度条

来源:互联网 发布:python黑客编程中文pdf 编辑:程序博客网 时间:2024/06/08 20:11

ProgressBar 通过style属性可以指定ProgressBar的风格:
@android:style/Widget.ProgressBar.Horizontal:水平进度条
@android:style/Widget.ProgressBar.Inverse:普通大小的环形进度条
@android:style/Widget.ProgressBar.Large:大环形进度条
@android:style/Widget.ProgressBar.Large.Inverse:大环形进度条
@android:style/Widget.ProgressBar.Small:小环形进度条
@android:style/Widget.ProgressBar.Small.Inverse:小环形进度条
ProgressBar常用XML属性
android:max设置该进度条的最大值
android:progress设置该进度条的已完成进度值
android:progressDrawable设置该进度条的轨道对应的Drawable对象
android:indeterminate该属性设为true,设置进度条不精确显示进度
android:indeterminateDrawable设置绘制不显示进度的进度条的Drawable对象
android:indeterminateDuration设置不精确显示进度条的持续时间
ProgressBar进度操作:
1.setProgress(int):设置进度完成的值
2.incrementProgressBarBy(int):设置进度值增加或减少,参数为正增加,参数为负减少
package shortcut.song.com.myapplication;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.ProgressBar;import java.util.Timer;import java.util.TimerTask;public class ProgressBarActivity extends AppCompatActivity {    ProgressBar mProgressBar1;    ProgressBar mProgressBar2;    int progressed=0;    Timer mTimer = new Timer();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_progress_bar);        mProgressBar1 = (ProgressBar)findViewById(R.id.progressbar1);        mProgressBar2 = (ProgressBar)findViewById(R.id.progressbar2);        mTimer.schedule(new TimerTask() {            @Override            public void run() {                if (progressed < 100)                {                    progressed++;                }else{                    progressed = 0;                }                mProgressBar1.setProgress(progressed);                mProgressBar2.setProgress(progressed);            }        }, 200, 500);    }}

my_progressbar.xml //自定义的进度条轨道样式

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <item android:id="@android:id/background"        android:drawable="@drawable/alert_dialog_icon"        />    <item android:id="@android:id/progress"        android:drawable="@drawable/sun"        /></layer-list>

xml layout 布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_progress_bar"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="shortcut.song.com.myapplication.ProgressBarActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        >        <!-- 大环形 进度条 -->        <ProgressBar            android:layout_width="wrap_content"            android:layout_height="wrap_content"            style="@android:style/Widget.ProgressBar.Large"            />        <!-- 中环形 进度条 -->        <ProgressBar            android:layout_width="wrap_content"            android:layout_height="wrap_content"            />        <!-- 小环形 进度条 -->        <ProgressBar            android:layout_width="wrap_content"            android:layout_height="wrap_content"            style="@android:style/Widget.ProgressBar.Small"            />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical">        <!-- 水平 进度条 -->        <ProgressBar            android:id="@+id/progressbar1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:max="100"            style="@android:style/Widget.ProgressBar.Horizontal"            />        <!-- 自定义轨道的水平 进度条 -->        <ProgressBar            android:id="@+id/progressbar2"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:max="100"            android:progressDrawable="@drawable/my_progressbar"            style="@android:style/Widget.ProgressBar.Horizontal"            />    </LinearLayout></LinearLayout>


0 0