多行的TextView是错误的与其他控件对齐

时间:2019-01-29 12:27:37

标签: android textview android-constraintlayout

我想要这种布局:

enter image description here

但是我得到了这个:

enter image description here

如您所见,TextView对齐错误,左侧不可见。我试图改变填补处理,重力,宽度,更换容器(变化为LinearLayout),但没有任何帮助。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/green_chat_layout_bg"
    android:paddingLeft="10dp"
    android:paddingTop="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    >

    <TextView
        android:id="@+id/user_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/night_rider"
        android:textSize="@dimen/normal_text_size"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/user_date"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="You Might Also Like. How to generate javadoc comments in Android Studio"
        />

    <TextView
        android:id="@+id/user_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="1dp"
        android:layout_marginLeft="1dp"
        android:alpha="0.4"
        android:textColor="@color/black"
        android:textSize="@dimen/small_text_size"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/seen"
        app:layout_constraintStart_toEndOf="@id/user_text"
        tools:text="16:25"
        />

    <ImageView
        android:id="@+id/seen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_marginLeft="5dp"
        android:contentDescription="@null"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/user_date"
        tools:srcCompat="@drawable/ic_chat_message_seen"
        />

</android.support.constraint.ConstraintLayout>

短文本右对齐。

2 个答案:

答案 0 :(得分:2)

您需要将父布局的宽度设置为match_parent,然后指定textview match_constraint。这样您的布局就可以了。像这样更改布局:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content"
    android:background="@drawable/green_chat_layout_bg"
    android:paddingLeft="10dp"
    android:paddingTop="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="10dp">

    <TextView
        android:id="@+id/user_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:padding="5dp"
        android:textColor="@color/night_rider"
        android:textSize="@dimen/normal_text_size"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/user_date"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="You Might Also Like. How to generate javadoc comments in Android Studio" />

    <TextView
        android:id="@+id/user_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:alpha="0.4"
        android:textColor="@color/black_color"
        android:textSize="12sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/seen"
        tools:text="16:25" />

    <ImageView
        android:id="@+id/seen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
    android:layout_marginLeft="5dp"
    android:contentDescription="@null"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        tools:srcCompat="@drawable/ic_group" />


</android.support.constraint.ConstraintLayout>

您的布局将如下所示: enter image description here

答案 1 :(得分:1)

当您使用"Location::: LOC"时,TextView会意识到文本太长,因此它分为两行并占据了整个屏幕空间。由于还有其他受约束的视图,因此ConstraintLayout决定将TextView稍微向左推。

wrap_content的宽度更改为TextView,并将布局宽度设置为0dp

  

使用0dp,相当于“ MATCH_CONSTRAINT”

     

将尺寸设置为MATCH_CONSTRAINT时,默认行为是使生成的尺寸占用所有可用空间。

https://developer.android.com/reference/android/support/constraint/ConstraintLayout