【问题标题】:Change color Progress indicator with Databinding?使用数据绑定更改颜色进度指示器?
【发布时间】:2022-11-23 17:51:15
【问题描述】:

我尝试了所有方法,但无法更改进度条的颜色。我试图不仅将其更改为绑定,而且还尝试将其更改为适配器,但它不起作用。你能帮忙吗?

.....

    <com.google.android.material.progressindicator.CircularProgressIndicator
                            android:id="@+id/progressBar"
                            android:layout_width="160dp"
                            android:layout_height="160dp"
                            android:progress="25"
                            app:indicatorColor="@{session.getChannel() == Channel.MOBILE ? @color/cerulean : @color/teal}"
                            app:indicatorDirectionCircular="counterclockwise"
                            app:indicatorInset="7dp"
                            app:indicatorSize="140dp"
                            app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintLeft_toLeftOf="parent"
                            app:layout_constraintRight_toRightOf="parent"
                            app:layout_constraintTop_toTopOf="parent"
                            app:trackColor="#DEE3ED"
                            app:trackCornerRadius="5dp"
                            app:trackThickness="9dp" />

在这种情况下我得到这个错误找不到接受参数类型“android.content.res.ColorStateList”的 <com.google.android.material.progressindicator.CircularProgressIndicator app:indicatorColor> 的设置器 如果绑定适配器提供了设置器,请检查适配器是否正确注释以及参数类型是否匹配。

val color = R.color.cerulean
             
progressBar.getIndeterminateDrawable().setColorFilter(color, 
                PorterDuff.Mode.SRC_IN)

然后我尝试了这段代码但又失败了。不明确的

【问题讨论】:

    标签: android kotlin data-binding android-databinding


    【解决方案1】:

    您可以像这样使用自定义绑定适配器

    @BindingAdapter("app:indicatorColor")
    fun setIndicatorColor(progress: CircularProgressIndicator, color: Int) {
        progress.setIndicatorColor(color)
    }
    

    之后,您可以在 xml 中使用您的代码

    app:indicatorColor="@{session.getChannel() == Channel.MOBILE ? @color/cerulean : @color/teal}"
    

    【讨论】:

      猜你喜欢
      • 2012-04-17
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 2021-12-19
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多