我的应用程序UI使用的是Android Support Library构建的,但目前没有AppCompat版本的(不确定性)进度条,这是我应用程序真正需要的。
我宁愿不使用任何第三方库来实现材料设计进度条,因此我想知道是否有人掌握了为什么它没有包含在支持库中的信息,并且是否有任何迹象表明它即将到来(以及何时)。
我的应用程序UI使用的是Android Support Library构建的,但目前没有AppCompat版本的(不确定性)进度条,这是我应用程序真正需要的。
我宁愿不使用任何第三方库来实现材料设计进度条,因此我想知道是否有人掌握了为什么它没有包含在支持库中的信息,并且是否有任何迹象表明它即将到来(以及何时)。
Jetpack Compose
您可以使用LinearProgressIndicator
或CircularProgressIndicator
。
// 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>
你还可以使用CircularProgressIndicator
组件来获得一个圆形进度指示器:
<com.google.android.material.progressindicator.CircularProgressIndicator
android:indeterminate="true"
app:indicatorColor="?attr/colorPrimary"/>
注意:它至少需要版本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" />
他们遵循官方材料指南。
这是对一个旧问题的答案,但我想其他读者可能会对这个解决方案感兴趣:
支持库的新版本(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);
}
}
}
我只是试图通过使用样式来改进Gabriele Mariotti先生的答案。
编写如下样式
<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>
像这样使用它与您的进度条。
<ProgressBar style="@style/infinite_progress_horizontal" />
nonAppCompat
版本中你将会编写什么代码,以及其他开发人员建议的解决办法。 - Budius