【问题标题】:Android MotionLayout setGuidelinePercent not working on alpha4 releaseAndroid MotionLayout setGuidelinePercent 不适用于 alpha4 版本
【发布时间】:2019-04-17 07:29:31
【问题描述】:

我的应用中有一个用于滑动过渡的 MotionLayout。目前我刚刚从2.0.0-alpha3 更新到2.0.0-alpha4 版本,有些东西不像以前那样工作。 有“myView”,这是一个应该从屏幕底部扩展到标题视图的布局。它对我需要以编程方式控制的指南有一个限制(基于一些用户操作)。

代码中没有其他更改,因此降级到 alpha3 可以让一切按预期工作。

代码非常基本:

<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">

    <Transition
        motion:constraintSetEnd="@id/end"
        motion:constraintSetStart="@id/start"
        motion:duration="1000"
        motion:interpolator="linear">
        <!-- I used motion:motionInterpolator="linear" for alpha4-->

        <OnSwipe
            motion:dragDirection="dragUp"
            motion:touchAnchorId="@id/myView"
            motion:touchAnchorSide="top" />

    </Transition>

    <ConstraintSet android:id="@+id/start">
        <Constraint
            android:id="@id/myView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toTopOf="@id/myGuideline" />
    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@id/myView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintTop_toBottomOf="@id/someHeaderView" />
    </ConstraintSet>
</MotionScene>

问题是在 alpha3 版本中,我可以使用类似这样的东西(并且有效):

myMotionLayout.getConstraintSet(R.id.start)?.setGuidelinePercent(R.id.myGuideline, guidelinePercentage)

现在我升级到2.0.0-alpha4,这不再有效,我不明白为什么。它总是从 xml 中获取默认值,并且在我尝试动态执行时永远不会更改。

我要做的是根据一些动态数据(在片段开头或用户返回时可用)更改过渡的起点。如果您对如何使用 MotionLayout 实现这一点有更好的了解,请给我一个提示。

Gradle 实现:

implementation "com.android.support.constraint:constraint-layout:2.0.0-alpha4"

【问题讨论】:

  • 我遇到了类似的问题:我正在尝试使用 val constraintSet = ConstraintSet() constraintSet.clone(motion_layout) constraintSet.clear(R.id.txt_view, ConstraintSet.BOTTOM) constraintSet.applyTo(motion_layout) 以编程方式从 MotionLayout 中删除约束它使用 ConstraintLayout alpha 3,但从 alpha 4 到最新的 beta 停止工作1. 有趣的是,当我旋转设备时,会按预期进行约束移除。

标签: android android-layout kotlin android-motionlayout


【解决方案1】:

我注意到 alpha4 版本中引入了类似的回归。通过反复试验和以下错误报告,我能够以编程方式在 MotionLayout(beta1 版本)中应用我的 ConstraintSet 更改。看来克隆方法不适用于 MotionLayout,应该使用 getConstraintSet 代替(正如您所做的那样)。看起来您缺少的步骤是constraintSet.applyTo(motion_layout)。最后我还需要重新应用 MotionLayout 描述 xml。

实施 "androidx.constraintlayout:constraintlayout:2.0.0-beta1"

步骤:

val constraintSet = motion_layout.getConstraintSet(R.id.motion_layout)
constraintSet.clear(R.id.txt_view, ConstraintSet.BOTTOM)
constraintSet.applyTo(motion_layout)
// If using a MotionLayout description xml file, re-apply it
motion_explore_article.loadLayoutDescription(R.xml.slide_over_image)

错误报告: https://issuetracker.google.com/issues/131874827

【讨论】:

    猜你喜欢
    • 2022-09-27
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多