Android-在预棒棒糖设备上为ProgressBar着色 [英] Android - Tinting ProgressBar on pre-Lollipop devices

查看:104
本文介绍了Android-在预棒棒糖设备上为ProgressBar着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的最低API级别为19(KitKat),并且包含一个带有水平 ProgressBar 的布局。我正在使用 android:progressTint 属性将栏着色为自定义颜色。它在Lollipop(API 21)和更高版本上工作得很好,但是在下面(例如,在API 19上)则不能运行;它以不同的颜色显示。原因是该属性

My app's minimum API-level is 19 (KitKat), and contains a layout with a horizontal ProgressBar. I'm using android:progressTint attribute to tint the bar to a custom color. It works pretty well on Lollipop (API 21) and above, but below (for example on API 19) it doesn't; it shows up in a different color. The reason is that this attribute


仅在API级别21和更高级别中使用

is only used in API level 21 and higher

如Android Studio所述。

as Android Studio states.

所以我想知道有什么办法可以很好地替代棒棒糖之前的设备上的ProgressBar 也是如此。可以使用XML在布局文件中制作吗?还是应该以其他方式完成此操作?

So I'm wondering what can be a good alternative for tinting the ProgressBar on pre-Lollipop devices, too. Can it be made inside the layout file with XML? Or should this be done on a different way?

编辑#1:我的ProgressBar是用于显示百分比的具体值,而不是用于指示我的布局正在加载。我想说清楚一点,因为尝试了下面的Kuldeep Kulkarni的写法之后,我的条看起来像是一个物料装载指示器(当然是在Lollipop设备上,在KitKat设备上没有任何可见结果)。

EDIT #1: My ProgressBar is used to show concrete values in percents, not for indicating that my layout is loading. I want to make this clear because after trying what Kuldeep Kulkarni wrote below, my bar looked like a material loading indicator (of course on the Lollipop device, not any visible result on KitKat device).

推荐答案

感谢大家的建议! :)

Thanks for the suggestions from everyone! :)

最适合我的解决方案是使用< layer-list> 根元素。在那里,我用本地Android ID @android:id / background @android:id / progress 。之后,我可以定义要使用的形状和颜色资源。此解决方案类似于 SO问题上的更流行的答案。

The solution which works for me is to create a custom drawable XML file with the <layer-list> root element. There, I'm defining two layer items with the native Android IDs @android:id/background and @android:id/progress. After this, I can define the shapes and color resources I would like to use. This solution is similar to a more popular answer on this SO question.

res / drawable / progressbar.xml 文件的内容:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/background">

        <shape>

            <corners android:radius="2dip" /> <!--optional-->

            <gradient
                android:angle="270"
                android:endColor="@color/gray"
                android:startColor="@color/gray" />

        </shape>

    </item>

    <item android:id="@android:id/progress">

        <clip>

            <shape>

                <corners android:radius="2dip" /> <!--optional-->

                <gradient
                    android:angle="270"
                    android:endColor="@color/blue"
                    android:startColor="@color/blue" />

            </shape>

        </clip>

    </item>

</layer-list>

为我的<$ c定义为 progressDrawable 布局XML文件中的$ c> ProgressBar

Defined as progressDrawable for my ProgressBar in the layout XML file:

<ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="0dp"
        android:layout_height="@dimen/progress_bar_height"
        android:progressDrawable="@drawable/progressbar" />

我没有机会在API 19以下对其进行测试,但在该级别及其之上效果很好。

I didn't have a chance to test it below API 19, but on this level and above it works perfectly.

这篇关于Android-在预棒棒糖设备上为ProgressBar着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆