无法从我的应用程序主题更改 colorAccent

Can't change colorAccent from my application's theme

我一直在尝试更改进度条的颜色,我注意到它使用的是 accentColor(在我的例子中是蓝色),但我一直在尝试更改它,但没有成功。

我是不是漏掉了什么?

这是我的 styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="searchViewStyle">@style/AppTheme.SearchView</item>
    <item name="editTextColor">@android:color/white</item>
    <item name="actionBarStyle">@style/AppTheme.ActionBarStyle</item>
    <item name="actionOverflowButtonStyle">@style/AppTheme.OverFlowItem</item>
    <item name="colorPrimary">@color/ColorPrimary</item>
    <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
    <item name="colorAccent">@color/ColorPrimaryDark</item>
</style>

这是我在 layout.xml

中的元素
<ProgressBar
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    style="@style/Widget.AppCompat.ProgressBar"
    android:id="@+id/progressBar01"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:indeterminate="true"/>

我的activity:

public class MainActivity extends android.support.v7.app.AppCompatActivity

如果您想更改一个特定视图的强调色,您需要创建样式自己的样式并使用 android:theme 属性将其设置到视图。

已更新

<style name="CustomProgressBarTheme" parent="Widget.AppCompat.ProgressBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimaryCutom</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDarkCustom</item>
    <item name="colorAccent">@color/colorAccentCustom</item>
</style>


<ProgressBar
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:theme="@style/CustomProgressBarTheme"
android:id="@+id/progressBar01"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:indeterminate="true"/>

由于您的 colorAccent 具有该颜色,因此您需要更改 ProgressBar 的颜色:

android:progressBackgroundTint="#a31212" // your color

正如the doc所说:

Tint to apply to the progress indicator background.

最后:

    <ProgressBar
        android:id="@+id/progressBar01"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="120dp"
        android:progressBackgroundTint="#a31212" />

试试这个

 progressBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.ColorPrimaryDark), Mode.SRC_IN);