我在我的Android应用程序中使用水平进度条,我想改变它的进度颜色(默认为黄色)。我如何使用代码(而不是XML)做到这一点?
当前回答
更改水平进度条颜色(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)
}
}
}
}
它最终正常地着色pre-lollipop progressBars
其他回答
更改进度条的前景和背景颜色的最简单的方法是
<ProgressBar
style="@android:style/Widget.ProgressBar.Horizontal"
android:id="@+id/pb_main"
android:layout_width="match_parent"
android:layout_height="8dp"
android:progress="30"
android:progressTint="#82e9de"
android:progressBackgroundTint="#82e9de"
/>
只需添加
android:progressTint="#82e9de" //for foreground colour
android:progressBackgroundTint="#82e9de" //for background colour
对于我的不确定的进度条(转轮),我只是设置了一个颜色过滤器的可绘制。工作很好,只是一行。
将颜色设置为红色的示例:
ProgressBar spinner = new android.widget.ProgressBar(
context,
null,
android.R.attr.progressBarStyle);
spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);
android:progressTint="#ffffff"
我知道我迟到了,但如果你只想改变进度条的颜色,这是最简单的解决方案,在你的主题中,只需添加,
@color - your_color
colorSecondary将改变你整个进程的颜色。
这是一个老问题,但这里没有提到使用主题。如果你的默认主题是使用AppCompat,你的ProgressBar的颜色将是你已经定义的colorAccent。
改变colorAccent也会改变你的ProgressBar的颜色,但这些变化也反映在多个地方。所以,如果你想为一个特定的ProgressBar使用不同的颜色,你可以通过应用theme到那个ProgressBar来实现:
扩展默认主题并覆盖colorAccent <样式名= " AppTheme。WhiteAccent”> <item name="colorAccent">@color/white</item> <!——你想要什么颜色都行——> > < /风格 在ProgressBar中添加android:theme属性: android:主题= " @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" />
你只是在为你特定的ProgressBar改变一个颜色重音。
注意:使用样式将不起作用。你只需要使用android:theme。 你可以在这里找到更多关于theme的用法:https://plus.google.com/u/0/+AndroidDevelopers/posts/JXHKyhsWHAH
推荐文章
- 如何解决“丢失的PendingIntent可变性标志”在android api 30+ lint警告?
- 我怎么能修复“意外元素<查询>发现<manifest>”错误?
- Android房间-自动生成新插入行的id
- Android Studio在哪里保存ProGuard映射文件?
- 我如何从资产文件夹解析一个本地JSON文件到一个ListView?
- Android 4.4 (KitKat)上的Android Gallery为Intent返回不同的URI。ACTION_GET_CONTENT
- 移除动作栏下面的阴影
- 如何在线性布局的一侧绘制边界?
- 找到可移动SD卡的位置
- 用于自定义视图的attrs.xml中的同名属性
- IntelliJ IDEA with Junit 4.7”!!JUnit 3.8或更高版本:
- 如何确定Gradle的版本?
- 在android studio中不能小写按钮文本
- 安装在控制台显示错误:安装失败,冲突的提供程序
- 如何在Android中制作倒计时计时器?