我在 Android 上使用循环进度条。我想改变它的颜色。我在用
"?android:attr/progressBarStyleLargeInverse"
风格。那么如何改变进度条的颜色。
如何自定义样式?此外,风格的定义是什么?
我在 Android 上使用循环进度条。我想改变它的颜色。我在用
"?android:attr/progressBarStyleLargeInverse"
风格。那么如何改变进度条的颜色。
如何自定义样式?此外,风格的定义是什么?
适用于 API 21 及更高版本。只需设置 indeterminateTint 属性。喜欢:
android:indeterminateTint="@android:color/holo_orange_dark"
要支持 API 21 之前的设备:
mProgressSpin.getIndeterminateDrawable()
.setColorFilter(ContextCompat.getColor(this, R.color.colorPrimary), PorterDuff.Mode.SRC_IN );
在 res/drawable 文件夹中,放置:
progress.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="76dip"
android:height="76dip" />
<gradient
android:type="sweep"
android:useLevel="false"
android:startColor="#447a29"
android:endColor="#00ffffff"
android:angle="0"/>
</shape>
</rotate>
设置startColor
并endColor
根据您的选择。
现在将其设置progress.xml
在ProgressBar
的背景中。
<ProgressBar
android:id="@+id/ProgressBar01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateDrawable="@drawable/progress"
/>
1.首先在resource下的drawable文件夹中创建一个xml文件
命名为“progress.xml”
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" >
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="8"
android:useLevel="false" >
<size
android:height="76dip"
android:width="76dip" />
<gradient
android:angle="0"
android:endColor="color/pink"
android:startColor="@android:color/transparent"
android:type="sweep"
android:useLevel="false" />
</shape>
</rotate>
2.然后使用以下代码段制作进度条
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/relativeLayout1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:indeterminate="true"
android:indeterminateDrawable="@drawable/progress" />
您可以使用以下代码以编程方式更改颜色:
ProgressBar v = (ProgressBar) findViewById(R.id.progress);
v.getIndeterminateDrawable().setColorFilter(0xFFcc0000,
android.graphics.PorterDuff.Mode.MULTIPLY);
如果你想改变 ProgressDialog 的进度条颜色,你可以使用这个:
mDilog.setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
ProgressBar v = (ProgressBar)mDilog.findViewById(android.R.id.progress);
v.getIndeterminateDrawable().setColorFilter(0xFFcc0000,
android.graphics.PorterDuff.Mode.MULTIPLY);
}
});
为了补充 Dato 的答案,我发现 SRC_ATOP 是一个更可取的乘法过滤器,因为它更好地支持 alpha 通道。
ProgressBar v = (ProgressBar) findViewById(R.id.progress);
v.getIndeterminateDrawable().setColorFilter(0xFFcc0000, android.graphics.PorterDuff.Mode.SRC_ATOP);
android:indeterminateTint="@color/progressbar"
样式.xml
<style name="CircularProgress" parent="Theme.AppCompat.Light">
<item name="colorAccent">@color/yellow</item>
</style>
<ProgressBar
android:layout_width="@dimen/d_40"
android:layout_height="@dimen/d_40"
android:indeterminate="true"
style="@style/Widget.AppCompat.ProgressBar"
android:theme="@style/CircularProgress"/>
这对我有用。
<ProgressBar
android:id="@+id/ProgressBar01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateTint="@color/black"/>
要自定义圆形 ProgressBar,我们需要创建一个indeterminateDrawable来显示自定义进度条
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1">
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="8"
android:useLevel="false">
<size
android:height="48dip"
android:width="48dip" />
<gradient
android:centerColor="#f96047"
android:centerY="0.50"
android:endColor="#fb9c8d"
android:startColor="#f72d0c"
android:type="sweep"
android:useLevel="false" />
</shape>
</rotate>
我们需要在进度条中包含drawable,如下所示:
<ProgressBar
android:layout_width="wrap_content"
android:layout_centerInParent="true"
style="?android:attr/progressBarStyleLarge"
android:visibility="visible"
android:progressDrawable="@drawable/widget_progressbar"
android:indeterminateDrawable="@drawable/widget_progressbar"
android:layout_height="wrap_content" />
检查这个答案:
对我来说,这两条线必须在那里才能工作并改变颜色:
android:indeterminateTint="@color/yourColor"
android:indeterminateTintMode="src_in"
PS:但它仅适用于 android 21
对我来说,主题不适用于accentColor。但它确实适用于 colorControlActivated
<style name="Progressbar.White" parent="AppTheme">
<item name="colorControlActivated">@color/white</item>
</style>
<ProgressBar
android:layout_width="@dimen/d_40"
android:layout_height="@dimen/d_40"
android:indeterminate="true"
android:theme="@style/Progressbar.White"/>
<style name="progressColor" parent="Widget.AppCompat.ProgressBar">
<item name="colorControlActivated">@color/colorPrimary</item>
</style>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="250dp"
android:theme="@style/progressColor"
android:layout_height="250dp"
android:layout_centerInParent="true" />
尝试使用样式并将 colorControlActivated 设置为所需的 ProgressBar 颜色。
<style name="progressColor" parent="Widget.AppCompat.ProgressBar">
<item name="colorControlActivated">@color/COLOR</item>
</style>
然后将 ProgressBar 的主题设置为新样式。
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/progressColor"
/>
为了补充Muhamed Riyas M 的最高投票答案:
更快的旋转
android:toDegrees="1080"
较薄的环
android:thicknessRatio="16"
浅白色
android:endColor="#80ffffff"
添加到您的活动主题,项目colorControlActivated,例如:
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="colorControlActivated">@color/rocket_black</item>
...
</style>
将此样式应用于清单中的 Activity:
<activity
android:name=".packege.YourActivity"
android:theme="@style/AppTheme.NoActionBar"/>
您可以使用一种样式来更改进度的颜色,如下所示
<style name="AppTheme.anyName">
<item name="colorAccent">YOUR_COLOR</item>
</style>
并在 ProgressBar 中使用它,如下所示
<ProgressBar
style="?android:attr/progressBarStyle"
android:theme="@style/AppTheme.WhiteAccent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/dimen_30"
/>
希望能帮助到你。
您可以很容易地使用自定义对话框执行此操作,重用其他答案中的 xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="8"
android:useLevel="false" >
<size
android:height="76dip"
android:width="76dip" />
<gradient
android:angle="0"
android:endColor="@color/oceanBlue"
android:startColor="@android:color/transparent"
android:type="sweep"
android:useLevel="false" />
</shape>
只需这样做:
public static class ModifiedProgressDialog extends ProgressDialog {
public ModifiedProgressDialog(Context context) {
super(context);
}
@Override
public void show() {
super.show();
setIndeterminateDrawable(getContext().getResources().getDrawable(R.drawable.blue_progress));
}
}
对于所有想知道如何提高自定义进度速度的人
<?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="1080"><!--HERE YOU COULD INCREASE SPEED BY SETTING TODEGRESS(1080 is 3 loops instead of 1 in same amt of time)-->
<shape android:shape="ring" android:innerRadiusRatio="3"
android:thicknessRatio="8" android:useLevel="false">
<size android:width="76dip" android:height="76dip" />
<gradient android:type="sweep" android:useLevel="false"
android:startColor="#447a29"
android:endColor="#447a29"
android:angle="0"
/>
</shape>
放android:indeterminateDrawable="@drawable/progress_custom_rotate"
使用以下代码自定义圆环进度条
复制下面的代码并在 Drawable 文件夹中创建“progress_custom_rotate.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="1080">
<shape android:shape="ring" android:innerRadiusRatio="3"
android:thicknessRatio="8" android:useLevel="false">
<size android:width="48dip" android:height="48dip" />
<gradient android:type="sweep" android:useLevel="false"
android:startColor="#4c737373" android:centerColor="#4c737373"
android:centerY="0.50" android:endColor="#ffffd300" />
</shape>
</rotate>
它从你的 Res/Values/Colors.xml -> colorAccent 中获取颜色值,如果你改变它,你的progressBar 颜色也会改变。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/black</item>
<item name="colorPrimaryDark">@color/white</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="progressColor" parent="Widget.AppCompat.ProgressBar">
<item name="colorControlActivated">@color/black</item>
</style>
</resources>
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:theme="@style/progressColor"
app:layout_constraintBottom_toBottomOf="@+id/textView3"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toTopOf="@+id/textView3" />
您可以使用以下代码更改进度条颜色:
progressBar.getProgressDrawable().setColorFilter(
getResources().getColor(R.color.your_color), PorterDuff.Mode.SRC_IN);