【问题标题】:changing CircularProgressIndicator width or height not applied未应用更改 CircularProgressIndicator 宽度或高度
【发布时间】:2021-02-15 09:38:41
【问题描述】:

我想使用来自 Material Library 宽度自定义大小的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>

【问题讨论】:

  • Shapeable 图像视图有可能在视图创建后获取其源集吗?如果是这样,您可能需要再次 requestLayout() 以便 CL 可以重新计算视图的实际宽度/高度,因为它们是相互关联的(又名:进度指示器的大小将取决于图像视图的大小,这是未知的当视图膨胀时,它采用进度指示器的默认大小(就像它被包裹一样)。当新图像进入时(对于可塑形),进度指示器已经被测量/布局。(这是一个理论):)
  • 使用 tools:src 对编辑器来说很好,但在运行时会做零/无 :)
  • 我只是使用工具来展示我想要实现的目标。我将ShapeableImageViewapp:layout_constraintDimensionRatio="H,1:1"android:layout_width="0dp"android:layout_height="0dp" 一起使用,这将在开始时定义视图大小。我认为ShapeableImageView 没关系。如果我将其更改为任何其他视图结果将是相同的

标签: android android-progressbar material-components-android


【解决方案1】:

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

【讨论】:

  • 当然,但我想在约束布局中使用它来自动适应另一个视图
  • @vahid 在这种情况下,您不能在 xml 中分配值,而是以编程方式测量正确的维度。在任何情况下,您都必须使用progressBar.indicatorSize 方法。 android:layout_heightandroid:layout_width 不会更改进度条/指示器尺寸。
  • 我以为有办法通过 xml 应用它,但似乎没有办法。无论如何感谢您的回复。而且我不知道我应该接受你的回答还是不接受:)。这不是我想要的,但在固定大小的情况下可以帮助其他人
  • @vahid 没问题。刚刚添加了一个如何以编程方式更改指标大小的示例。
  • 提示:使用 core-ktx 中的 doOnLayout 而不是 addOnGlobalLayoutListener
猜你喜欢
  • 1970-01-01
  • 2014-09-26
  • 1970-01-01
  • 2021-02-01
  • 2020-01-23
  • 2016-01-01
  • 1970-01-01
  • 2013-09-11
  • 1970-01-01
相关资源
最近更新 更多