Recyclerview Listitem布局重力问题

时间:2019-07-14 05:21:10

标签: android android-layout android-recyclerview android-constraintlayout linearlayoutmanager

这是聊天机器人。有些消息左对齐,有些则右对齐。左对齐的消息可以正常工作,但是右对齐的消息也可以左对齐。这是必需的列表项。 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>`

<code>enter image description here</code>

`<?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无法正常工作。

3 个答案:

答案 0 :(得分:1)

您需要使用嵌套的constraintlayout来实现该视图。同样在您的回收站视图中,将宽度设置为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>

enter image description here

答案 1 :(得分:0)

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

答案 2 :(得分:0)

尝试

     <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"/>