【问题标题】:How to change the color of the Progress Bar in Android?- (I tried one way,and it isn't working) [duplicate]如何更改Android中进度条的颜色?-(我尝试了一种方法,但它不起作用)[重复]
【发布时间】:2023-04-02 07:36:01
【问题描述】:

我使用以下代码在我的活动中添加了一个进度条:

<LinearLayout
    android:id="@+id/linlaHeaderProgress"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:visibility="gone" >

    <ProgressBar
        android:id="@+id/pbHeaderProgress"
        android:indeterminateOnly="true"
        android:keepScreenOn="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ProgressBar>
</LinearLayout>

然后我叫它:

 progressbar = (LinearLayout) findViewById(R.id.linlaHeaderProgress);
 progressbar.setVisibility(View.VISIBLE);

显示进度条,我想更改它的颜色。默认情况下,进度条以灰色显示。这是我尝试更改颜色的内容:

我在drawables文件夹中创建了一个xml文件并将其命名为activityindicator.xml 这个xml的内容是:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@android:id/secondaryProgress">


        <color android:color="#f58233" />
    </item>
    <item android:id="@android:id/progress">

        <color android:color="#f58233" />
    </item>

</layer-list>

我将布局文件更改为:

<LinearLayout
    android:id="@+id/linlaHeaderProgress"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:progressDrawable="@drawable/activityindicator"
    android:orientation="vertical"
    android:visibility="gone" >

    <ProgressBar
        android:id="@+id/pbHeaderProgress"
        android:indeterminateOnly="true"
        android:keepScreenOn="true"
        android:progressDrawable="@drawable/activityindicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ProgressBar>
</LinearLayout>

这是我尝试过的,但颜色没有改变。谁能告诉我我做错了什么?

我正在使用棒棒糖版本。

【问题讨论】:

    标签: android xml progress-bar android-linearlayout android-drawable


    【解决方案1】:

    如果您只想更改颜色,请在进度条中添加颜色过滤器:

    pbHeaderProgress.getIndeterminateDrawable().setColorFilter(Color.RED, Mode.MULTIPLY);
    

    Mode 参数,指的是 PorterDuff.Mode 值 - 可用 here

    【讨论】:

    • 这使您可以灵活地从colors.xml中进行选择,也可以从progressBar.getProgressDrawable().setColorFilter(ContextCompat.getColor(getActivity(),R.color.product_status_color), PorterDuff.Mode.MULTIPLY)
    • 我想使用 Color.WHITE,但这条线不起作用。我不得不将 porter duff 模式更改为 SRC_ATOP
    • 这个方法改变应用中所有进度条的颜色
    • 对于该模式,如果 .MULTIPLY 不起作用,请尝试 Mode.SRC_IN
    • 是否可以更改 xml 中的颜色?
    【解决方案2】:

    我刚刚找到了一个方法。我什至不需要单独的 xml 文件来更改颜色,因为进度条的类型是“indeterminate:true”

    我使用以下方法来更改进度条的颜色:

    pbHeaderProgress.getIndeterminateDrawable().setColorFilter(Color.parseColor("#C0D000"), android.graphics.PorterDuff.Mode.SRC_ATOP);
    

    您可以从这里获得各种十六进制颜色代码:http://www.nthelp.com/colorcodes.htm 要么 http://www.color-hex.com/

    【讨论】:

      猜你喜欢
      • 2017-07-07
      • 1970-01-01
      • 2011-01-02
      • 2019-12-15
      • 1970-01-01
      • 1970-01-01
      • 2016-11-02
      • 1970-01-01
      相关资源
      最近更新 更多