【问题标题】:How to set default color of progress bar如何设置进度条的默认颜色
【发布时间】:2018-05-27 17:19:12
【问题描述】:

我想在整个应用程序中更改 ProgressBar 的颜色。 Android 文档表明,如果我使用AppCompat,它将使用colorAccent 作为进度条的颜色。但它没有在我的应用程序中使用它。这是我的styles.xml

<resources>

<!-- Application theme -->
<style name="AppTheme" parent="AppTheme.Base">
</style>

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppTheme.Base" parent="Theme.AppCompat">
<item name="android:colorPrimary">@color/colorPrimary</item>
<item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:colorAccent">@color/colorAccent</item>

<item name="android:windowNoTitle">false</item>
<item name="android:windowActionBar">true</item>
<item name="android:windowActionModeOverlay">true</item>

<item name="android:windowBackground">@color/white</item>

<item name="android:windowFullscreen">false</item>

<item name="android:textCursorDrawable">@null</item>
</style>

<style name="AppTheme.Login" parent="AppTheme.Base">
<item name="android:colorControlNormal">@color/gray</item>
<item name="android:colorControlActivated">@color/colorPrimary</item>
<item name="android:colorControlHighlight">@color/med_gray</item>

<item name="android:statusBarColor">@color/dark_gray</item>
</style>

<style name="AppTheme.Menu" parent="AppTheme.Base">
<item name="android:colorPrimary">@color/black</item>
</style>

<style name="AppTheme.Splash" parent="AppTheme.Base">
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
</style>

<style name="login_button_style">
<item name="android:textSize">@dimen/text_small</item>
<item name="android:textAllCaps">false</item>
</style>

</resources>

【问题讨论】:

  • 检查this 答案。和你的问题类似。

标签: android progress-bar


【解决方案1】:

如果@RuslanMatveev 作为评论给出的建议不起作用,您可以尝试以下方法。

<style name="MyProgressBar" parent="@style/Widget.AppCompat.ProgressBar">
    <item name="android:progressBackgroundTint">@color/colorAccent</item>
    <item name="android:progressTint">@color/colorAccent</item> 
</style>

然后在您的 AppTheme.Base 中设置以下参数

<item name="android:progressBarStyleHorizontal">@style/MyProgressBar</item>

<item name="android:progressBarStyle">@style/MyProgressBar</item

更新

如果上述解决方案改变了进度条的形状,您可以尝试将以下行添加到您的 AppTheme.base

<item name="colorControlActivated">@color/accent</item>

【讨论】:

  • 这几乎对我有用。它改变了颜色,但它把我的旋转进度条变成了一个水平条。
  • 我更新了我的答案。您可以删除 @style/Widget.AppCompat.ProgressBar 前面的 .Horizo​​ntal ,它应该可以工作
  • 如果我去掉它,颜色就不会再改变了
  • 您可以尝试在您的 AppTheme.base 上设置 @color/accent 并删除其余部分吗?
  • 是的,这是唯一对我有用的东西!将其附加到您的帖子中 - 将您的原始答案也留在那里也没有什么坏处,因为这适用于单杠。
【解决方案2】:

试试这个

  • 您必须在您的drawable 中创建progress

<?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"
    android:toDegrees="360" >

    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="15"
        android:useLevel="false" >
        <size
            android:height="48dip"
            android:width="48dip" />

        <gradient
            android:centerColor="@android:color/white"
            android:centerY="0.50"
            android:endColor="@color/colorYellow"
            android:startColor="@color/colorYellow"
            android:type="sweep"
            android:useLevel="false" />
    </shape>

</rotate>

  • 现在您可以使用progress 来更改progressbar 的颜色

<ProgressBar
             android:id="@+id/progressBar"
             style="?android:attr/progressBarStyleLarge"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerHorizontal="true"
             android:layout_gravity="center"
             android:layout_centerVertical="true"
             android:indeterminateDrawable="@drawable/progress" >
         </ProgressBar>

【讨论】:

  • 从我在这里阅读的内容来看,这适用于很多人,但不适用于我。 Ruan_Lopes 的答案是唯一对我有用的东西。
猜你喜欢
  • 2022-10-18
  • 2011-09-19
  • 1970-01-01
  • 2016-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-24
  • 2011-02-27
相关资源
最近更新 更多