【问题标题】:Can't change colorAccent from my application's theme无法从我的应用程序主题更改 colorAccent
【发布时间】:2016-02-11 15:06:15
【问题描述】:

我一直在尝试更改进度条的颜色,我注意到它使用了强调颜色(在我的情况下是蓝色),我一直在尝试更改它,但没有运气。

我错过了什么吗?

这是我的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"/>

我的活动:

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

【问题讨论】:

  • without luck 是什么意思到目前为止您尝试过什么?它如何不工作的任何结果?因为你有"colorAccent"&gt;@color/ColorPrimaryDark,颜色来自ColorPrimaryDark
  • 是的,那是深红色,&lt;color name="ColorPrimaryDark"&gt;#DD2C00&lt;/color&gt;
  • 谢谢 :) 您说:在我的情况下是蓝色,所以 AccentColor 应该是 #DD2C00。但这不是蓝色的吧?

标签: android android-layout android-styles


【解决方案1】:

如果您想为某个特定视图更改强调色,您需要创建自己的样式并使用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"/>

【讨论】:

    【解决方案2】:

    试试这个

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

    【讨论】:

      【解决方案3】:

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

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

      正如the doc 所说:

      应用于进度指示器背景的色调。

      最后:

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

      【讨论】:

      • 那是什么? -> I've been trying to change the color of the progressBar
      • @Gazta - 你能说清楚吗?您一直在尝试更改AccentColor 或此ProgressBar?它正在使用上面的代码,有趣..
      • 就像我猜的那样,它在 Android Studio 的预览版上不可见,但它正在 emulator.goodluck 上运行
      • 有哪些版本?因为它应该只适用于 +21 apis
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-19
      • 1970-01-01
      • 1970-01-01
      • 2017-06-27
      • 2013-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多