- android - android-progressbar |
- |
溶液
私はそれが答えではないことを残念に思いますが、コードからそれを設定する必要性を駆動何?
そして.setProgressDrawable
が動作するはずが正しく定義されていた場合、
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/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="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners
android:radius="5dip" />
<gradient
android:startColor="@color/progress_start"
android:endColor="@color/progress_end"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>
その他のヒント
水平プログレスバーのために、あなたはこのように、あまりにも、ColorFilter
を使用することができます:
progressBar.getProgressDrawable().setColorFilter(
Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);
注:このアプリ内のすべてのプログレスバーの外観を変更します。 1つの特定のプログレスバーを変更するには、次の操作を行います:
Drawable progressDrawable = progressBar.getProgressDrawable().mutate();
progressDrawable.setColorFilter(Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);
progressBar.setProgressDrawable(progressDrawable);
プログレスバーが不確定である場合は、、代わりにgetIndeterminateDrawable()
のgetProgressDrawable()
を使用します。
ロリポップ(API 21)以来、あなたは進捗色合いを設定することができます
progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));
私の不確定プログレスバー(スピナー)のために私はちょうど描画可能な上にカラーフィルタを設定します。素晴らしい作品とちょうど1行。
赤色に例設定色:
ProgressBar spinner = new android.widget.ProgressBar(
context,
null,
android.R.attr.progressBarStyle);
spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);
この古い質問ですが、テーマを使用すると、ここで言及されていません。デフォルトのテーマはAppCompat
を使用している場合は、あなたのProgressBar
の色は、あなたが定義したcolorAccent
されます。
変更colorAccent
はまたあなたのProgressBar
の色を変更しますが、そこにも変化の複数の場所で反映されます。あなただけの特定のPregressBar
に異なる色をしたいのであれば、あなたはそのProgressBar
にテーマを適用することによって、それを行うことができます:
-
あなたのデフォルトのテーマを拡張し、
colorAccent
を上書き
ます。<style name="AppTheme.WhiteAccent"> <item name="colorAccent">@color/white</item> <!-- Whatever color you want--> </style>
-
そして
ProgressBar
にandroid:theme
属性を追加します:
ます。android:theme="@style/AppTheme.WhiteAccent"
だから、このようになります。
<ProgressBar
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:theme="@style/AppTheme.WhiteAccent" />
だから、あなたは自分の特定のcolorAccent
ためProgressBar
を変更されます。
注:動作しませんstyle
を使用します。あなただけandroid:theme
を使用する必要があります。
あなたがここでのテーマの多くの使用を見つけることができます: https://plus.google.com/u/0/ + AndroidDevelopers /ポスト/ JXHKyhsWHAHする
のすべてのAPI の
使用している場合は、すべてのAPIは、スタイルだけでテーマを作成
style.xml
<resources>
//...
<style name="progressBarBlue" parent="@style/Theme.AppCompat">
<item name="colorAccent">@color/blue</item>
</style>
</resources>
と
進行中で使用<ProgressBar
...
android:theme="@style/progressBarBlue" />
<時間>
の APIレベル21より高いの
APIレベル21で使用され、より高いだけで、このコードを使用する場合:
<ProgressBar
//...
android:indeterminate="true"
android:indeterminateTintMode="src_atop"
android:indeterminateTint="@color/secondary"/>
の提案の一部につきとして、あなたはそれを設定し、色と形とclipdrawableを指定することができます。私はこのプログラムで働いてきました。これは私がそれを行う方法です..
まず、あなたが描画可能なライブラリをインポートしてください..
import android.graphics.drawable.*;
次に、以下のようなコードを使用し、
ProgressBar pg = (ProgressBar)row.findViewById(R.id.progress);
final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
pgDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null,null));
String MyColor = "#FF00FF";
pgDrawable.getPaint().setColor(Color.parseColor(MyColor));
ClipDrawable progress = new ClipDrawable(pgDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
pg.setProgressDrawable(progress);
pg.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal));
pg.setProgress(45);
これは私のために働いてます:
<ProgressBar
android:indeterminateTint="#d60909"
... />
不定の場合:
((ProgressBar)findViewById(R.id.progressBar))
.getIndeterminateDrawable()
.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
今日では2016年に私はすべてのAPIのための私の最終的な解決策は、現在、以下のある、いくつかの事前ロリポップデバイスはcolorAccent
設定を尊重していない見つけます:
// fixes pre-Lollipop progressBar indeterminateDrawable tinting
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Drawable wrapDrawable = DrawableCompat.wrap(mProgressBar.getIndeterminateDrawable());
DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(getContext(), android.R.color.holo_green_light));
mProgressBar.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
} else {
mProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), android.R.color.holo_green_light), PorterDuff.Mode.SRC_IN);
}
は、ボーナスポイントの場合は、任意の非推奨のコードを使用していません。それを試してみてください!
これは私がやったことです。働いていた。
<強い>プログレスバー:の
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="4"
android:indeterminateDrawable="@drawable/progressdrawable"
/>
<時間>
<強い> progressdrawable.xml:の
はここで好きなように色を変更するためにグラデーションを使用しています。そして、Android:=「X」toDegreesはXとプログレスバーの値をincreseは、高速回転させます。減少し、それはあなたのニーズに応じてslow.Customizeを回転させます。の
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="4000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" >
<shape
android:innerRadius="20dp"
android:shape="ring"
android:thickness="4dp"
android:useLevel="false" >
<size
android:height="48dp"
android:width="48dp" />
<gradient
android:centerColor="#80ec7e2a"
android:centerY="0.5"
android:endColor="#ffec7e2a"
android:startColor="#00ec7e2a"
android:type="sweep"
android:useLevel="false" />
</shape>
</rotate>
<時間>
"ここに画像の説明を入力します"
デフォルトのプログレスバーの外観/フィールを変更することに取り組んでいる間、
同じ問題をヒット。ここでうまくいけば、人々を助けるいくつかの詳細は、次のとおりです。)
- xmlファイルの名前には文字だけ含まれている必要があります:
a-z0-9_.
(すなわち無首都。!) - あなたのそれは "描画可能" である
R.drawable.filename
ます。 を参照するには
- デフォルトの外観を上書きするには、あなたはしかし、あなたがちょうど
myProgressBar.setProgressDrawable(...)
として、カスタムレイアウトを参照することができない必要がある、R.drawable.filename
を使用し、あなたはDrawable
としてそれを取得する必要があります。
ます。Resources res = getResources(); myProgressBar.setProgressDrawable(res.getDrawable(R.drawable.filename);
- あなたはスタイルを設定する必要が進捗状況を設定する前に/二次進行/最大の(私のために、後でそれを設定する「空の」プログレスバーが生じた)の
この回答で言及されていない一つのことは、おそらくあります
あなたのテーマはTheme.AppCompat
から継承している場合、ProgressBar
は、あなたのテーマに"colorAccent"
として定義された色を想定します。
だから、使用して..
<item name="colorAccent">@color/custom_color</item>
.. @color/custom_color
に自動的にプログレスバーの色の着色されます。
私は水平プログレスバーでそれをやった方法:
LayerDrawable layerDrawable = (LayerDrawable) progressBar.getProgressDrawable();
Drawable progressDrawable = layerDrawable.findDrawableByLayerId(android.R.id.progress);
progressDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
あなたは、あなたのスタイル、テーマ、またはアンドロイドを使用して変更しようとすることができますindeterminateTint =「@カラー/ yourColor」どこでもあなたがしたいが、ただ一つの方法がありますことをやってoを任意のアンドロイドSKDのバージョンで動作します。
あなたはバーが不確定でない進行場合は、ご使用くださいます:
progressBar.getProgressDrawable().setColorFilter(ContextCompat.getColor(context, R.color.yourColor), PorterDuff.Mode.SRC_IN );
あなたはバーが不確定で進行した場合、ご使用くださいます:
progressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), R.color.yourColor), PorterDuff.Mode.SRC_IN );
これは、Androidがそのような混乱であることを悲しいです!
android:progressTint="#ffffff"
最も簡単な解決策をあなたは、レイアウトxmlファイル内の色を変更するには、以下のコードを使用して使用する場合の indeterminateTint をご希望のカラーのプロパティます。
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:indeterminate="true"
android:indeterminateTintMode="src_atop"
android:indeterminateTint="#ddbd4e"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
このソリューションは、私のために働いてます:
<style name="Progressbar.White" parent="AppTheme">
<item name="colorControlActivated">@color/white</item>
</style>
<ProgressBar
android:layout_width="@dimen/d_40"
android:layout_height="@dimen/d_40"
android:indeterminate="true"
android:theme="@style/Progressbar.White"/>
あなたが基本テーマを継承している場合でなければなりませんあなたのテーマを圧縮アプリのためのように、もう一つの小さなことが、テーマのソリューションは、動作します:
<style name="AppTheme.Custom" parent="@style/Theme.AppCompat">
<item name="colorAccent">@color/custom</item>
</style>
そして
プログレスバーのテーマでこれを設定します<ProgressBar
android:id="@+id/progressCircle_progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:theme="@style/AppTheme.Custom"
android:indeterminate="true"/>
ateiobは何かを説明するために私に尋ねたので、
は、 PaulieGの答えについて情報を追加するために投稿されました...
<時間>私はそこにあると言うことができる(あるいは、少なくとも私はAndroidのソースコードの現在のバージョンを見て書いている時点で、あった)進展を設定しようとする試みを無視しProgressBar
コードのバグ/問題/最適化は、値には、すでにです。
- すなわち。進捗状況= 45、そしてあなたは、コード進行を再描画しません何もしない、とのします45に設定してみてくださいをします。 もし
ProgressBar.setProgressDrawable()
を呼び出した後、あなたの進行状況バーが空白になります。
これは、あなたが進捗状況を設定し、それを再描画する必要があるということです。あなただけの保存値に進捗状況を設定している場合しかし、それは何もしません。
あなたは再び「古い」の値に、最初の0に設定する必要があり、バーが再描画されます。
<時間>だから、要約する:
- "古い" の進行状況値を維持ます。
- (バーブランクを作る)、描画可能/色を更新
- (そうでなければ次の行は何もしない) 0に進捗をリセット
- 「古い」値(修正バー)に進捗状況をリセットします。
- 無効ます。
以下は、これを行い、私が持っている方法です。
protected void onResume()
{
super.onResume();
progBar = (ProgressBar) findViewById(R.id.progress_base);
int oldProgress = progBar.getProgress();
// define new drawable/colour
final float[] roundedCorners = new float[]
{ 5, 5, 5, 5, 5, 5, 5, 5 };
ShapeDrawable shape = new ShapeDrawable(new RoundRectShape(
roundedCorners, null, null));
String MyColor = "#FF00FF";
shape.getPaint().setColor(Color.parseColor(MyColor));
ClipDrawable clip = new ClipDrawable(shape, Gravity.LEFT,
ClipDrawable.HORIZONTAL);
progBar.setProgressDrawable(clip);
progBar.setBackgroundDrawable(getResources().getDrawable(
android.R.drawable.progress_horizontal));
// work around: setProgress() ignores a change to the same value
progBar.setProgress(0);
progBar.setProgress(oldProgress);
progBar.invalidate();
}
<時間>
限りHappyEngineerのソリューションとして、私はそれを手動でオフセット「進歩」を設定し、同様の回避策だったと思います。いずれの場合も、上記のコードは、あなたのために働く必要があります。
android.support.v4.graphicsを使用してください。 のdrawable.DrawableCompatます:
Drawable progressDrawable = progressBar.getIndeterminateDrawable();
if (progressDrawable != null) {
Drawable mutateDrawable = progressDrawable.mutate();
DrawableCompat.setTint(mutateDrawable, primaryColor);
progressBar.setProgressDrawable(mutateDrawable);
}
私が使用して水平スタイルのプログレスバーの場合:
import android.widget.ProgressBar;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ClipDrawable;
import android.view.Gravity;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
public void setColours(ProgressBar progressBar,
int bgCol1, int bgCol2,
int fg1Col1, int fg1Col2, int value1,
int fg2Col1, int fg2Col2, int value2)
{
//If solid colours are required for an element, then set
//that elements Col1 param s the same as its Col2 param
//(eg fg1Col1 == fg1Col2).
//fgGradDirection and/or bgGradDirection could be parameters
//if you require other gradient directions eg LEFT_RIGHT.
GradientDrawable.Orientation fgGradDirection
= GradientDrawable.Orientation.TOP_BOTTOM;
GradientDrawable.Orientation bgGradDirection
= GradientDrawable.Orientation.TOP_BOTTOM;
//Background
GradientDrawable bgGradDrawable = new GradientDrawable(
bgGradDirection, new int[]{bgCol1, bgCol2});
bgGradDrawable.setShape(GradientDrawable.RECTANGLE);
bgGradDrawable.setCornerRadius(5);
ClipDrawable bgclip = new ClipDrawable(
bgGradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
bgclip.setLevel(10000);
//SecondaryProgress
GradientDrawable fg2GradDrawable = new GradientDrawable(
fgGradDirection, new int[]{fg2Col1, fg2Col2});
fg2GradDrawable.setShape(GradientDrawable.RECTANGLE);
fg2GradDrawable.setCornerRadius(5);
ClipDrawable fg2clip = new ClipDrawable(
fg2GradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
//Progress
GradientDrawable fg1GradDrawable = new GradientDrawable(
fgGradDirection, new int[]{fg1Col1, fg1Col2});
fg1GradDrawable.setShape(GradientDrawable.RECTANGLE);
fg1GradDrawable.setCornerRadius(5);
ClipDrawable fg1clip = new ClipDrawable(
fg1GradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
//Setup LayerDrawable and assign to progressBar
Drawable[] progressDrawables = {bgclip, fg2clip, fg1clip};
LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);
progressLayerDrawable.setId(0, android.R.id.background);
progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
progressLayerDrawable.setId(2, android.R.id.progress);
//Copy the existing ProgressDrawable bounds to the new one.
Rect bounds = progressBar.getProgressDrawable().getBounds();
progressBar.setProgressDrawable(progressLayerDrawable);
progressBar.getProgressDrawable().setBounds(bounds);
// setProgress() ignores a change to the same value, so:
if (value1 == 0)
progressBar.setProgress(1);
else
progressBar.setProgress(0);
progressBar.setProgress(value1);
// setSecondaryProgress() ignores a change to the same value, so:
if (value2 == 0)
progressBar.setSecondaryProgress(1);
else
progressBar.setSecondaryProgress(0);
progressBar.setSecondaryProgress(value2);
//now force a redraw
progressBar.invalidate();
}
例の呼び出しは以下のようになります:
setColours(myProgressBar,
0xff303030, //bgCol1 grey
0xff909090, //bgCol2 lighter grey
0xff0000FF, //fg1Col1 blue
0xffFFFFFF, //fg1Col2 white
50, //value1
0xffFF0000, //fg2Col1 red
0xffFFFFFF, //fg2Col2 white
75); //value2
あなたはVALUE1に「二次進行」単に設定値2を必要としない場合ます。
プログレスバーに、このカスタムスタイルを適用します。
<style name="customProgress" parent="@android:style/Widget.ProgressBar.Small">
<item name="android:indeterminateDrawable">@drawable/progress</item>
<item name="android:duration">40</item>
<item name="android:animationCache">true</item>
<item name="android:drawingCacheQuality">low</item>
<item name="android:persistentDrawingCache">animation</item>
</style>
描画可能/ progress.xml @ -
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/spinner_white"
android:pivotX="50%"
android:pivotY="50%" />
プログレスバーのイメージのこのタイプを使用してください。
"ここに画像の説明を入力し、" の
より良い結果のためには、複数の進行状況の画像を使用することができます。 Androidプラットフォーム自体は、プログレスバーのイメージを使用しているためと、画像を使用して躊躇しないでください。 コードは、SDKから抽出される。)
ここではプログラム的によりプログレスバーの色を変更するためのコードである。
ProgressBar progressBar = (ProgressBar) findViewById(R.id.pb_listProgressBar);
int colorCodeDark = Color.parseColor("#F44336");
progressBar.setIndeterminateTintList(ColorStateList.valueOf(colorCodeDark));
ProgressBar freeRamPb = findViewById(R.id.free_ram_progress_bar);
freeRamPb.getProgressDrawable().setColorFilter(
Color.BLUE, android.graphics.PorterDuff.Mode.SRC_IN);
ProgressBar bar;
private Handler progressBarHandler = new Handler();
GradientDrawable progressGradientDrawable = new GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT, new int[]{
0xff1e90ff,0xff006ab6,0xff367ba8});
ClipDrawable progressClipDrawable = new ClipDrawable(
progressGradientDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
Drawable[] progressDrawables = {
new ColorDrawable(0xffffffff),
progressClipDrawable, progressClipDrawable};
LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);
int status = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
setContentView(R.layout.startup);
bar = (ProgressBar) findViewById(R.id.start_page_progressBar);
bar.setProgress(0);
bar.setMax(100);
progressLayerDrawable.setId(0, android.R.id.background);
progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
progressLayerDrawable.setId(2, android.R.id.progress);
bar.setProgressDrawable(progressLayerDrawable);
}
これはコードをプログレスバーするためのカスタム色を設定するために私を助けました。ホープは役立ちます。
ムハンマド・アディルが示唆あたりとして 21版SDKおよび
上記android:indeterminateTint="@color/orange"
XML作品で私のために、十分に簡単です。
あなたはmultistyleアプリで扱っている場合は、
これは、ATTRを使用してとても簡単です。
この方法を試してみます:
属性attrs.xmlの下に宣言します。
<attr name="circularProgressTheme" format="reference"></attr>
のstyles.xmlにコードペースト
<style name="ProgressThemeWhite" parent="ThemeOverlay.AppCompat.Light">
<item name="colorAccent">#FF0000</item>
</style>
<style name="circularProgressThemeWhite">
<item name="android:theme">@style/ProgressThemeWhite</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="circularProgressTheme">@style/circularProgressThemeWhite</item>
</style>
以下のようなプログレスバーを使用します
<ProgressBar
style="?attr/circularProgressTheme"
android:id="@+id/commonProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="visible"/>
<強い>水平プログレスバーのカスタム材料スタイル:の
背景と水平プログレスバーの進捗の色を変更します。
<style name="MyProgressBar" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
<item name="android:progressBackgroundTint">#69f0ae</item>
<item name="android:progressTint">#b71c1c</item>
<item name="android:minWidth">200dp</item>
</style>
カスタム材料スタイルとカスタムプログレスバーチェック<のhref =「http://www.zoftino.com/android-progressbar-and-custom-progressbar-examples」RELのために、スタイル属性を設定することで、バーを進めるために、それを適用します= "nofollowをnoreferrer"> http://www.zoftino.com/android-progressbar-and-custom-progressbar-examples の
(kotlinに)水平プログレスバーの色を変更する
fun tintHorizontalProgress(progress: ProgressBar, @ColorInt color: Int = ContextCompat.getColor(progress.context, R.color.colorPrimary)){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
progress.progressTintList = ColorStateList.valueOf(color)
} else{
val layerDrawable = progress.progressDrawable as? LayerDrawable
val progressDrawable = layerDrawable?.findDrawableByLayerId(android.R.id.progress)
progressDrawable?.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
}
}
不定のProgressBarの色を変更するには:
fun tintIndeterminateProgress(progress: ProgressBar, @ColorInt color: Int = ContextCompat.getColor(progress.context, R.color.colorPrimary)){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
progress.indeterminateTintList = ColorStateList.valueOf(color)
} else {
(progress.indeterminateDrawable as? LayerDrawable)?.apply {
if (numberOfLayers >= 2) {
setId(0, android.R.id.progress)
setId(1, android.R.id.secondaryProgress)
val progressDrawable = findDrawableByLayerId(android.R.id.progress).mutate()
progressDrawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
}
}
}
}
そして最後に正常に色合い前ロリポップ進捗バー