xml中通过indeterminateTint设置圆形进度条颜色,此方法不兼容Android21以下的版本,若需要兼容Android21以下的版本,使用方法2即可
<ProgressBar
android:indeterminateTint="#803F51B5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
代码中设置:
ProgressBar circleProgress = findViewById(R.id.circle_progress);
circleProgress.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.colorPrimaryDark), PorterDuff.Mode.SRC_ATOP);
xml中通过backgroundTint、progressTint、secondaryProgressTint设置,此方法不兼容Android21以下的版本,若需要兼容Android21以下的版本,使用方法2即可
<ProgressBar
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:backgroundTint="@android:color/darker_gray"
android:progressTint="@color/colorPrimary"
android:secondaryProgressTint="#803F51B5"
android:progress="25"
android:secondaryProgress="50"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
使用progressDrawable设置,新建bg_horizontal_progress.xml,放在drawable文件夹下:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<solid android:color="@android:color/darker_gray" />
shape>
item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<solid android:color="#803F51B5" />
shape>
clip>
item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<solid android:color="@color/colorPrimaryDark" />
shape>
clip>
item>
layer-list>
如果只需要一级进度条颜色,把二级进度条去掉即可,即:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<solid android:color="@android:color/darker_gray" />
shape>
item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<solid android:color="@color/colorPrimaryDark" />
shape>
clip>
item>
layer-list>
在布局中使用bg_horizontal_progress.xml:
<ProgressBar
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:progressDrawable="@drawable/bg_horizontal_progress"
android:progress="25"
android:secondaryProgress="50"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical">
<TextView
android:text="1.通过indeterminateTint设置:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar
android:indeterminateTint="#803F51B5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="2.通过circleProgress.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.colorPrimaryDark), PorterDuff.Mode.SRC_ATOP)设置:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar
android:id="@+id/circle_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="3.通过backgroundTint、progressTint、secondaryProgressTint设置:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:backgroundTint="@android:color/darker_gray"
android:progressTint="@color/colorPrimary"
android:secondaryProgressTint="#803F51B5"
android:progress="25"
android:secondaryProgress="50"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="4.通过progressDrawable设置:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:progressDrawable="@drawable/bg_horizontal_progress"
android:progress="25"
android:secondaryProgress="50"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
LinearLayout>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<solid android:color="@android:color/darker_gray" />
shape>
item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<solid android:color="#803F51B5" />
shape>
clip>
item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<solid android:color="@color/colorPrimaryDark" />
shape>
clip>
item>
layer-list>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ProgressBar circleProgress = findViewById(R.id.circle_progress);
circleProgress.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.colorPrimaryDark), PorterDuff.Mode.SRC_ATOP);
}
}