Android 以编程方式设置的约束布局不正确

Android constraint layout set programmatically is incorrect

所以我一直在尝试以编程方式设置约束布局。 原版布局是这样的

布局文件

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="10dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.3">

        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintHeight_percent="1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_default="percent"
            app:layout_constraintWidth_percent="0.48"
            app:cardCornerRadius="20dp"
            app:cardElevation="10dp">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/card1_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/up_arrow">

                <TextView
                    android:id="@+id/card1_company"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_margin="10dp"
                    android:text="CompanyName"
                    android:textColor="@color/black"
                    android:textSize="20sp"
                    app:layout_constraintHeight_default="percent"
                    app:layout_constraintHeight_percent="0.1"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintWidth_default="percent"
                    app:layout_constraintWidth_percent="0.5" />

                <TextView
                    android:id="@+id/card1_point"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_margin="10dp"
                    android:text="CompanyPoint"
                    android:textColor="@color/black"
                    app:layout_constraintHeight_default="percent"
                    app:layout_constraintHeight_percent="0.1"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/card1_company"
                    app:layout_constraintWidth_default="percent"
                    app:layout_constraintWidth_percent="0.5" />

                <TextView
                    android:id="@+id/card1_des"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_margin="10dp"
                    android:text="SOME COMPANY DESCRIPTION"
                    android:textColor="@color/black"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHeight_default="wrap"
                    app:layout_constraintWidth_default="percent"
                    app:layout_constraintWidth_percent="0.5"/>

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintHeight_percent="1"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_default="percent"
            app:layout_constraintWidth_percent="0.48"
            app:cardCornerRadius="20dp"
            app:cardElevation="10dp">


            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/card2_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/up_arrow">

                <TextView
                    android:id="@+id/card2_company"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_margin="10dp"
                    android:text="CompanyName"
                    android:textColor="@color/black"
                    android:textSize="20sp"
                    app:layout_constraintHeight_default="percent"
                    app:layout_constraintHeight_percent="0.1"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintWidth_default="percent"
                    app:layout_constraintWidth_percent="0.5" />

                <TextView
                    android:id="@+id/card2_point"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_margin="10dp"
                    android:text="CompanyPoint"
                    android:textColor="@color/black"
                    app:layout_constraintHeight_default="percent"
                    app:layout_constraintHeight_percent="0.1"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/card2_company"
                    app:layout_constraintWidth_default="percent"
                    app:layout_constraintWidth_percent="0.5" />

                <TextView
                    android:id="@+id/card2_des"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_margin="10dp"
                    android:text="SOME COMPANY DESCRIPTION"
                    android:textColor="@color/black"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHeight_default="wrap"
                    app:layout_constraintWidth_default="percent"
                    app:layout_constraintWidth_percent="0.5" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.cardview.widget.CardView>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.01"/>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.3">

        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintHeight_percent="1"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_default="percent"
            app:layout_constraintWidth_percent="0.48"
            app:cardCornerRadius="20dp"
            app:cardElevation="10dp">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/card3_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/down_arrow">

                <TextView
                    android:id="@+id/card3_company"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_margin="10dp"
                    android:text="CompanyName"
                    android:textColor="@color/black"
                    android:textSize="20sp"
                    app:layout_constraintHeight_default="percent"
                    app:layout_constraintHeight_percent="0.1"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintWidth_default="percent"
                    app:layout_constraintWidth_percent="0.5" />

                <TextView
                    android:id="@+id/card3_point"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_margin="10dp"
                    android:text="CompanyPoint"
                    android:textColor="@color/black"
                    app:layout_constraintHeight_default="percent"
                    app:layout_constraintHeight_percent="0.1"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/card3_company"
                    app:layout_constraintWidth_default="percent"
                    app:layout_constraintWidth_percent="0.5" />

                <TextView
                    android:id="@+id/card3_des"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_margin="10dp"
                    android:text="SOME COMPANY DESCRIPTION"
                    android:textColor="@color/black"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintHeight_default="wrap"
                    app:layout_constraintWidth_default="percent"
                    app:layout_constraintWidth_percent="0.5" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintHeight_percent="1"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_default="percent"
            app:layout_constraintWidth_percent="0.48"
            app:cardCornerRadius="20dp"
            app:cardElevation="10dp">


            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/card4_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/down_arrow">

                <TextView
                    android:id="@+id/card4_company"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_margin="10dp"
                    android:text="CompanyName"
                    android:textColor="@color/black"
                    android:textSize="20sp"
                    app:layout_constraintHeight_default="percent"
                    app:layout_constraintHeight_percent="0.1"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintWidth_default="percent"
                    app:layout_constraintWidth_percent="0.5" />

                <TextView
                    android:id="@+id/card4_point"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_margin="10dp"
                    android:text="CompanyPoint"
                    android:textColor="@color/black"
                    app:layout_constraintHeight_default="percent"
                    app:layout_constraintHeight_percent="0.1"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/card4_company"
                    app:layout_constraintWidth_default="percent"
                    app:layout_constraintWidth_percent="0.5" />

                <TextView
                    android:id="@+id/card4_des"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_margin="10dp"
                    android:text="SOME COMPANY DESCRIPTION"
                    android:textColor="@color/black"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintHeight_default="wrap"
                    app:layout_constraintWidth_default="percent"
                    app:layout_constraintWidth_percent="0.5" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.cardview.widget.CardView>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.01"/>


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.3">

        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintHeight_percent="1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_default="percent"
            app:layout_constraintWidth_percent="0.48"
            app:cardCornerRadius="20dp"
            app:cardElevation="10dp">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/card5_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/up_arrow">

                <TextView
                    android:id="@+id/card5_company"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_margin="10dp"
                    android:text="CompanyName"
                    android:textColor="@color/black"
                    android:textSize="20sp"
                    app:layout_constraintHeight_default="percent"
                    app:layout_constraintHeight_percent="0.1"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintWidth_default="percent"
                    app:layout_constraintWidth_percent="0.5" />

                <TextView
                    android:id="@+id/card5_point"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_margin="10dp"
                    android:text="CompanyPoint"
                    android:textColor="@color/black"
                    app:layout_constraintHeight_default="percent"
                    app:layout_constraintHeight_percent="0.1"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/card5_company"
                    app:layout_constraintWidth_default="percent"
                    app:layout_constraintWidth_percent="0.5" />

                <TextView
                    android:id="@+id/card5_des"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_margin="10dp"
                    android:text="SOME COMPANY DESCRIPTION"
                    android:textColor="@color/black"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHeight_default="wrap"
                    app:layout_constraintWidth_default="percent"
                    app:layout_constraintWidth_percent="0.5"/>

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_default="percent"
            app:layout_constraintHeight_percent="1"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_default="percent"
            app:layout_constraintWidth_percent="0.48"
            app:cardCornerRadius="20dp"
            app:cardElevation="10dp">


            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/card6_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/down_arrow">

                <TextView
                    android:id="@+id/card6_company"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_margin="10dp"
                    android:text="CompanyName"
                    android:textColor="@color/black"
                    android:textSize="20sp"
                    app:layout_constraintHeight_default="percent"
                    app:layout_constraintHeight_percent="0.1"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintWidth_default="percent"
                    app:layout_constraintWidth_percent="0.5" />

                <TextView
                    android:id="@+id/card6_point"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_margin="10dp"
                    android:text="CompanyPoint"
                    android:textColor="@color/black"
                    app:layout_constraintHeight_default="percent"
                    app:layout_constraintHeight_percent="0.1"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/card6_company"
                    app:layout_constraintWidth_default="percent"
                    app:layout_constraintWidth_percent="0.5" />

                <TextView
                    android:id="@+id/card6_des"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_margin="10dp"
                    android:text="SOME COMPANY DESCRIPTION"
                    android:textColor="@color/black"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintHeight_default="wrap"
                    app:layout_constraintWidth_default="percent"
                    app:layout_constraintWidth_percent="0.5" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.cardview.widget.CardView>

    </androidx.constraintlayout.widget.ConstraintLayout>

</LinearLayout>

因此,根据公司点,设置布局。如果积分为负,则公司名称和积分文本设置为父项的末尾,描述为开头,如图所示,如果积分为正,反之亦然。

但是当我以编程方式设置它时,对于进行更改的某些布局,文本设置很奇怪,例如查看下一张以编程方式设置的图片

如果更改布局,则某些文本的位置更靠近中心 例如,请注意第二张图片、垂直第二个布局和第三个布局中标识的差异。 第三个布局不必更改,因此它的位置与未以编程方式设置的第一个图片相同。然而,第二个布局被识别,因为它是通过编程方式更改的

以编程方式设置布局的代码

if(arrayList.get(0).getPoints() < 0){
        constraintLayout1.setBackgroundResource(R.drawable.down_arrow);
        company1Name.setGravity(Gravity.END);
        company1point.setGravity(Gravity.END);
        ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) company1Name.getLayoutParams();
        params.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
        params = (ConstraintLayout.LayoutParams) company1point.getLayoutParams();
        params.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
        params = (ConstraintLayout.LayoutParams) company1desc.getLayoutParams();
        params.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
    }else{
        constraintLayout1.setBackgroundResource(R.drawable.up_arrow);
        company1desc.setGravity(Gravity.END);
        ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) company1Name.getLayoutParams();
        params.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
        params = (ConstraintLayout.LayoutParams) company1point.getLayoutParams();
        params.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
        params = (ConstraintLayout.LayoutParams) company1desc.getLayoutParams();
        params.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
    }

如何解决这个问题?

您似乎在为视图设置附加约束,但并未删除原始约束。换句话说,您正在为视图设置开始和结束约束,这将使视图在约束之间居中。我不认为重力有任何影响。

如果需要将视图向右移动,请移除左侧约束并设置右侧。如果要将视图向左移动,请设置左侧约束并移除右侧。您还可以考虑为视图设置两个约束并使用水平偏移将其向左或向右移动。这可能是更简单的方法。

您可以通过将参数设置为 View.NO_ID, but I am not 100% sure about that. I think that it would be better to use ConstraintSet. Also, see 来移除带有参数的约束以获取使用示例。