【问题标题】:How to set color to secondary drawable in progress bar如何将颜色设置为进度条中的辅助可绘制对象
【发布时间】:2012-08-28 00:31:17
【问题描述】:

我有这个代码

 pb = (ProgressBar) findViewById(R.id.progressBar1);
    final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
    ShapeDrawable pgDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));
    String MyColor = "#00FF00";
    pgDrawable.getPaint().setColor(Color.parseColor(MyColor));
    ClipDrawable progress = new ClipDrawable(pgDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
    pb.setProgressDrawable(progress);
    pb.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal));

此代码中的问题是,对于进度可绘制对象和辅助进度可绘制对象,我具有相同的颜色。

如何设置次要进度颜色?

【问题讨论】:

    标签: android colors progress-bar drawable


    【解决方案1】:

    指定progressDrawable,如下例所示:

    这是一个布局:

    <ProgressBar
        android:id="@+id/gauge"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="4dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/section"
        android:progress="50" 
        android:progressDrawable="@drawable/progressbar"/>
    

    这转到drawable/progressbar.xml,在这里您可以为两个进度条指定背景和颜色。

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item android:id="@android:id/background">
        <shape>
            <gradient
                    android:startColor="@color/basecolor"
                    android:endColor="@color/basecolor"
                    android:angle="270"
            />
        </shape>
    </item>
    
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <gradient
                        android:startColor="#808080"
                        android:endColor="#808080"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <gradient
                    android:startColor="#ffcc33"
                    android:endColor="#ffcc33"
                    android:angle="270" />
            </shape>
        </clip>
    </item>
    

    【讨论】:

    • 你知道@color中的状态选择器是否被考虑?
    • @njzk2 用于绘制背景,实际进度在其上绘制。
    • 一张便条。 xml 中这些项目的顺序很重要。您必须将secondaryProgress 放在进度之上,否则它不会正确分层。
    【解决方案2】:

    我也遇到了同样的问题。 我在上面的答案中添加了一些额外的点。 搜索栏或进度条分三层绘制。第一层是背景层,在次要进度层之上,在主要进度之上。

    您可以像这样将自定义颜色设置为进度条

    <item android:id="@android:id/background"
        android:drawable="@drawable/progress_bar_nor">       
    </item>    
    <item android:id="@android:id/secondaryProgress"
        android:drawable="@drawable/progress_bar_nor">       
    </item>
    <item
        android:id="@android:id/progress"
        android:drawable="@drawable/progress_bar_sel"/>
    

    或者像 Ridcully 在上面的回答中所说的那样

    恰好有些android os版本不使用二次进度的背景色。我已经检查了 android 4.1.2 三星 8 英寸平板电脑。这是行不通的。在这种情况下,使用背景通过设置背景、次要进度颜色来为次要进度设置自定义颜色

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-21
      • 2018-05-27
      相关资源
      最近更新 更多