198

Horizo​​ntal を設定しProgressBarました。

進行状況の色を黄色に変更したいと思います。

<ProgressBar 
    android:id="@+id/progressbar" 
    android:layout_width="80dip" 
    android:layout_height="20dip"  
    android:focusable="false" 
    style="?android:attr/progressBarStyleHorizontal" />

問題は、進行状況の色がデバイスによって異なることです。だから、進行状況の色を修正したい。

4

18 に答える 18

380

私は自分のアプリの 1 つからこれをコピーしたので、いくつかの余分な属性がある可能性がありますが、アイデアが得られるはずです。これは、進行状況バーがあるレイアウトからのものです。

<ProgressBar
    android:id="@+id/ProgressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:indeterminate="false"
    android:maxHeight="10dip"
    android:minHeight="10dip"
    android:progress="50"
    android:progressDrawable="@drawable/greenprogress" />

次に、次のような新しいドローアブルを作成します (この場合はgreenprogress.xml)。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <gradient
                android:angle="270"
                android:centerColor="#ff5a5d5a"
                android:centerY="0.75"
                android:endColor="#ff747674"
                android:startColor="#ff9d9e9d" />
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                    android:angle="270"
                    android:centerColor="#80ffb600"
                    android:centerY="0.75"
                    android:endColor="#a0ffcb00"
                    android:startColor="#80ffd300" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                    android:angle="270"
                    android:endColor="#008000"
                    android:startColor="#33FF33" />
            </shape>
        </clip>
    </item>

</layer-list>

必要に応じて色を変更できます。これにより、緑色の進行状況バーが表示されます。

于 2011-04-21T15:03:13.957 に答える
106

より簡単な解決策:

progess_drawable_blue

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
    <shape>
        <solid
                android:color="@color/disabled" />
    </shape>
</item>

<item
    android:id="@android:id/progress">
    <clip>
        <shape>
            <solid
                android:color="@color/blue" />
        </shape>
    </clip>
</item>

</layer-list>
于 2013-01-31T18:43:35.980 に答える
40

プログレス バーの色だけを変更したい場合は、Activity の onCreate() メソッドでカラー フィルターを使用するだけです。

ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressbar);
int color = 0xFF00FF00;
progressbar.getIndeterminateDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);
progressbar.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_IN);

ここからのアイデア。

これは、ロリポップ以前のバージョンでのみ行う必要があります。Lollipop では、colorAccent スタイル属性を使用できます。

于 2015-03-22T20:09:56.520 に答える
39

API レベル 21 以上の場合は、単に使用します

android:progressBackgroundTint="@color/yourBackgroundColor"
android:progressTint="@color/yourColor"
于 2016-12-03T07:42:00.663 に答える
7

この問題を解決するには、次の 3 つの方法があります。

  1. プログレスバーの色を宣言的に調整する方法
  2. プログレスバーの色をプログラムで調整する方法ですが、コンパイル時の前に宣言されたさまざまな事前定義された色から色を選択します
  3. プログレスバーの色をプログラムで調整する方法、およびプログラムで色を作成する方法。

特に、amfcosta の回答へのコメントに見られるように、#2 と #3 の周りに多くの混乱があります。その答えは、プログレスバーを原色以外に設定したいときはいつでも予測できない色の結果をもたらします。これは、背景色を変更するだけであり、実際のプログレスバーの「クリップ」領域は不透明度が低下した黄色のオーバーレイのままです。たとえば、その方法を使用して背景を濃い紫に設定すると、プログレス バーの「クリップ」カラーが、アルファを減らして濃い紫と黄色が混ざり合ったクレイジーなピンクがかった色になります。

とにかく、#1 は Ryan によって完全に答えられ、Štarke は #3 のほとんどに答えますが、#2 と #3 の完全な解決策を探している人のために:


プログレスバーの色をプログラムで調整するが、XML で宣言された所定の色から色を選択する方法

res/drawable/my_progressbar.xml :

このファイルを作成しますが、my_progress と my_secondsProgress の色を変更します。

(注: これは、デフォルトの Android SDK ProgressBar を定義する実際の XML ファイルのコピーにすぎませんが、ID と色を変更しました。オリジナルは、progress_horizo​​ntal という名前です)

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/my_progress_background">
    <shape>
        <corners android:radius="5dip" />
        <gradient
            android:startColor="#ff9d9e9d"
            android:centerColor="#ff5a5d5a"
            android:centerY="0.75"
            android:endColor="#ff747674"
            android:angle="270"
            />
    </shape>
</item>

<item android:id="@+id/my_secondaryProgress">
    <clip>
        <shape>
            <corners android:radius="5dip" />
            <gradient
                android:startColor="#80ff171d"
                android:centerColor="#80ff1315"
                android:centerY="0.75"
                android:endColor="#a0ff0208"
                android:angle="270"
                />
        </shape>
    </clip>
</item>

<item android:id="@+id/my_progress">
    <clip>
        <shape>
            <corners android:radius="5dip" />
            <gradient
                android:startColor="#7d2afdff"
                android:centerColor="#ff2afdff"
                android:centerY="0.75"
                android:endColor="#ff22b9ba"
                android:angle="270"
                />
        </shape>
    </clip>
</item>

あなたのJavaで

final Drawable drawable;
int sdk = android.os.Build.VERSION.SDK_INT;
    if(sdk < 16) {
        drawable =  ctx.getResources().getDrawable(R.drawable.my_progressbar);
    } else {
        drawable = ContextCompat.getDrawable(ctx, R.drawable.my_progressbar);
    }
progressBar.setProgressDrawable(drawable)

プログレスバーの色をプログラムで調整する方法、およびプログラムで色を作成する方法

編集:これを適切に解決する時間を見つけました。私の以前の答えは、これを少しあいまいにしました。

ProgressBar は、LayerDrawable 内の 3 つの Drawable として構成されます。

  1. レイヤー1は背景です
  2. レイヤー 2 は二次進行色です
  3. レイヤー 3 はメインの進行状況バーの色です

以下の例では、メインのプログレス バーの色をシアンに変更します。

//Create new ClipDrawable to replace the old one
float pxCornerRadius = viewUtils.convertDpToPixel(5);
final float[] roundedCorners = new float[] { pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius, pxCornerRadius };
ShapeDrawable shpDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null, null));
shpDrawable.getPaint().setColor(Color.CYAN);
final ClipDrawable newProgressClip = new ClipDrawable(shpDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);

//Replace the existing ClipDrawable with this new one
final LayerDrawable layers = (LayerDrawable) progressBar.getProgressDrawable();
layers.setDrawableByLayerId(R.id.my_progress, newProgressClip);

その例では、単色に設定されています。置き換えることでグラデーションカラーに設定できます

shpDrawable.getPaint().setColor(Color.CYAN);

Shader shader = new LinearGradient(0, 0, 0, progressBar.getHeight(), Color.WHITE, Color.BLACK, Shader.TileMode.CLAMP);
shpDrawable.getPaint().setShader(shader);

乾杯。

-ランス

于 2015-10-19T20:01:29.423 に答える
7

Material Components LibraryLinearProgressIndicatorでは、app:indicatorColor属性を使用して色を変更できます。

  <com.google.android.material.progressindicator.LinearProgressIndicator
      app:indicatorColor="@color/..."
      app:trackThickness="..."
      app:trackColor="@color/..."
      />

ここに画像の説明を入力

: 少なくともバージョンが必要1.3.0-alpha04です。

を使用すると、ProgressBar次を使用して色をオーバーライドできます。

<ProgressBar
    android:theme="@style/CustomProgressBarTheme"
    ..>

と:

<style name="CustomProgressBarTheme">
    <item name="colorAccent">@color/primaryDarkColor</item>
</style>

ここに画像の説明を入力

于 2020-06-06T19:45:31.867 に答える
4
final LayerDrawable layers = (LayerDrawable) progressBar.getProgressDrawable();
layers.getDrawable(2).setColorFilter(color,PorterDuff.Mode.SRC_IN);
于 2016-06-09T06:47:52.830 に答える
3

android:indeterminateTint="@color/colorPrimary"プログレスバーに追加するだけです

例:

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminateTint="@color/colorPrimary"/>

progressBarStyleHorizo​​ntal 変更の場合android:progressTint="@color/colorPrimary"

例:

 <ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:progress="50"
    android:progressTint="@color/colorPrimary" />
于 2020-09-27T11:02:02.213 に答える
1

ProgressBar の色は次のように変更できます。

/res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorAccent">#FF4081</color>
</resources>

/res/values/styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorAccent">@color/colorAccent</item>
</style> 

作成時:

progressBar = (ProgressBar) findViewById(R.id.progressBar);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    Drawable drawable = progressBar.getIndeterminateDrawable().mutate();
    drawable.setColorFilter(ContextCompat.getColor(this, R.color.colorAccent), PorterDuff.Mode.SRC_IN);
    progressBar.setIndeterminateDrawable(drawable);
}
于 2016-09-25T14:22:25.550 に答える