2015-12-01 57 views
21

默認ProgressDialog圈顏色,我使用ProgressDialog用於顯示進度如何改變Android的

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

但不工作。

+0

http://stackoverflow.com/questions/4908289/styling-progress-dialog-in-android –

+2

的可能的複製[如何改變進度條的進度顏色在Android](http://stackoverflow.com/questions/2020882/how-to-change-progress-bars-progress-color-in-android) –

+0

可能是一個重複的問題。請檢查this

回答

4

試試這個:

例子,其中設置顏色爲紅色:

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

spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY); 
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> 

使用什麼顏色,你想

+1

在API21上不起作用 – PassKit

31

在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/

+5

這不會改變圓形顏色 – Abhishek

+0

此外,「show」是靜態的,並且不適用於ProgressDialog的新實例。 – mwieczorek

+0

這不支持棒棒糖(API 21) –

4

enter image description here

創建進度對話框的佈局,作爲自定義命名佈局文件夾中的_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> 
6

這不是一個完美的解決方案,但可能會有幫助。

此外,請檢查我以前的評論。 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(); 
0

用於自定義色彩進度條的完善和簡單的解決方案。 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> 
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()); 
}