首页 > 解决方案 > 以编程方式运动布局

问题描述

这是我在这个平台上的第一个问题,我正在制作应用程序的一部分,我需要使用运动布局动态移动文本视图,但是当我showBubble ()在执行应用程序时添加我的方法时不显示文本视图,有一些东西我想?我认为在检测 BubbleUi 的 id 时 XML 中存在一些错误,但归根结底,它可能比看起来更容易,你知道 :(

注:我正在翻译这篇文章,如果写作有任何错误,请见谅

XML:

<?xml version="1.0" encoding="utf-8"?>
<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="3500"
        >
        <OnClick
            motion:targetId="@+id/Bubble"
            motion:clickAction="toggle"
            />

    </Transition>


    <ConstraintSet android:id="@+id/start">

    <Constraint
        android:id="@id/Bubble"

        android:layout_width="70dp"
        android:layout_height="70dp"

        motion:layout_constraintTop_toTopOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintEnd_toEndOf="parent"

        >
        <CustomAttribute
            motion:attributeName="backgroundColor"
            motion:customColorValue="@color/colorGreenLight"
            />

    </Constraint>


    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">

        <Constraint
            android:id="@+id/Bubble"
            android:layout_width="100dp"
            android:layout_height="100dp"

            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintStart_toStartOf="parent"
            motion:layout_constraintEnd_toEndOf="parent"
            >
            <CustomAttribute
                motion:attributeName="backgroundColor"
                motion:customColorValue="@color/colorGreenLight"
                />

        </Constraint>


    </ConstraintSet>

</MotionScene>

爪哇:

private void ShowBubble(String tempWord) {
        //Create Motion layout.
        MotionLayout motionBubble = new MotionLayout(ActivityGame.this);
        motionBubble.setId(View.generateViewId());
        motionBubble.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        LinearLayout.LayoutParams linearParams= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
        linearParams.setMargins(5,0,5,0);
        motionBubble.setLayoutParams(linearParams);

        //Create the Bubble.
        TextView BubbleUi = new TextView(ActivityGame.this);
        BubbleUi.setId(R.id.Bubble);

        BubbleUi.setText(tempWord);
        BubbleUi.setTextSize(24);
        BubbleUi.setTextColor(getResources().getColor(R.color.colorDefaultWhite));
        BubbleUi.setBackgroundColor(getResources().getColor(R.color.colorGreenLight));
        BubbleUi.setLayoutParams(new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT));

        BubbleUi.setGravity(Gravity.CENTER);

        motionBubble.addView(BubbleUi,0);

        ConstraintSet constraintSetBubble = new ConstraintSet();
        constraintSetBubble.clone(motionBubble);

        constraintSetBubble.connect(BubbleUi.getId(),ConstraintSet.TOP,ConstraintSet.PARENT_ID,ConstraintSet.TOP);
        constraintSetBubble.connect(BubbleUi.getId(),ConstraintSet.START,ConstraintSet.PARENT_ID,ConstraintSet.START);
        constraintSetBubble.connect(BubbleUi.getId(),ConstraintSet.END,ConstraintSet.PARENT_ID,ConstraintSet.END);

        constraintSetBubble.applyTo(motionBubble);

        //set transition.

        motionBubble.loadLayoutDescription(R.xml.scene_bubbles);
        linearLayoutBubbles.addView(motionBubble,indiceBubble);

        CurrentMotionBubbleCycle [indiceBubble] = motionBubble;

        motionBubble.transitionToStart();
        motionBubble.transitionToEnd();
    }

标签: androidandroid-motionlayout

解决方案


推荐阅读