没有剪辑的android ProgressBar?

标签 android progress-bar

任何默认的 ProgressBar 都有一个 ClipDrawable 级别,例如,一个绿色的 Drawable 在我的设备上填充灰色背景,在其他设备上填充黄色。

enter image description here

我的问题:当进度小于最大值时,是否可以创建一个没有这个(部分)空背景的 ProgressBar,或者我应该改用 ImageView 吗?我只需要 ClipDrawable 来改变它的宽度,这样它看起来就像一个图表,而灰色背景永远不会出现。

最佳答案

像这样创建 layer-list 可绘制对象。

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

<item android:id="@android:id/background">
    <shape android:shape="rectangle" >
        <solid android:color="@android:color/transparent" />
    </shape>
</item>

<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <gradient
                    android:startColor="#234"
                    android:centerColor="#234"
                    android:centerY="0.75"
                    android:endColor="#a24"
                    android:angle="270"
            />
        </shape>
    </clip>
</item>

<item android:id="@android:id/progress">
    <clip>
        <shape>
            <gradient
                android:startColor="#144281"
                android:centerColor="#0b1f3c"
                android:centerY="0.75"
                android:endColor="#06101d"
                android:angle="270"
            />
        </shape>
    </clip>
</item>

重点是让 android:id="@android:id/background 的项目使用 @android:color/transparent

然后将其设置为 ProgressBarandroid:progressDrawable 属性

<ProgressBar
    android:id="@+id/progressBar"
    android:progressDrawable="@drawable/your_layer_list_created_on_previous_step"
    android:layout_width="fill_parent"
    android:layout_height="8dp" 
    style="?android:attr/progressBarStyleHorizontal"
    android:indeterminateOnly="false" 
    android:max="100" />

关于没有剪辑的android ProgressBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14124171/

相关文章:

node.js - 如何在 node.js CLI 应用程序中使用日志记录和进度条?

android - 升级到 2.3,现在 ant 构建不包括我的 libs 目录中的 jars

android - MathView 中的点击监听器

css - 具有 2 种不同颜色的 JavaFX slider ,例如选定区域为绿色,未选定区域为红色

java - 小程序加载时的进度条

android - 将刷新的 ImageView 变成 ProgressBar

java - 如何使用 Eclipse 创建非 fragment Android 应用程序?

安卓退出应用

android - Flutter:如何构建改变移动设备方向时移动 block 的逻辑?

java - 如何显示下载附件的进度条