Android 支持库什么时候出现不确定的进度条

When is indeterminate progressbar coming to Android Support Library

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

我宁愿不使用任何第三方库来实现 material 设计进度条,所以我想知道是否有人知道为什么它不包含在支持库中,以及是否有任何信息它到达的迹象(以及何时)。

Material 组件库

您可以将 LinearProgressIndicatorandroid:indeterminate="true" 属性一起使用:

<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>

您还可以使用 CircularProgressIndicator 组件来获得循环进度指示器:

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

注意:需要至少1.3.0-alpha04

版本

Jetpack 组合

通过 1.0.x 您可以使用 LinearProgressIndicator or CircularProgressIndicator

// Indeterminate
CircularProgressIndicator()
LinearProgressIndicator()

// Determinate
CircularProgressIndicator(progress = ..)
LinearProgressIndicator(progress = ..)

示例:

var progress by remember {  mutableStateOf(0.1f) }

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

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" />

他们关注官方materialguidelines.

我只是想使用样式改进 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" />
    

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

支持库的较新版本 (26.1.+) 包含一个名为 CircularProgressDrawable 的 class,它实现了原生 Material 不确定可绘制对象的精确外观。为了在布局中轻松使用它,您可以像这样构建 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);
        }
    }
}