【问题标题】:Android CustomAttribute in MotionLayout to change Text in TextViewMotionLayout 中的 Android CustomAttribute 更改 TextView 中的文本
【发布时间】:2019-04-13 15:27:53
【问题描述】:

任何人都可以帮助我以正确的方式更改TextViewMotionLayout 中的文本...这就是我正在做的事情。

我正在一个简单的应用程序上测试MotionLayout... 我到达Motion tutorials 上关于CustomAttributes 的部分

使用它们,您可以更改ViewBackgroundColor,也可以使用customColorValue 更改textColor

在这种情况下,您可以看到在开始和结束场景中更改此值非常有效:

        <CustomAttribute
            motion:attributeName="backgroundColor"
            motion:customColorValue="#004A6D" />

        <CustomAttribute
            motion:attributeName="textColor"
            motion:customColorValue="#000000" />

我还注意到有一个customStringValue,所以我想我可以将TextView 文本更改为“之前”->“之后”。但是当我尝试使用CustomAttribute 设置时,应用程序崩溃了。

在开始场景中:

<CustomAttribute
            motion:attributeName="Text"
            motion:customStringValue="BEFORE" />

在最后一幕中:

        <CustomAttribute
            motion:attributeName="Text"
            motion:customStringValue="AFTER" />

在 MotionScene 外部,textView 文本是 TEST

  • 当我为结束场景设置CustomAttribute 时...文本从初始值 TEST 更改为结束 AFTER 值...所以它部分工作,但永远不会回到初始状态。
  • TextView 上没有设置初始文本时也会发生这种情况。它可以部分工作。

所以...任何人都可以帮助我以正确的方式更改TextViewMotionLayout 中的文本。

【问题讨论】:

  • Oscar,你找到解决办法了吗?我有同样的问题:(
  • 如果没有,您是否向 Google 报告了此问题?

标签: android android-custom-attributes android-motionlayout


【解决方案1】:

您可以像这样使用 TransitionListener 以编程方式完成:

motionLayout.setTransitionListener(object : MotionLayout.TransitionListener {
    override fun onTransitionTrigger(p0: MotionLayout?, p1: Int, p2: Boolean, p3: Float) {
        // ADD YOUR CODE HERE
    }

    override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) {
        // ADD YOUR CODE HERE
    }

    override fun onTransitionChange(p0: MotionLayout?, p1: Int, p2: Int, p3: Float) {
        textView.text = if(p3==0f) "before" else "after"
    }

    override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) {

    }
})

【讨论】:

  • 这对我有用。我认为我们现在需要使用此解决方法,直到 Google 发布针对此问题的错误修复。
【解决方案2】:

这样做的简单方法是

 <ConstraintSet android:id="@+id/start">
    <Constraint
        android:id="@id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
      >

        <CustomAttribute
            app:attributeName="textColor"
            app:customColorValue="#1D1D1D" />
    </Constraint>
</ConstraintSet>

    <Constraint
        android:id="@id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <CustomAttribute
            app:attributeName="textColor"
            app:customColorValue="#FFFFFF" />
    </Constraint>
</ConstraintSet>

将动作更改为应用程序

【讨论】:

  • 随着constraintlayout:2.0.0-beta6motion 更改为app 实际有效。我什至不... ??‍♂️
【解决方案3】:

我测试并喜欢这个

//constraintSetStart
<ConstraintSet...>
<Constraint
  android:id="@+id/button">
    <CustomAttribute
       motion:attributeName="Text"
       motion:customStringValue="CLOSE"/>
</Constraint>
</ConstraintSet>

//constraintSetEnd
<ConstrainSet..>
<Constraint
   android:id="@+id/button">
     <CustomAttribute
       motion:attributeName="Text"
       motion:customStringValue="OPEN"/>
 </Constraint>
</ConstraintSet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 2011-01-19
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多