Recyclerview 列表项布局重力问题

Recyclerview listitem layout gravity issue

这是聊天机器人。有些消息左对齐,有些消息右对齐。左对齐消息工作正常,但右对​​齐消息也会左对齐。这是必需的列表项。 layout_gravity 在右边。但是当添加到回收视图时,列表项会移动到左侧,如另一张照片所示。我不知道罪魁祸首是什么。是 constraint_layout 还是 linearlayoutmanager 还是别的东西。

`<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:padding="@dimen/paddingBig"
        android:background="@drawable/balloon_outgoing_normal"
        android:layout_gravity="right"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_margin="8dp">

    <TextView
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:id="@+id/text"
            android:layout_width="0dp"
            android:paddingRight="@dimen/paddingSmall"
            android:layout_height="wrap_content"
            tools:text="Hello hello Hellomnhbhvbhbvhbhvbhvbhbvhbvhbvhbvhbhvbhvbhvbhvbhbvhbvhbv hello Hello hello Hello hello Hello hello"/>

    <TextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintRight_toLeftOf="@id/status"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@id/text"
            tools:text="21/04/2016, 2:30 P.M."/>

    <ImageView
            android:id="@+id/status"
            tools:visibility="visible"
            android:src="@drawable/baseline_schedule_black_18"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="@id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" tools:ignore="ContentDescription"/>
</androidx.constraintlayout.widget.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:id="@+id/chat_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true">

    <ImageView
            android:id="@+id/sendMessage"
            android:layout_width="52dp"
            android:layout_height="52dp"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:background="?android:attr/selectableItemBackground"
            android:padding="@dimen/paddingMedium"
            android:src="@drawable/send_disable" tools:ignore="ContentDescription"/>

    <EditText
            android:id="@+id/input"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="@string/type_message"
            app:layout_constraintRight_toLeftOf="@id/sendMessage"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:inputType="textCapSentences|textMultiLine"
            android:maxLines="2"
            android:padding="@dimen/paddingMedium" tools:ignore="Autofill"/>

    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:paddingBottom="20dp"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toTopOf="@id/input"
            tools:listitem="@layout/me_message"
            app:layout_constraintTop_toTopOf="parent">
    </androidx.recyclerview.widget.RecyclerView>

    <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            tools:visibility="visible"
            android:visibility="gone"/>

</androidx.constraintlayout.widget.ConstraintLayout>`

我尝试了很多东西。实际上,早些时候我使用了相同的 relativelayout 并使用了 alignParentBottom ,但我很惊讶为什么 constraintlayout 不起作用。

将 RecyclerView 的 layout_width 设置为 match_parent 并为其设置正确的重力。那应该有效。

试试这个

     <TextView
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:paddingRight="@dimen/paddingSmall"
        android:layout_height="wrap_content"
        tools:text="Hello hello Hellomnhbhvbhbvhbhvbhvbhbvhbvhbvhbvhbhvbhvbhvbhvbhbvhbvhbv hello Hello hello Hello hello Hello hello"/>

您需要使用嵌套约束布局来实现该视图。同样在您的 recyclerview 中将宽度设置为 match_parent

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

    <androidx.constraintlayout.widget.ConstraintLayout
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:padding="@dimen/paddingBig"
        android:background="@drawable/balloon_outgoing_normal"
        android:layout_margin="8dp">

        <TextView
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:id="@+id/text"
            android:layout_width="0dp"
            android:paddingRight="@dimen/paddingSmall"
            android:layout_height="wrap_content"
            tools:text="Hello hello Hellomnhbhvbhbvhbhvbhvbhbvhbvhbvhbvhbhvbhvbhvbhvbhbvhbvhbv hello Hello hello Hello hello Hello hello"/>

        <TextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintRight_toLeftOf="@id/status"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@id/text"
            tools:text="21/04/2016, 2:30 P.M."/>

        <ImageView
            android:id="@+id/status"
            tools:visibility="visible"
            android:src="@drawable/baseline_schedule_black_18"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="@id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" tools:ignore="ContentDescription"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>