首页 > 解决方案 > 应用程序没有采用我设置的布局 - 为什么?

问题描述

我的消息有一个活动(聊天屏幕)和一个布局文件。如果用户正在发送消息,那么它们应该出现在屏幕的右侧,并且消息气泡应该是绿色而不是橙色(橙色是您收到的消息的颜色)。有时对齐和颜色很好,ChatScreenActivity但它们可能会失去位置。此外,当您退出ChatScreenActivity并返回它时,一些应该在右侧的消息将向左对齐(例如在消息项的默认 xml 上 - 就像应用程序建议您发送的消息来自接收者)。我不知道为什么会这样。这是我的代码:在我的MessagesAdapter课堂上

    @Override
    protected void onBindViewHolder(@NonNull MessagesAdapter.MessageViewHolder messageViewHolder, int i, @NonNull Messages messages) {
        if (messages.getFrom().equals(currentUser)) {
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) contentLayout.getLayoutParams();
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            contentLayout.setLayoutParams(lp);
            messageViewHolder.messageText.setBackgroundResource(R.drawable.rounded_rectangle_green);
            messageViewHolder.profilePicture.setVisibility(View.GONE);
            messageViewHolder.txtUsername.setVisibility(View.GONE);
        } else {
             messageViewHolder.messageText.setBackgroundResource(R.drawable.rounded_rectangle_orange);
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) contentLayout.getLayoutParams();
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            contentLayout.setLayoutParams(lp);
            messageViewHolder.profilePicture.setVisibility(View.VISIBLE);
            messageViewHolder.txtUsername.setVisibility(View.VISIBLE);
        }
        messageViewHolder.messageText.setText(messages.getMessages());
        try {
            Date date = messages.getTime().toDate();
            String hours = String.valueOf(date.getHours());
            String mins = String.valueOf(date.getMinutes());
            if (mins.equals("0")) {
                mins = "00";
            }
            String time = hours + ":" + mins;
            messageViewHolder.timeText.setText(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这是我的消息项:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/messConstraintLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="8dp">
    <RelativeLayout
        android:id="@+id/mainRelativeContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/text_message_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/text_message_body"
            android:layout_marginLeft="8dp"
            android:layout_toRightOf="@id/image_message_profile"
            android:text="John Doe"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/text_message_body"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/image_message_profile"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="4dp"
            android:background="@drawable/rounded_rectangle_orange"
            android:maxWidth="240dp"
            android:padding="8dp"
            android:text="hi man, how are you?"
            android:textColor="#ffffff" />

        <TextView
            android:id="@+id/text_message_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text_message_body"
            android:layout_alignRight="@id/text_message_body"
            android:layout_marginLeft="4dp"
            android:text="11:40"
            android:textSize="10sp" />

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/image_message_profile"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_marginLeft="8dp"
            android:src="@drawable/ic_account_circle_white_24dp" />
    </RelativeLayout>

</RelativeLayout>

这就是程序正在做的事情:图 1:正确对齐,但第二条消息应该是橙色并显示图片

图 2:两条消息都左对齐,应用程序说我发送了我收到的消息

标签: javaandroidandroid-recyclerviewandroid-relativelayout

解决方案


在其他情况下,您需要将颜色显式设置为橙色:

 messageViewHolder.messageText.setBackgroundResource(R.drawable.rounded_rectangle_orange);

因为颜色被记忆为绿色。在第二个示例中,您有一个橙色圆圈,因为它是第一个。

对于第二个问题,您可以检查您使用 getRules() 添加的规则,看看您是否添加了多个 ALIGN 规则。在这种情况下,您可以使用 removeRule() 删除不需要的参数,或者维护两个 layoutParams,一个用于左侧,一个用于右侧。


推荐阅读