更改 CircularProgressIndicator 宽度或高度未应用

问题描述 投票:0回答:2

我想使用材质库中的

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>
android android-progressbar material-components-android
2个回答
8
投票

要更改 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
            
        }
    });

0
投票

enter image description here

必须同时设置indicatorSize和trackThickness。

© www.soinside.com 2019 - 2024. All rights reserved.