ProgressBar的使用實(shí)例

以下內(nèi)容有三方式設(shè)置進(jìn)度條

設(shè)置一個(gè)簡單的進(jìn)度條

布局

        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:max="100"
        android:progressDrawable="@drawable/pg"
        android:progress="50"
        android:secondaryProgress="80"
        android:layout_height="20dp" />
 protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //啟動窗口特征(帶進(jìn)度條和不帶進(jìn)度條)
        requestWindowFeature(Window.FEATURE_PROGRESS);
        //不帶
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_process);
        //顯示倆種進(jìn)度條
         setProgressBarVisibility(true);
        setProgressBarIndeterminate(true);
        setProgress(600);

效果圖


image.png

設(shè)置一個(gè)進(jìn)度條,類似于歌曲播放一樣,顯示不同顏色的進(jìn)度,來設(shè)置三個(gè)按鈕對其進(jìn)行講解

        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="80"
        android:layout_height="20dp" />
    <Button
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add"
        android:id="@+id/add"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/delcline"
        android:id="@+id/decline" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/reset"
        android:id="@+id/reset"/>
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

 btn_add=(Button) findViewById(R.id.add);
        btn_decline=(Button)findViewById(R.id.decline);
        btn_reset=(Button) findViewById(R.id.reset);
        dialog=(Button) findViewById(R.id.btn1);
        progressBar=(ProgressBar)findViewById(R.id.progressBar);
        textView=(TextView)findViewById(R.id.text);
       int first= progressBar.getProgress();
        int second=progressBar.getSecondaryProgress();
        int max=progressBar.getMax();
        textView.setText("第一進(jìn)度百分比:"+(int)(first/(float)max*100)+"%"+"第二進(jìn)度百分比:"+(int)(second/(float)max*100)+"%");
        btn_add.setOnClickListener(this);
        btn_decline.setOnClickListener(this);
        btn_reset.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
     switch (v.getId()){
         case R.id.add:{
             progressBar.incrementProgressBy(10);
             progressBar.incrementSecondaryProgressBy(10);
             break;
         }
         case R.id.decline:{
             progressBar.incrementProgressBy(-10);
             progressBar.incrementSecondaryProgressBy(-10);
             break;
         } case R.id.reset:{
             progressBar.setProgress(50);
             progressBar.setSecondaryProgress(80);
             break;
         }
         }
     }
        textView.setText("第一進(jìn)度百分比:"+(int)(progressBar.getProgress()/(float)progressBar.getMax()*100)+"%"+"第二進(jìn)度百分比:"+(int)(progressBar.getSecondaryProgress()/(float)progressBar.getMax()*100)+"%");

    }

效果圖


zaidierzh

設(shè)置一個(gè)在對話框里顯示的進(jìn)度條(在第二種設(shè)置的基礎(chǔ)上添加一個(gè)按鈕)

//       帶有彈框的進(jìn)條
         }case R.id.btn1:{
             //基礎(chǔ)頁面顯示風(fēng)格
             progressDialog=new ProgressDialog(ProgressBarActivity.this);
             progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
             progressDialog.setTitle("晨曦詩雨");
             progressDialog.setMessage("歡迎來看晨曦分享小知識");
             progressDialog.setIcon(R.drawable.ic_my);
             //設(shè)置進(jìn)度條的屬性
             progressDialog.setMax(100);
             progressDialog.incrementProgressBy(50);
             //明確顯示進(jìn)度
             progressDialog.setIndeterminate(false);
             //設(shè)置確定按鈕
             progressDialog.setButton(DialogInterface.BUTTON_POSITIVE,"確定", new DialogInterface.OnClickListener() {
                 @Override
                 public void onClick(DialogInterface dialog, int which) {
                     Toast.makeText(ProgressBarActivity.this,"歡迎支持我的分享",Toast.LENGTH_SHORT).show();

                 }
             });
             //是否通過返回按鈕退出對話框
             progressDialog.setCancelable(true);
             progressDialog.show();

             break;
}

效果圖


image.png

設(shè)置自定義形式的進(jìn)度條,改變其顏色,樣式(自定設(shè)定一個(gè)樣式的文件,在頁面中引入)

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="2dp" />
            <gradient
                android:angle="270"
                android:centerColor="#E3E3E3"
                android:endColor="#E6E6E6"
                android:startColor="#C8C8C8" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="2dp" />
                <gradient
                    android:centerColor="#f1cd06"
                    android:endColor="#fff701"
                    android:startColor="#dace27" />

            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="2dp" />
                <gradient
                    android:centerColor="#4AEA2F"
                    android:endColor="#31CE15"
                    android:startColor="#5FEC46" />

            </shape>
        </clip>
    </item>

</layer-list>

引入此樣式的設(shè)置屬性

 android:progressDrawable="@drawable/pg"

ProgressBar是一個(gè)進(jìn)度條,展示給用戶某個(gè)耗時(shí)操作的完成程度


image.png

image.png

可以再標(biāo)題上來設(shè)置進(jìn)度條(切換一個(gè)頁面加載數(shù)據(jù)時(shí)就會使用到這個(gè)功能)

image.png

image.png

注意:不顯示精確度是false,反之為true


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,756評論 4 61
  • 此身要報(bào)國 何須脫口說 我心早已現(xiàn) 戰(zhàn)死也心愿 (c27)
    曹廣潼樹根草閱讀 157評論 0 5
  • 今天去接一個(gè)案子,但是由于自己的過分不自信在表達(dá)過程當(dāng)中會有一些問題,雖然現(xiàn)在的結(jié)果還沒有出來,但是我對自己有...
    蓮心巧克閱讀 274評論 0 0
  • 人的內(nèi)心承受不起太多的真實(shí) 這是不爭的事實(shí) 因?yàn)椴辉敢獬姓J(rèn)自己的自私和虛偽 所以寧愿不知道
    原來是你吶閱讀 428評論 0 0

友情鏈接更多精彩內(nèi)容