Gravity not set properlywith LayoutParams in onBindViewHolder

Gravity not set properlywith LayoutParams in onBindViewHolder

我有一个聊天 activity,它根据发件人的用户名排列消息。 onBindViewHolder 中概述的所有行为都正常工作,除了容器的重力(所有消息都对齐结束,而它们应该分别为合作伙伴和登录用户对齐开始和结束)。我在执行 LayoutParams 时做错了什么?

这是我的代码:

@Override
public void onBindViewHolder(@NonNull DMViewHolder holder, int position) {

    // This method fills fields with data for each list item


    Message message = messageList.get(position);

    String signedInUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();

    boolean isSignedInUser = message.getAuthorUID().equals(signedInUserID);

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a");

    holder.message.setText(message.getMessage());
    holder.date.setText(simpleDateFormat.format(message.getTimestamp()));


    if (isSignedInUser) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_END);
        holder.singleDMContainer.setLayoutParams(params);

        holder.textContainer.setBackgroundResource(R.drawable.user_message_text_background);
    } else {
        RelativeLayout.LayoutParams params2= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params2.addRule(RelativeLayout.ALIGN_START);
        holder.singleDMContainer.setLayoutParams(params2);

        holder.textContainer.setBackgroundResource(R.drawable.partner_message_text_background);
    }


}

和消息的 xml 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/singleDMContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:orientation="horizontal">


    <RelativeLayout
        android:id="@+id/text_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="5dp"
        android:background="@drawable/user_message_text_background">

        <TextView
            android:id="@+id/dm_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/nunito_extralight"
            android:gravity="start"
            android:padding="5dp"
            android:text="@string/sample_message"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/dm_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_toEndOf="@+id/dm_message"
            android:fontFamily="@font/nunito_extralight"
            android:padding="5dp"
            android:text="@string/sample_time"
            android:textColor="@android:color/black"
            android:textSize="12sp"
            android:textStyle="normal" />

    </RelativeLayout>


</RelativeLayout>

将规则应用于子视图,而不是容器:替换

holder.singleDMContainer.setLayoutParams(params2);

holder.textContainer.setLayoutParams(params2);

更新:

还使用 RelativeLayout.ALIGN_PARENT_START 代替 RelativeLayout.ALIGN_START,并使用 RelativeLayout.ALIGN_PARENT_END 代替 RelativeLayout.ALIGN_END