如何添加不确定进度条

52

我的应用程序UI使用的是Android Support Library构建的,但目前没有AppCompat版本的(不确定性)进度条,这是我应用程序真正需要的。

我宁愿不使用任何第三方库来实现材料设计进度条,因此我想知道是否有人掌握了为什么它没有包含在支持库中的信息,并且是否有任何迹象表明它即将到来(以及何时)。


我投票关闭此问题,因为它涉及第三方库可能的未来功能和预计时间。 - Budius
当你说 ETA 时,你的意思是什么? - Joakim
预计到达时间(ETA) - Budius
我误读了评论。我没有看到任何规则说明这个问题为什么会被视为离题。 - Joakim
2
你的问题并不是一个实际的编程问题,无法用适当的实用方式来回答。它在问为什么一个公司发布了一个没有你期望的功能的库,以及该公司是否考虑添加它。这纯属推测。任何人要回答它的唯一方法就是如果谷歌的某个人决定来这里告诉你为什么他们认为它不重要。也许你可以重新写下你的问题,准确地描述你想要实现什么(带有屏幕截图),在nonAppCompat版本中你将会编写什么代码,以及其他开发人员建议的解决办法。 - Budius
3
我不要求任何人猜测。我想知道是否有未来支持库功能清单和/或计划安排时程。 - Joakim
3个回答

99

Jetpack Compose

您可以使用LinearProgressIndicatorCircularProgressIndicator

// Indeterminate
CircularProgressIndicator()
LinearProgressIndicator()

// Determinate (specify the progress value with a fixed value or animateFloatAsState to animate it)
CircularProgressIndicator(progress = ..)
LinearProgressIndicator(progress = ..)

例子:

var progress by remember {  mutableStateOf(0.1f) }

LinearProgressIndicator(
    backgroundColor = Color.White,
    progress = progress,
    color = Blue
)

Material组件库

使用带有android:indeterminate="true"属性的LinearProgressIndicator

<com.google.android.material.progressindicator.LinearProgressIndicator
    android:indeterminate="true"
    app:indicatorColor="?attr/colorPrimary"/>

输入图像描述这里

您也可以使用不同的颜色,方法如下:

<com.google.android.material.progressindicator.LinearProgressIndicator
        android:indeterminate="true"
        app:indicatorColor="@array/progress_colors"
        app:indeterminateAnimationType="contiguous"/>

随着:

  <integer-array name="progress_colors">
    <item>@color/...</item>
    <item>@color/....</item>
    <item>@color/....</item>
  </integer-array>

enter image description here

你还可以使用CircularProgressIndicator组件来获得一个圆形进度指示器:

<com.google.android.material.progressindicator.CircularProgressIndicator
    android:indeterminate="true"
    app:indicatorColor="?attr/colorPrimary"/>

enter image description here

注意:至少需要版本1.3.0-alpha04


AppCompat

您可以使用带有AppCompat风格的ProgressBar

只需在您的布局中添加这个:

<ProgressBar
        style="@style/Base.Widget.AppCompat.ProgressBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:visibility="visible" />

如果你想要一个水平进度条,请使用:

  <ProgressBar
        style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="8dp"
        android:layout_marginTop="24dp"
        android:indeterminate="true"
        android:visibility="visible" />

他们遵循官方材料指南


如何在带有webView的操作栏中使用此不确定进度条? - Mohammad
需要补充说明的是,对于不确定的圆形进度条,这种方法不起作用。它们缺少动画矢量可绘制图形。 - Robert Baker
1
@Robert,你有什么提示可以用来处理不定圆的情况吗?这是我通过谷歌搜索得到的最佳结果之一。 - Richard Le Mesurier
1
@RichardLeMesurier 你可以尝试包含这个实现,来自Chromium。它不使用矢量可绘制图形:https://chromium.googlesource.com/android_tools/+/master/sdk/extras/android/support/v4/src/java/android/support/v4/widget/MaterialProgressDrawable.java - gnuf
21
这个答案仅描述如何使用平台原生的进度条。在早期的设备上,你会得到一个未着色的 holo 进度条。这并没有回答问题。 - BladeCoder
显示剩余3条评论

7

这是对一个旧问题的答案,但我想其他读者可能会对这个解决方案感兴趣:

支持库的新版本(26.1.+)包含一个名为CircularProgressDrawable的类,它实现了原生材料不确定可绘制的确切外观。为了在布局中轻松使用它,你可以像这样构建一个MaterialProgressBar

public class MaterialProgressBar extends ProgressBar{
    // Same dimensions as medium-sized native Material progress bar
    private static final int RADIUS_DP = 16;
    private static final int WIDTH_DP = 4;

    public MaterialProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);

        // The version range is more or less arbitrary - you might want to modify it
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP
                || Build.VERSION.SDK_INT > Build.VERSION_CODES.O_MR1) {

            final DisplayMetrics metrics = getResources().getDisplayMetrics();
            final float screenDensity = metrics.density;

            CircularProgressDrawable drawable = new CircularProgressDrawable(context);
            drawable.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
            drawable.setCenterRadius(RADIUS_DP * screenDensity);
            drawable.setStrokeWidth(WIDTH_DP * screenDensity);
            setIndeterminateDrawable(drawable);
        }
    }
}

0

我只是试图通过使用样式来改进Gabriele Mariotti先生的答案。

  1. 编写如下样式

    <style name="infinite_progress_horizontal" parent="Widget.AppCompat.ProgressBar.Horizontal">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:indeterminate">true</item>
    </style>
    
  2. 像这样使用它与您的进度条。

    <ProgressBar style="@style/infinite_progress_horizontal" />
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接