自定義progressBar顯示靜態資料

gao_chun發表於2014-08-20

佈局檔案:

<ProgressBar
                    android:progress="50"
                    android:secondaryProgress="70"
                    android:id="@+id/progress_horizontal"
                    style="@style/ProgressBar_Main"
                    android:layout_width="260dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:max="100"/>

樣式檔案:styles.xml

<!-- 自定義progressbar -->
    <style name="ProgressBar_Main" parent="@android:style/Widget.ProgressBar.Horizontal">
        <item name="android:maxHeight">50dip</item>
        <item name="android:minHeight">10dip</item>
        <item name="android:indeterminateOnly">false</item>
        <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
        <item name="android:progressDrawable">@drawable/progressbar_main</item>
    </style>

修改系統預設progressBar檔案:progress_main.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@android:id/background">
        <shape>

            <gradient
                android:angle="270"
                android:centerY="0.75"
                android:endColor="#BEBEBE"
                android:startColor="#BEBEBE" />
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>

                <gradient
                    android:angle="270"
                    android:centerY="0.75"
                    android:endColor="@color/main_color"
                    android:startColor="@color/main_color" />
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape>

                <gradient
                    android:angle="270"
                    android:centerY="0.75"
                    android:endColor="@color/green"
                    android:startColor="@color/green" />
            </shape>
        </clip>
    </item>
</layer-list>

個人在專案中控制ProgressBar資料顯示程式碼塊:

//int goodNum = new Random().nextInt(6) + 1;          //產生1 - 6 的隨機數
//int generalNum=(int) Math.round(Math.random()*(10-6)+6);  //產生 6 - 10 的隨機數
final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progress_horizontal);
        int num = picInfo.getId()%10 ;
        num=num==0?num+1:num;
        if( num <= 6)
        {
            progressBar.setProgress(num*10);//設定第一條資料
            progressBar.setSecondaryProgress(0);//設定第二條資料
        }else if(num > 6 && num <= 10){
            progressBar.setProgress((num>6?6:num)*10);
            progressBar.setSecondaryProgress(num*10);
        }

相關文章