【问题标题】:How to change default ProgressDialog circle color in android如何在android中更改默认ProgressDialog圆圈颜色
【发布时间】:2016-03-05 05:50:09
【问题描述】:

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

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

它就是这样来的

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

我试过了

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

但不工作。

【问题讨论】:

标签: android android-progressbar


【解决方案1】:

试试这个:

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

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

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

【讨论】:

    【解决方案2】:

    在您的应用程序主题中,更改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>
    

    使用任何你想要的颜色

    【讨论】:

      【解决方案3】:

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

      <style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
      <item name="colorAccent">@color/black</item>
      <item name="android:textColorPrimary">@color/white</item>
      <item name="android:background">@color/grey_dialog_box</item>
      </style>
      

      在这个"android:textColorPrimary" 中需要定义你想要显示的颜色,并在java 代码中定义ProgressDialog 的样式,比如:

      ProgressDialog progressDialog = new ProgressDialog(context,R.style.AppCompatAlertDialogStyle);
      

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

      【讨论】:

      • 这不会改变圆形颜色
      • 另外,“show”是静态的,不适用于 ProgressDialog 的新实例。
      • @color/black 这是更改加载器微调器/圆圈颜色的关键值。
      • 仍在 Kotlin 上工作。如前所述,关键是新风格的“colorAccent”。
      【解决方案4】:

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

      <?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 = trueandroid:indeterminateDrawable="@drawable/custom_progress_background" 在要使用加载器的活动布局中

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout 
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/loadingPanel"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:gravity="center">
      
          <ProgressBar
              android:layout_width="wrap_content"
              style="?android:attr/progressBarStyleLarge"
              android:layout_height="wrap_content"
              android:indeterminateDrawable="@drawable/custom_progress_dialog"
              android:indeterminate="true" />
      </RelativeLayout>
      

      【讨论】:

        【解决方案5】:

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

        还可以查看我之前的评论。 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();
        

        【讨论】:

          【解决方案6】:

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

          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>
          

          【讨论】:

            【解决方案7】:

            如果您想临时定义颜色,而不需要继承或专用 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());
            }
            

            【讨论】:

              【解决方案8】:

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

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

              【讨论】:

                【解决方案9】:

                添加样式.xml

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

                设置进度对话框的样式

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

                【讨论】:

                • 最佳答案!只有一个人完全按照提问者的意愿行事。
                • 谢谢这个答案很简单,应该是正确的答案
                【解决方案10】:

                从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);
                

                【讨论】:

                  猜你喜欢
                  • 2015-11-10
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-06-27
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多