Nis*_*hah 165 android android-progressbar
我已经设置了水平进度条.
我想改变黄色的进度颜色.
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="80dip"
android:layout_height="20dip"
android:focusable="false"
style="?android:attr/progressBarStyleHorizontal" />
Run Code Online (Sandbox Code Playgroud)
问题是,不同设备的进度颜色不同.所以,我想要它来修复进度颜色.
小智 352
我从我的一个应用程序中复制了这个,所以有一些额外的属性,但应该给你一个想法.这是来自具有进度条的布局:
<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" />
Run Code Online (Sandbox Code Playgroud)
然后使用类似于以下内容的东西创建一个新的drawable(在本例中为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: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="#33FF33"
android:endColor="#008000"
android:angle="270" />
</shape>
</clip>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
您可以根据需要更改颜色,这将为您提供绿色进度条.
小智 103
更简单的解决方案:
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>
Run Code Online (Sandbox Code Playgroud)
amf*_*sta 37
如果您只想更改进度条颜色,可以在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);
Run Code Online (Sandbox Code Playgroud)
来自这里的想法.
您只需要为棒棒糖前版本执行此操作.在Lollipop上,您可以使用colorAccent样式属性.
Ali*_*ali 37
只需创建一个样式values/styles.xml
.
<style name="ProgressBarStyle">
<item name="colorAccent">@color/greenLight</item>
</style>
Run Code Online (Sandbox Code Playgroud)
然后将此样式设置为ProgressBar
主题.
<ProgressBar
android:theme="@style/ProgressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)
并不重要你的进度条是水平或圆形.就这样.
小智 21
对于API级别21或更高版本,请使用
android:progressBackgroundTint="@color/yourBackgroundColor"
android:progressTint="@color/yourColor"
Run Code Online (Sandbox Code Playgroud)
有3种方法可以解决这个问题:
特别是围绕#2和#3存在很多混淆,正如对amfcosta答案的评论所示.当你想要将进度条设置为除原色之外的任何东西时,该答案将产生不可预测的颜色结果,因为它仅修改背景颜色,并且实际进度条"剪辑"区域仍将是具有降低的不透明度的黄色叠加.例如,使用该方法将背景设置为深紫色将导致进度条"剪辑"颜色的一些疯狂的粉红色,由暗紫色和黄色混合通过减少的α混合.
无论如何,#1被Ryan完美地回答,而Štarke回答了#3的大部分内容,但对于那些寻找#2和#3的完整解决方案的人来说:
res/drawable/my_progressbar.xml:
创建此文件但更改my_progress和my_secondsProgress中的颜色:
(注意:这只是定义默认android SDK ProgressBar的实际XML文件的副本,但我更改了ID和颜色.原始名称为progress_horizontal)
<?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>
Run Code Online (Sandbox Code Playgroud)
在你的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)
Run Code Online (Sandbox Code Playgroud)
编辑:我找到了解决这个问题的时间.我以前的回答让这有点模棱两可.
ProgressBar由LayerDrawable中的3个Drawable组成.
在下面的示例中,我将主进度条的颜色更改为青色.
//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);
Run Code Online (Sandbox Code Playgroud)
该示例将其设置为纯色.您可以通过替换将其设置为渐变颜色
shpDrawable.getPaint().setColor(Color.CYAN);
Run Code Online (Sandbox Code Playgroud)
同
Shader shader = new LinearGradient(0, 0, 0, progressBar.getHeight(), Color.WHITE, Color.BLACK, Shader.TileMode.CLAMP);
shpDrawable.getPaint().setShader(shader);
Run Code Online (Sandbox Code Playgroud)
干杯.
-长矛
如果您处于 API 级别 21 或更高级别,则可以使用这些 XML 属性:
<!-- For indeterminate progress bar -->
android:indeterminateTint="@color/white"
<!-- For normal progress bar -->
android:progressTint="@color/white"
Run Code Online (Sandbox Code Playgroud)
使用 Material Components Library,您可以使用LinearProgressIndicator
withapp:indicatorColor
属性来更改颜色:
<com.google.android.material.progressindicator.LinearProgressIndicator
app:indicatorColor="@color/..."
app:trackThickness="..."
app:trackColor="@color/..."
/>
Run Code Online (Sandbox Code Playgroud)
注意:它至少需要版本1.3.0-alpha04
。
使用 ,ProgressBar
您可以使用以下方法覆盖颜色:
<ProgressBar
android:theme="@style/CustomProgressBarTheme"
..>
Run Code Online (Sandbox Code Playgroud)
和:
<style name="CustomProgressBarTheme">
<item name="colorAccent">@color/primaryDarkColor</item>
</style>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
177686 次 |
最近记录: |