我在我的Android应用程序中使用水平进度条,我想改变它的进度颜色(默认为黄色)。我如何使用代码(而不是XML)做到这一点?
当前回答
使用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"/>
其他回答
适用于SDK 21及以上版本
android:indeterminateTint="@color/orange"
对我来说是很简单的。
很抱歉,这不是答案,但是是什么驱使需求从代码中设置它呢? 如果定义正确。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>
使用android.support.v4.graphics.drawable.DrawableCompat:
Drawable progressDrawable = progressBar.getIndeterminateDrawable();
if (progressDrawable != null) {
Drawable mutateDrawable = progressDrawable.mutate();
DrawableCompat.setTint(mutateDrawable, primaryColor);
progressBar.setProgressDrawable(mutateDrawable);
}
使用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"/>
这是一个老问题,但这里没有提到使用主题。如果你的默认主题是使用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中制作倒计时计时器?