应用程序没有采用我设置的布局 - 为什么?
App not taking the layout that I set - why?
我有一个 activity(聊天屏幕)和一个消息布局文件。如果用户正在发送消息,那么它们应该出现在屏幕的右侧,消息气泡应该是绿色而不是橙色(橙色是您收到的消息的颜色)。 ChatScreenActivity
对齐和颜色有时很好,但它们可能会丢失它们的位置。此外,当您退出 ChatScreenActivity
并返回时,一些本应位于右侧的消息将左对齐(例如消息项的默认 xml - 就像应用程序建议的那样您发送的消息来自接收者)。我无法找出为什么会这样。这是我的代码:
在我的 MessagesAdapter
class
@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:correct alignment but second message should be orange and show picture
图 2:Both messages aligned left and app says I sent the message that I received
在其他情况下,您需要明确将颜色设置为橙色:
messageViewHolder.messageText.setBackgroundResource(R.drawable.rounded_rectangle_orange);
因为颜色被记忆为绿色。在第二个示例中,您有一个橙色圆圈,因为它是第一个。
对于第二个问题,您可以检查使用 getRules() 添加的规则,看看是否添加了多个 ALIGN 规则。在这种情况下,您可以使用 removeRule() 删除不需要的那些,或者保留两个布局参数,一个用于左侧,一个用于右侧。
我有一个 activity(聊天屏幕)和一个消息布局文件。如果用户正在发送消息,那么它们应该出现在屏幕的右侧,消息气泡应该是绿色而不是橙色(橙色是您收到的消息的颜色)。 ChatScreenActivity
对齐和颜色有时很好,但它们可能会丢失它们的位置。此外,当您退出 ChatScreenActivity
并返回时,一些本应位于右侧的消息将左对齐(例如消息项的默认 xml - 就像应用程序建议的那样您发送的消息来自接收者)。我无法找出为什么会这样。这是我的代码:
在我的 MessagesAdapter
class
@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:correct alignment but second message should be orange and show picture
图 2:Both messages aligned left and app says I sent the message that I received
在其他情况下,您需要明确将颜色设置为橙色:
messageViewHolder.messageText.setBackgroundResource(R.drawable.rounded_rectangle_orange);
因为颜色被记忆为绿色。在第二个示例中,您有一个橙色圆圈,因为它是第一个。
对于第二个问题,您可以检查使用 getRules() 添加的规则,看看是否添加了多个 ALIGN 规则。在这种情况下,您可以使用 removeRule() 删除不需要的那些,或者保留两个布局参数,一个用于左侧,一个用于右侧。