【问题标题】:Is there a way to set MotionLayout custom attribute for MaterialButton's iconTint?有没有办法为 MaterialButton 的 iconTint 设置 MotionLayout 自定义属性?
【发布时间】:2021-08-15 09:41:26
【问题描述】:

据此article

CustomAttribute 使用 attributeName 指定,它需要匹配对象的 getter/setter 方法,例如: getter:getName(例如 getBackgroundColor) setter: setName (e.g. setBackgroundColor)

(所以motion:attributeName 需要是backgroundColor

我用材质按钮尝试了波纹管属性名称,但没有一个起作用。

<CustomAttribute motion:attributeName="IconTintResource" motion:customColorValue="@color/keyTextColor" />

'IconTintResource', 'iconTintResource', 'IconTint', 'iconTint', 'ColorFilter'

有什么建议吗?

这些是我遇到的错误

E/TransitionLayout: Custom Attribute "IconTint" not found on com.google.android.material.button.MaterialButton

E/TransitionLayout: com.google.android.material.button.MaterialButton must have a method setIconTint

E/TransitionLayout: no method setIconTinton View "f_editor_image_view_terminal"

【问题讨论】:

    标签: android android-motionlayout materialbutton constraintset


    【解决方案1】:

    MotionLayout 的 CustomAttribute 使用反射来设置视图上的值(大致基于 Java bean 约定)

    如果你说

    <CustomAttribute motion:attributeName="foo" motion:customColorValue="@color/keyTextColor" />
    

    它寻找一个方法 setFoo(int value); 不幸的是,即使 MaterialButton 解析了 xml android:iconTint="#FFF" 它没有方法 setIconTint(int color);

    MotionLayout 还会检查 setFoo(Drawable()) 并使用 ColorDrawable

    您可以创建 MaterialButton 的子类并实现所需的方法 setInconTint(int color)

    class MyButton extends MaterialButton {
    
        public MyButton(@NonNull Context context) {
            super(context);
        }
    
        public MyButton(@NonNull Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
        public MyButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        void setIconTint(int color) {
            ColorStateList colorStateList = new ColorStateList(new int[1][0],new int[]{color});
            setIconTint(colorStateList);
        }
    }
    

    这将与 MotionLayout 一起使用。这将在动画期间创建许多对象,但它们将是短暂的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-02
      • 1970-01-01
      • 2011-06-02
      • 2020-02-19
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      • 2021-12-14
      相关资源
      最近更新 更多