TextViews 未移动到约束布局中的开始位置

TextViews not moving to Start position in constraint layout

出于某种原因,我的 ConstraintLayout 中的 TextViews 根本不会移动并且卡在中间。如何将它们移动到 Left/Start 位置?我尝试了以下但这些都不起作用。关于其他用途的任何想法?

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    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="wrap_content"
    app:cardUseCompatPadding="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/linearLayoutB"
                android:foreground="?android:attr/selectableItemBackground">

                    <!--Constraint 1-->
                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/constraintLayoutBTitle"
                        android:layout_marginBottom="10dp">

                        <ImageButton
                            android:id="@+id/ibB1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toTopOf="parent"
                            android:background="?attr/selectableItemBackgroundBorderless"/>

                        <TextView
                            android:id="@+id/tvB1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            app:layout_constraintStart_toEndOf="@+id/ibB1"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintTop_toTopOf="parent"
                            app:layout_constrainedWidth="true"
                            style="@android:style/TextAppearance.Medium"/>
                    </androidx.constraintlayout.widget.ConstraintLayout>

                <!--Constraint 2-->
                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/constraintLayoutBContent">

                    <ImageView
                        android:id="@+id/ivB2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:srcCompat="@mipmap/ic_launcher_round" />

                    <TextView
                        android:id="@+id/tvB2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="10dp"
                        android:layout_marginBottom="10dp"
                        app:flow_horizontalBias="0.0"
                        app:layout_constraintStart_toEndOf="@+id/ivB2"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toTopOf="@+id/ivB2"
                        app:layout_constrainedWidth="true"
                        style="@android:style/TextAppearance.Medium"/>

                    <TextView
                        android:id="@+id/tvB3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="10dp"
                        android:layout_marginTop="10dp"
                        android:layout_marginBottom="10dp"
                        app:layout_constraintBottom_toTopOf="@+id/tvB4"
                        app:layout_constraintStart_toEndOf="@+id/ivB2"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/ivB2"
                        app:layout_constrainedWidth="true"
                        style="@android:style/TextAppearance.Medium"/>

                    <ImageView
                        android:id="@+id/ivB4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        app:layout_constraintEnd_toEndOf="@+id/ivB2"
                        app:layout_constraintStart_toStartOf="@+id/ivB2"
                        app:layout_constraintTop_toBottomOf="@+id/ivB2"
                        app:layout_constraintTop_toTopOf="@+id/tvB4"
                        app:srcCompat="@mipmap/ic_launcher_round" />

                    <TextView
                        android:id="@+id/tvB4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="10dp"
                        app:layout_constraintStart_toEndOf="@+id/ivB4"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/tvB3"
                        app:layout_constraintTop_toTopOf="@+id/ivB4"
                        app:layout_constrainedWidth="true"
                        style="@android:style/TextAppearance.Medium"/>
                </androidx.constraintlayout.widget.ConstraintLayout>
            </LinearLayout>
</androidx.cardview.widget.CardView>

如果你想得到类似的东西,请复制下面的代码。

android:layout_width="0dp"

将 textView 放在左侧的关键是展开 textview 以匹配约束。

<androidx.cardview.widget.CardView
    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="wrap_content"
    app:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/linearLayoutB"
        android:foreground="?android:attr/selectableItemBackground">

        <!--Constraint 1-->
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/constraintLayoutBTitle"
            android:layout_marginBottom="10dp">

            <ImageButton
                android:id="@+id/ibB1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:background="?attr/selectableItemBackgroundBorderless"/>

            <TextView
                android:id="@+id/tvB1"
                style="@android:style/TextAppearance.Medium"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="tvB1"
                app:layout_constrainedWidth="true"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/ibB1"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>

        <!--Constraint 2-->
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLayoutBContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/ivB2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@mipmap/ic_launcher_round" />

            <TextView
                android:id="@+id/tvB2"
                style="@android:style/TextAppearance.Medium"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:text="tvb2"
                app:layout_constrainedWidth="true"
                app:layout_constraintBottom_toBottomOf="@+id/ivB2"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/ivB2"
                app:layout_constraintTop_toTopOf="@+id/ivB2" />

            <TextView
                android:id="@+id/tvB3"
                style="@android:style/TextAppearance.Medium"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:text="tvb3"
                app:layout_constrainedWidth="true"
                app:layout_constraintBottom_toTopOf="@+id/tvB4"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toEndOf="@+id/ivB2"
                app:layout_constraintTop_toTopOf="@+id/ivB4" />

            <ImageView
                android:id="@+id/ivB4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                app:layout_constraintStart_toStartOf="@+id/ivB2"
                app:layout_constraintTop_toBottomOf="@+id/ivB2"
                app:srcCompat="@mipmap/ic_launcher_round" />

            <TextView
                android:id="@+id/tvB4"
                style="@android:style/TextAppearance.Medium"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:text="tvb4"
                app:layout_constrainedWidth="true"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toEndOf="@+id/ivB4"
                app:layout_constraintTop_toBottomOf="@+id/tvB3" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </LinearLayout>
</androidx.cardview.widget.CardView>

在这里,我只是将偏差设置为0.0

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardUseCompatPadding="true">

    <LinearLayout
        android:id="@+id/linearLayoutB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foreground="?android:attr/selectableItemBackground"
        android:orientation="vertical">

        <!--Constraint 1-->
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLayoutBTitle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="10dp">

            <ImageButton
                android:id="@+id/ibB1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="?attr/selectableItemBackgroundBorderless"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/tvB1"
                style="@android:style/TextAppearance.Medium"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constrainedWidth="true"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/ibB1"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>

        <!--Constraint 2-->
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLayoutBContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/ivB2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@mipmap/ic_launcher_round" />

            <TextView
                android:id="@+id/tvB2"
                style="@android:style/TextAppearance.Medium"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                app:layout_constrainedWidth="true"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toEndOf="@+id/ivB2"
                app:layout_constraintTop_toTopOf="@+id/ivB2"
                tools:text="2a" />

            <TextView
                android:id="@+id/tvB3"
                style="@android:style/TextAppearance.Medium"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                app:layout_constrainedWidth="true"
                app:layout_constraintBottom_toTopOf="@+id/tvB4"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toEndOf="@+id/ivB2"
                app:layout_constraintTop_toBottomOf="@+id/ivB2"
                tools:text="2b" />

            <ImageView
                android:id="@+id/ivB4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintEnd_toEndOf="@+id/ivB2"
                app:layout_constraintStart_toStartOf="@+id/ivB2"
                app:layout_constraintTop_toBottomOf="@+id/ivB2"
                app:layout_constraintTop_toTopOf="@+id/tvB4"
                app:srcCompat="@mipmap/ic_launcher_round" />

            <TextView
                android:id="@+id/tvB4"
                style="@android:style/TextAppearance.Medium"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                app:layout_constrainedWidth="true"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toEndOf="@+id/ivB4"
                app:layout_constraintTop_toBottomOf="@+id/tvB3"
                tools:text="2c" />
        </androidx.constraintlayout.widget.ConstraintLayout>

    </LinearLayout>

</androidx.cardview.widget.CardView>

结果:


但我认为在你的布局中你添加了太多的约束。您不必为每个 textView 都将约束设置为卡片视图的末尾。只要加一个就会自动开始