android - 在android上设计不确定的水平进度条

标签 android android-styles android-progressbar

确定进度条的样式很容易,有很多教程可以实现。这是我正在使用的:

<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="8dp"
    android:background="@drawable/progress_background"
    android:progressDrawable="@drawable/progress"
    android:id="@+id/progressBar" />

可绘制progress_background.xml:

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="@color/colorAccent" />
                <corners android:radius="4dp" />
            </shape>
        </clip>
    </item>
</layer-list>

看起来像这样:

enter image description here

但是当还没有进展时,我想使用不确定的。所以我尝试了:

<ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="8dp"
        android:background="@drawable/progress_background"
        android:progressDrawable="@drawable/progress"
        android:indeterminateTint="@color/colorAccent"
        android:indeterminateTintMode="src_in"
        android:indeterminate="true"
        android:id="@+id/progressBar" />

但不确定的进度不会拉伸(stretch)到条形高度:

enter image description here

我也尝试使用可绘制文件来设计它的样式,但它看起来像是损坏的确定进度条(再次从 0 填充到 100)。

所需的不确定进度条应该看起来像常规进度条,但具有 8dp 高度和圆角。

最佳答案

默认的不确定进度条动画使用非 9 补丁 png。所以它不能超过你的 8dp 高度进度条。您应该添加带有自定义动画的 android:indeterminateDrawable:

<animation-list
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:oneshot="false">
    <item android:drawable="@drawable/progressbar_indeterminate1" android:duration="50" />
    <item android:drawable="@drawable/progressbar_indeterminate2" android:duration="50" />
    <item android:drawable="@drawable/progressbar_indeterminate3" android:duration="50" />
    ...
    <item android:drawable="@drawable/progressbar_indeterminateX" android:duration="50" />
</animation-list>

然后像这样制作可绘制的动画(它可以是 xml 或图像或 9-patch):

Animation frame 1

Animation frame 2

android (api21+) 的任何版本都不会为不确定的 ProgressBar 使用 AnimatedVectorDrawable。

关于android - 在android上设计不确定的水平进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35514741/

相关文章:

Android windowEnterAnimation 属性不存在

android - 如何取消像whatsapp这样的图片和视频上传

Android:自定义圆形ProgressBar,中间有空格

android - 共享首选项 - 启动时崩溃

android - Python - MySQL 数据库更改后向 Android 应用程序发送通知

android - 在 Android Studio 1.4.1 中设置 Android 工具栏背景和文本颜色

android - 如何制作 AlertDialog 自定义按钮 android?

Android:如何在进度条处于 Activity 状态时禁用控件

java - android 如何处理url中的空格

android - 尝试在运行 4.2 的设备上访问 Picasa 图像时出现安全异常