Textview 内容在约束布局中从右侧截断

Textview content cuts off from right in constraint layout

我有一个卡片视图布局,左侧有一个 imageView,右侧有两个垂直对齐的文本视图。现在我遇到问题,因为文本在文本视图之一的屏幕右侧剪切。下面是我正在使用的 XML 布局。

<?xml version="1.0" encoding="utf-8"?>
<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:padding="@dimen/spacing_large"
    tools:context=".ui.profile.GroupProfileActivity"
    android:background="@android:color/background_light">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/groupCardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="@dimen/spacing_normal"
        app:cardElevation="2dp"
        android:elevation="2dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/layoutGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/groupImageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="@+id/layoutGroup"
                app:layout_constraintBottom_toBottomOf="@+id/layoutGroup"
                app:layout_constraintStart_toStartOf="@+id/layoutGroup"
                app:srcCompat="@drawable/ic_group_black_24dp"
                android:paddingLeft="@dimen/spacing_normal"/>

            <!-- Donor type layout -->
            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/layoutGroupView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="10dp"
                app:layout_constraintLeft_toRightOf="@+id/groupImageView"
                app:layout_constraintTop_toTopOf="@+id/layoutGroup"
                android:layout_marginLeft="@dimen/spacing_normal">

                <TextView
                    android:id="@+id/tvGroupName"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="Demo"
                    android:textSize="@dimen/font_normal"
                    app:layout_constraintTop_toTopOf="@+id/layoutGroupView"
                    app:layout_constraintStart_toStartOf="@+id/layoutGroupView"/>

                <TextView
                    android:id="@+id/tvGroupNameLabel"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="@string/group_label"
                    android:textSize="@dimen/font_normal"
                    app:layout_constraintTop_toBottomOf="@+id/tvGroupName"
                    app:layout_constraintStart_toStartOf="@+id/layoutGroupView"/>
            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>

    </com.google.android.material.card.MaterialCardView>

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/mobileCardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/groupCardView"
        android:layout_marginTop="@dimen/spacing_large"
        app:cardElevation="2dp"
        android:elevation="2dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/layoutPhone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/phoneImageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="@+id/layoutPhone"
                app:layout_constraintBottom_toBottomOf="@+id/layoutPhone"
                app:layout_constraintStart_toStartOf="@+id/layoutPhone"
                app:srcCompat="@drawable/ic_phone_black_24dp"
                android:paddingLeft="@dimen/spacing_normal"/>

            <!-- Donor type layout -->
            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/layoutPhoneView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="10dp"
                app:layout_constraintLeft_toRightOf="@+id/phoneImageView"
                app:layout_constraintTop_toTopOf="@+id/layoutPhone"
                android:layout_marginLeft="@dimen/spacing_normal">

                <TextView
                    android:id="@+id/tvPhone"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    tools:text="Demo"
                    android:textSize="@dimen/font_normal"
                    app:layout_constraintTop_toTopOf="@+id/layoutPhoneView"
                    app:layout_constraintStart_toStartOf="@+id/layoutPhoneView"/>

                <TextView
                    android:id="@+id/tvPhoneLabel"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="@string/phone_label"
                    android:textSize="@dimen/font_normal"
                    app:layout_constraintTop_toBottomOf="@+id/tvPhone"
                    app:layout_constraintStart_toStartOf="@+id/layoutPhoneView"/>
            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </com.google.android.material.card.MaterialCardView>

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/addressCardView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/mobileCardView"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginTop="@dimen/spacing_large"
        app:cardElevation="2dp"
        android:elevation="2dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/layoutAddress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/addressImageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="@+id/layoutAddress"
                app:layout_constraintBottom_toBottomOf="@+id/layoutAddress"
                app:layout_constraintStart_toStartOf="@+id/layoutAddress"
                app:srcCompat="@drawable/ic_home_black_24dp"
                android:paddingLeft="@dimen/spacing_normal"/>

            <!-- Donor type layout -->
            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/layoutHomeView"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="10dp"
                app:layout_constraintLeft_toRightOf="@+id/addressImageView"
                app:layout_constraintTop_toTopOf="@+id/layoutAddress"
                android:layout_marginLeft="@dimen/spacing_normal">

                <TextView
                    android:id="@+id/tvAddress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    tools:text="Demo"
                    android:textSize="@dimen/font_normal"
                    app:layout_constraintTop_toTopOf="@+id/layoutHomeView"
                    app:layout_constraintStart_toStartOf="@+id/layoutHomeView"/>

                <TextView
                    android:id="@+id/tvAddressLabel"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="@string/donor_address_hint"
                    android:textSize="@dimen/font_normal"
                    tools:text="sfsfgagaggsggddeggdgagahhahahahahhahahgsgjaaasshhs"
                    android:paddingRight="@dimen/spacing_xxhuge"
                    app:layout_constraintEnd_toEndOf="@+id/layoutHomeView"
                    app:layout_constraintTop_toBottomOf="@+id/tvAddress" />
            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>

并且还添加了屏幕截图。

请查看这个问题,因为我一直在努力解决这个问题。

您已将宽度设置为 0dp。而是使用 wrap_content:

    android:id="@+id/tvAddressLabel"
                android:layout_width="wrap_content" //here
                android:layout_height="wrap_content"

添加app:layout_constraintEnd_toEndOf="parent"

布局HomeView和addressCardView

Your last material card constraint and width is wrong. Also, You can remove more layout hierarchy like in Donor type layout you can remove unnecessary constraintlayout.

    <?xml version="1.0" encoding="utf-8"?>
<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:background="@android:color/background_light"
    android:padding="@dimen/spacing_large">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/groupCardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/spacing_normal"
        android:elevation="2dp"
        app:cardElevation="2dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/layoutGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/groupImageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/spacing_normal"
                app:layout_constraintBottom_toBottomOf="@+id/layoutGroup"
                app:layout_constraintStart_toStartOf="@+id/layoutGroup"
                app:layout_constraintTop_toTopOf="@+id/layoutGroup"
                app:srcCompat="@drawable/ic_launcher_background" />

            <!-- Donor type layout -->
            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/layoutGroupView"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/spacing_normal"
                android:orientation="horizontal"
                android:padding="10dp"
                app:layout_constraintStart_toEndOf="@+id/groupImageView"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintLeft_toRightOf="@+id/groupImageView"
                app:layout_constraintTop_toTopOf="@+id/layoutGroup">

                <TextView
                    android:id="@+id/tvGroupName"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="Demo"
                    android:textSize="@dimen/font_normal"
                    app:layout_constraintEnd_toEndOf="@+id/layoutGroupView"
                    app:layout_constraintStart_toStartOf="@+id/layoutGroupView"
                    app:layout_constraintTop_toTopOf="@+id/layoutGroupView" />

                <TextView
                    android:id="@+id/tvGroupNameLabel"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="group_label"
                    android:textSize="@dimen/font_normal"
                    app:layout_constraintEnd_toEndOf="@+id/layoutGroupView"
                    app:layout_constraintStart_toStartOf="@+id/layoutGroupView"
                    app:layout_constraintTop_toBottomOf="@+id/tvGroupName" />
            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>

    </com.google.android.material.card.MaterialCardView>

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/mobileCardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/spacing_large"
        android:elevation="2dp"
        app:cardElevation="2dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/groupCardView">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/layoutPhone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/phoneImageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/spacing_normal"
                app:layout_constraintBottom_toBottomOf="@+id/layoutPhone"
                app:layout_constraintStart_toStartOf="@+id/layoutPhone"
                app:layout_constraintTop_toTopOf="@+id/layoutPhone"
                app:srcCompat="@drawable/ic_launcher_background" />

            <!-- Donor type layout -->
            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/layoutPhoneView"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/spacing_normal"
                android:orientation="horizontal"
                android:padding="10dp"
                app:layout_constraintEnd_toEndOf="@+id/layoutPhone"
                app:layout_constraintStart_toEndOf="@+id/phoneImageView"
                app:layout_constraintTop_toTopOf="@+id/layoutPhone">

                <TextView
                    android:id="@+id/tvPhone"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:textSize="@dimen/font_normal"
                    app:layout_constraintEnd_toEndOf="@+id/layoutPhoneView"
                    app:layout_constraintStart_toStartOf="@+id/layoutPhoneView"
                    app:layout_constraintTop_toTopOf="@+id/layoutPhoneView"
                    tools:text="Demo" />

                <TextView
                    android:id="@+id/tvPhoneLabel"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="phone_label"
                    android:textSize="@dimen/font_normal"
                    app:layout_constraintEnd_toEndOf="@+id/layoutPhoneView"
                    app:layout_constraintStart_toStartOf="@+id/layoutPhoneView"
                    app:layout_constraintTop_toBottomOf="@+id/tvPhone" />
            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </com.google.android.material.card.MaterialCardView>

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/addressCardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/spacing_large"
        android:elevation="2dp"
        app:cardElevation="2dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mobileCardView">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/layoutAddress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/addressImageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/spacing_normal"
                app:layout_constraintBottom_toBottomOf="@+id/layoutAddress"
                app:layout_constraintStart_toStartOf="@+id/layoutAddress"
                app:layout_constraintTop_toTopOf="@+id/layoutAddress"
                app:srcCompat="@drawable/ic_launcher_background" />

            <!-- Donor type layout -->
            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/layoutHomeView"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/spacing_normal"
                android:orientation="horizontal"
                android:padding="10dp"
                app:layout_constraintEnd_toEndOf="@+id/layoutAddress"
                app:layout_constraintStart_toEndOf="@+id/addressImageView"
                app:layout_constraintTop_toTopOf="@+id/layoutAddress">

                <TextView
                    android:id="@+id/tvAddress"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:textSize="@dimen/font_normal"
                    app:layout_constraintEnd_toEndOf="@+id/layoutHomeView"
                    app:layout_constraintStart_toStartOf="@+id/layoutHomeView"
                    app:layout_constraintTop_toTopOf="@+id/layoutHomeView"
                    tools:text="Demo" />

                <TextView
                    android:id="@+id/tvAddressLabel"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:paddingRight="@dimen/spacing_xxhuge"
                    android:text="donor_address_hint"
                    app:layout_constraintEnd_toEndOf="@+id/layoutHomeView"
                    app:layout_constraintTop_toBottomOf="@+id/tvAddress"
                    tools:text="sfsfgagaggsggddeggdgagahhahahahahhahahgsgjaaasshhs" />
            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>

首先:您的文字没有空格,Android Studio 的东西是一个词。 所以在现实世界中我们没有这么长的词。 只需添加空格。
第二:您为一个单元格创建了两个容器。例如,您创建了 1 个包含 imageView 的容器和其他带有 textView 的容器。 因此,您只能使用 1 个包含所有视图的容器。因为你的 'text container' 不显示了(添加到图片上)。