【问题标题】:Change Horizontal Progress Bar Color更改水平进度条颜色
【发布时间】:2016-09-29 13:33:19
【问题描述】:

问题描述

我正在尝试使用style.xml 中定义的自定义样式更改ProgressBar 颜色

layout.xml

<ProgressBar
        android:id="@+id/progress_bar"
        style="@style/ProgressTheme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:indeterminateBehavior="repeat" />

style.xml

<style name="ProgressTheme" parent="Widget.AppCompat.ProgressBar.Horizontal">
        <item name="colorAccent">#000</item>
</style>

问题

我找到了一些关于如何以编程方式更改颜色或添加一些layer-list 的有用文章,但我想要简单的解决方案。而且我想我实施的方式应该可行。

如何改变颜色?

【问题讨论】:

  • @nicopasso 这不是重复的,因为我需要具体的解决方案,我需要知道有没有办法直接从 style.xml 中做到这一点
  • 您的问题是“我怎样才能改变颜色?”。已经有很多答案了。作为旁注,主题解决方案仅适用于 API Level >= 21
  • 检查我更新的答案。

标签: android styles progress-bar


【解决方案1】:

您可以尝试使用:

mProgressBar.getProgressDrawable().setColorFilter(Color.BLACK, android.graphics.PorterDuff.Mode.SRC_IN);

对于棒棒糖及以上,使用:

mProgressBar.setProgressTintList(ColorStateList.valueOf(Color.BLACK));

如果寻找 xml 解决方案,请尝试ProgressBar (Api 21 +) 的这些属性:

android:indeterminateTintMode="src_atop"
android:indeterminateTint="@android:color/black"

在styles.xml(v21) 中创建以下&lt;style.../&gt; 适用于Api 21+:

<style name="blackProgressBar" parent="Widget.AppCompat.ProgressBar.Horizontal">
    <item name="android:indeterminateTintMode">src_atop</item>
    <item name="android:indeterminateTint">@android:color/black</item>
</style>

并将其应用为:

<ProgressBar
    ...
    style="@style/blackProgressBar" />

【讨论】:

  • 我只需要使用样式的解决方案(我在我的问题中提到过)。
【解决方案2】:

您可以通过以下方式设置进度对话框的样式:使用您的环形创建一个可绘制文件 (circular_progress_dialog.xml)

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="270"
android:toDegrees="270">
<shape
    android:innerRadiusRatio="2.5"
    android:shape="ring"
    android:thickness="2dp"
    android:useLevel="true"><!-- this line fixes the issue for lollipop api 21 -->

    <gradient
        android:angle="0"
        android:endColor="@color/main_background"
        android:startColor="@color/main_orange"
        android:type="sweep"
        android:useLevel="false" />
</shape>
</rotate>

一个样式你的进度条

 <style name="ProgressBar" parent="@android:style/Widget.ProgressBar">
    <item name="android:layout_centerHorizontal">true</item>
    <item name="android:layout_centerVertical">true</item>
    <item name="android:visibility">gone</item>
    <item name="android:background">@color/transparency_pleca</item>
    <item name="android:indeterminateDrawable">@drawable/circular_progress_dialog</item>
</style>

注意你的“android:indeterminateDrawable”属性

您可以创建 ProgressDialog 的实例并添加您的样式

// create progressDialog
final ProgressDialog myProgressDialog = new  ProgressDialog(MyActivity.this);
myProgressDialog.setProgressStyle(R.style.ProgressBar)
myProgressDialog.show();

【讨论】:

    【解决方案3】:

    可能会迟到,但我把它放在这里是为了其他可能遇到同样问题的人。

    在您的 styles.xml 文件中,为您的进度条添加新样式

    <style name="CustomProgressBarTheme">
        <item name="colorAccent">@color/myProgressBarColor</item>
        <item name="android:progressBarStyle">@style/Widget.AppCompat.ProgressBar.Horizontal</item>
    </style>
    

    android:progressBarStyle 属性确保进度条是水平的。

    只需将此样式添加到您的进度条,然后设置android:indeterminate="true"。

    <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:theme="@style/slang_lib_ProgressBarTheme" />
    

    注意:在这里使用style 属性不起作用,请改用android:theme。

    【讨论】:

      【解决方案4】:

      你需要添加 android:indeterminateTint="@color/red"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-02
        • 2014-03-27
        • 2014-10-08
        相关资源
        最近更新 更多