问题描述
在我们的应用程序中,我们需要一个不确定的进度条,如下所示:
In our application we need an indeterminate progress bar, like so:
我们可以通过在ProgressBar上设置负余量来实现这一点,如下所示:
We can achieve this by setting a negative margin on the ProgressBar, like this:
<ProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:marginTop="-7dp"
android:visibility="@{loading ? View.VISIBLE : View.GONE}" />
但是因为ConstraintLayout不支持负边距,它看起来像这样:
BUT because ConstraintLayout does not support negative margins, it will look like this:
好的,负边距是黑客。让我们换一个不同的黑客,好吗?让我们介绍我们的自定义视图 CustomProgressBar
,它扩展 ProgressBar
并覆盖其 onDraw
方法,如下所示:
OK, the negative margin was a hack. Let's replace it with a different hack, shall we? Let's introduce our custom view CustomProgressBar
, which extends ProgressBar
and overrides its onDraw
method, like this:
@Override
protected void onDraw(Canvas canvas) {
int marginTop = dpToPx(7);
canvas.translate(0, -marginTop);
super.onDraw(canvas);
}
但所有这些都闻起来像坏代码。必须有一个更好的解决方案!
你会推荐什么?
But all of this smells like bad code. There has to be a better solution!What would you recommend?
推荐答案
另一种方法是在父母之间使用指南和中心ProgressBar顶部和指南。
Another way to do this is to use a Guideline and center ProgressBar between the parent top and the guideline.
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:paddingTop="-4dp"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline"
android:orientation="horizontal"
app:layout_constraintGuide_begin="4dp" />
这篇关于在水平进度条中删除填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!