Android进度条样式

时间:2015-11-04 16:56:35

标签: android

我的ProgressBar上有3个问题。首先,我想将ProgressBar设置为使用带有大条形的旧黄色样式。然后,我想设置ProgressBar的颜色。最后,我想设置ProgressBar的边距。我的代码如下

ProgressBar statusPB = new ProgressBar(this, null, android.R.attr.progressBarStyleLarge);
                    statusPB.setId(300 + i);
                    statusPB.setProgress(mProgressStatus);
                    statusPB.getIndeterminateDrawable().setColorFilter(Color.parseColor("#D69E29"), android.graphics.PorterDuff.Mode.MULTIPLY);
                    TableRow.LayoutParams params = new TableRow.LayoutParams(160, 80);
                    params.setMargins(0, 0, 5, 0);
                    statusPB.setLayoutParams(params);
                    tr.addView(statusPB);

他们三个都没有工作。将android.R.attr.progressBarStyleLarge更改为android.R.attr.progressBarStyleHorizontal会创建一个蓝色的新小进度条。使用setColrFilter设置颜色也不起作用。

需要一些帮助。谢谢!

2 个答案:

答案 0 :(得分:0)

我认为您最好在drawable中创建自定义进度条,并设置所需内容:

custom_progressbar.xml     

<!-- Define the background properties like color etc -->
<item android:id="@android:id/background">
    <shape >
        <gradient
                android:startColor="#F1C40F"
                android:centerColor="#F1C40F"
                android:centerY="1.0"
                android:endColor="#F1C40F"
                android:angle="270"
        />
    </shape>
</item>

<!-- Define the progress properties like start color, end color etc -->
<item android:id="@android:id/progress">
    <clip android:gravity="right">
        <shape>
            <gradient
                android:startColor="#56616D"
                android:centerColor="#56616D"
                android:centerY="1.0"
                android:endColor="#56616D"
                android:angle="270"
            />
        </shape>
    </clip>
</item>

在styles.xml中定义样式:

<style name="CustomProgressBarHorizontal" parent="android:Widget.ProgressBar.Horizontal">
    <item name="android:progressDrawable">@drawable/custom_progressbar</item>
    <item name="android:minHeight">4dip</item>
    <item name="android:maxHeight">4dip</item>
</style>

然后使用它:

<ProgressBar
    android:id="@+id/progress_bar"
    style="@style/CustomProgressBarHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

答案 1 :(得分:0)

Try:

ProgressBar statusPB = new ProgressBar(this, null, android.R.attr.progressBarStyleLarge);

            Drawable drawable = getResoures().getDrawable(R.drawable.a);
                drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());

                   statusPB .setProgressDrawable(R.drawable.custom_progressbar)
                    statusPB.setId(300 + i);
                    statusPB.setProgress(mProgressStatus);
                    statusPB.getIndeterminateDrawable().setColorFilter(Color.parseColor("#D69E29"), android.graphics.PorterDuff.Mode.MULTIPLY);
                    TableRow.LayoutParams params = new TableRow.LayoutParams(160, 80);
                    params.setMargins(0, 0, 5, 0);
                    statusPB.setLayoutParams(params);
                    tr.addView(statusPB);