尽管设置了属性,ConstraintLayout中的TextViews仍未正确包装

时间:2020-09-10 09:51:50

标签: android xml android-layout android-constraintlayout android-cardview

出于某些奇怪的原因,我的TextViews中的ConstraintLayout不会显示所有文本,并且似乎超出了屏幕的边界。我已经尝试过将app:layout_constrainedWidth="true"android:layout_width="wrap_content"一起使用,但这对更改位置没有任何影响,这使我思考当我添加更长的内容时,'1C'和'1D'文本视图会发生什么在其中输入文字。

当前结果

enter image description here

<?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"
    android:layout_marginBottom="20dp"
    app:cardUseCompatPadding="true"
    android:background="@android:color/white">

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

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

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

                        <TextView
                            android:id="@+id/tvA"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginStart="10dp"
                            app:layout_constraintStart_toEndOf="@+id/ibA"
                            app:layout_constraintTop_toTopOf="parent"
                            app:layout_constrainedWidth="true"
                            style="@android:style/TextAppearance.Medium"/>
                    </androidx.constraintlayout.widget.ConstraintLayout>

                <!--Constraint 2-->
                <androidx.constraintlayout.widget.ConstraintLayout
                    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="match_parent"
                    android:id="@+id/constraintLayout">

                    <ImageView
                        android:id="@+id/ivA"
                        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/tvB"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="10dp"
                        app:layout_constraintStart_toEndOf="@+id/ivA"
                        app:layout_constraintTop_toTopOf="@+id/ivA"
                        style="@android:style/TextAppearance.Medium"/>

                    <TextView
                        android:id="@+id/tvC"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="10dp"
                        android:layout_marginBottom="10dp"
                        android:layout_marginTop="10dp"
                        app:layout_constraintBottom_toTopOf="@+id/tvD"
                        app:layout_constraintStart_toEndOf="@+id/ivA"
                        app:layout_constraintTop_toBottomOf="@+id/tvB"
                        style="@android:style/TextAppearance.Medium"/>

                    <ImageView
                        android:id="@+id/ivD"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="20dp"
                        app:layout_constraintEnd_toEndOf="@+id/ivA"
                        app:layout_constraintStart_toStartOf="@+id/ivA"
                        app:layout_constraintTop_toBottomOf="@+id/ivA"
                        app:layout_constraintTop_toTopOf="@+id/tvD"
                        app:srcCompat="@mipmap/ic_launcher_round" />

                    <TextView
                        android:id="@+id/tvD"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="10dp"
                        app:layout_constraintStart_toEndOf="@+id/ivD"

                        app:layout_constraintTop_toBottomOf="@+id/tvC"
                        style="@android:style/TextAppearance.Medium"/>
                </androidx.constraintlayout.widget.ConstraintLayout>
            </LinearLayout>
</androidx.cardview.widget.CardView>

Suraj Vaishnav的建议

enter image description here

1 个答案:

答案 0 :(得分:1)

由于在右侧(或末端)没有任何约束,请将此app:layout_constraintEnd_toEndOf="parent"添加到tvA,tvB等。

更新

应用两个约束:开始和结束并设置宽度0dp。这是tvB的工作属性:

<TextView
    android:id="@+id/tvB"
    app:layout_constraintStart_toEndOf="@+id/ivA"
    android:layout_width="0dp"
    app:layout_constraintEnd_toEndOf="parent"
...

我想您可以为tvC和tvD做相同的事情,如果您遇到任何问题,请告诉我。

相关问题