如何在android中更改默认的ProgressDialog圆圈颜色

时间:2015-12-01 10:31:01

标签: android android-progressbar

我正在使用ProgressDialog来显示进度条

            ProgressDialog progressDialog = new ProgressDialog(context);
            progressDialog.setCancelable(false);
            progressDialog.setMessage(message);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.show();

它就像这样来了

enter image description here

我想将圆圈的绿色更改为红色。有什么办法吗?

我试过

.getIndeterminateDrawable().setColorFilter(0xFFFFFFFF, android.graphics.PorterDuff.Mode.MULTIPLY);

但不行。

10 个答案:

答案 0 :(得分:43)

在style.xml中为对话框创建样式:

"android:textColorPrimary"

在此ProgressDialog需要定义要显示的颜色,并在java代码中定义ProgressDialog progressDialog = new ProgressDialog(context,R.style.AppCompatAlertDialogStyle); 的样式,如:

 where ([num]% 2) = 0

更多:http://androidprogressdialogcustomcolor.blogspot.in/

答案 1 :(得分:9)

enter image description here

为可在drawable文件夹中命名为custom_progress_dialog.xml的进度对话框创建布局。

Client.Me.Calendars["<valid calendar id>"].CalendarView(from, to)

确保<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0" android:toDegrees="360"> <shape android:shape="ring" android:innerRadiusRatio="3" android:thicknessRatio="8" android:useLevel="false"> <size android:width="56dip" android:height="56dip" /> <gradient android:type="sweep" android:useLevel="false" android:startColor="@android:color/transparent" android:endColor="#1e9dff" android:angle="0" /> </shape> </rotate> indeterminate = true  在要使用加载程序的活动布局中

android:indeterminateDrawable="@drawable/custom_progress_background"

答案 2 :(得分:9)

添加Style.xml

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorAccent">@color/colorPrimary</item>
</style>

将样式设置为进度对话框

pdialog = new ProgressDialog(context, R.style.MyAlertDialogStyle);

答案 3 :(得分:7)

这不是一个完美的解决方案,但可能有所帮助。

同时查看我以前的评论。 https://stackoverflow.com/a/39687893/2598453

final ProgressDialog progress = new ProgressDialog(this);
progress.setMessage(getString(R.string.progress_message));
progress.setIndeterminate(true);
progress.setCancelable(false);

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    Drawable drawable = new ProgressBar(this).getIndeterminateDrawable().mutate();
    drawable.setColorFilter(ContextCompat.getColor(this, R.color.colorAccent),
            PorterDuff.Mode.SRC_IN);
    progress.setIndeterminateDrawable(drawable);
}

progress.show();

答案 4 :(得分:4)

试试这个:

将颜色设置为红色的示例:

ProgressBar spinner = new android.widget.ProgressBar(
                context,
                null,
                android.R.attr.progressBarStyle);

spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);

答案 5 :(得分:3)

应用主题中,更改 colorAccent

<style name="AppTheme.Bash" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">Add your color here</item>
    <item name="android:windowBackground">@color/gray</item>
</style>

使用你想要的任何颜色

答案 6 :(得分:2)

从ProgressDialog正在使用的android资源中获取资源。

final ProgressDialog progress = new ProgressDialog(context);
progress.setTitle("Loading");
progress.setMessage("Wait while loading...");
progress.setCancelable(false); // disable dismiss by tapping outside of the dialog
progress.show();
ProgressBar progressbar=(ProgressBar)progress.findViewById(android.R.id.progress);
progressbar.getIndeterminateDrawable().setColorFilter(Color.parseColor("#C60000"), android.graphics.PorterDuff.Mode.SRC_IN);

答案 7 :(得分:1)

自定义颜色进度条的完美而简单的解决方案。 Custom Progress bar

public class CustomProgressBar extends ProgressBar{
    public CustomProgressBar(Context context) {
        super(context);
        this.setIndeterminate(true);
        this.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.app_blue), PorterDuff.Mode.MULTIPLY);
    }

    public CustomProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.app_blue), PorterDuff.Mode.MULTIPLY);
    }

    public CustomProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}

并在布局中使用它。

<RelativeLayout
        android:id="@+id/loadingPanel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:visibility="gone">

        <com.mayproject.views.CustomProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>

答案 8 :(得分:0)

如果您想定义ad-hoc颜色,而不需要继承或专用XML,您可以使用以下内容:

/** Styles a ProgressDialog to use a themed Spinner. */
public void setStyledProgressDialog(final Context pContext, final ProgressDialog pProgressDialog, final int pColorResourceId) {
    // Allocate a new ProgressBar.
    final ProgressBar lProgressBar = new android.widget.ProgressBar(pContext, null, android.R.attr.progressBarStyle);
    // Configure the IndeterminateDrawable.
    lProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(pContext, pColorResourceId), android.graphics.PorterDuff.Mode.MULTIPLY);
    // Assign the ProgressBar to the ProgressDialog.
    pProgressDialog.setIndeterminateDrawable(lProgressBar.getIndeterminateDrawable());
}

答案 9 :(得分:0)

如果您已经使用了材质对话框库(https://github.com/afollestad/material-dialogs#indeterminate-progress-dialogs),那么最简单的解决方案就是使用该库中的进度对话框。圆圈将自动用原色着色:

MaterialDialog dialog = new MaterialDialog.Builder(context)
                    .content(R.string.loading)
                    .progress(true, 0)
                    .show();