android - 未应用更改 CircularProgressIndicator 宽度或高度
问题描述
我想使用CircularProgressIndicator
材料库宽度自定义大小,但是当我为视图设置任何宽度或高度时,只是视图本身改变而不是在它里面圈。我想要实现的是将进度填充到图像视图,如下图所示
这是我的代码
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/circleBackground"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.5"
app:shapeAppearanceOverlay="@style/circleImageView"
tools:src="@tools:sample/avatars" />
<com.google.android.material.progressindicator.CircularProgressIndicator
android:id="@+id/progressBar"
android:layout_width="0dp"
android:layout_height="0dp"
android:max="100"
android:progress="50"
app:indicatorColor="@color/green_true"
app:indicatorDirectionCircular="clockwise"
app:layout_constraintBottom_toBottomOf="@id/circleBackground"
app:layout_constraintLeft_toLeftOf="@id/circleBackground"
app:layout_constraintRight_toRightOf="@id/circleBackground"
app:layout_constraintTop_toTopOf="@id/circleBackground"
app:trackColor="#50ffffff"
app:trackCornerRadius="90dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案
要更改 ProgressIndicator 的维度,您必须使用以下indicatorSize
属性:
<com.google.android.material.progressindicator.CircularProgressIndicator
app:indicatorSize="100dp"
在您的情况下,您必须以编程方式更改它。
就像是:
circleBackground.viewTreeObserver.addOnGlobalLayoutListener(
object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
circleBackground.viewTreeObserver.removeOnGlobalLayoutListener(this);
progressBar.indicatorSize = circleBackground.height
}
});
推荐阅读
- sql - 使用 SQL 比较 2 个 url 主机名
- javascript - 在 Gatsby 中使用链接标签将样式注入 DOM
- python - Python Openpyxl:如何停止推理或覆盖单元格数据类型
- android - 如何使用设计略有不同的相同自定义视图?
- vba - VBA 将行数据移动到其相关工作表,但如果发现重复行,则跳过该行
- javascript - 获取参数并将其用作 Javascript 中的数组名称
- django - Django sum 对象过滤另一个模型
- javascript - 为什么我不能从 jsp 中的嵌入文件执行代码?
- sql-server - 在 Microsoft SQL Server 上加载图像文件
- system-verilog - 对不同的地址范围使用隐含运算符