我正在尝试编写类似于WhatsApp的聊天屏幕,我的意思是,收到的消息与左边对齐,发送的消息对齐到右边。
这是代码的一部分,我将使用消息填充messageContainer。
List<Message> chatList = Database_IO.parseChat(user1, user2);
for (Message m : chatList) {
LayoutInflater li = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View message = li.inflate(R.layout.msgbubble, null);
TextView messageText = message.getRootView().findViewById(R.id.messageText);
messageText.setText(m.messageText);
RelativeLayout messageContainer = findViewById(R.id.messageContainer);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)messageContainer.getLayoutParams();
if (userID == m.sender.id) {
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
}
if (userID == m.recipient.id) {
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
}
messageContainer.setLayoutParams(params);
messageContainer.addView(message);
}
msgbubble.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/messageText"
android:padding="8dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hey there, it's been a while, how are you? :)"
android:background="@color/colorAccent"
android:textColor="@android:color/black"/>
</RelativeLayout>
activity_chat.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.messenger.ChatActivity">
<RelativeLayout
android:id="@+id/messageContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/msgPanel"
android:orientation="vertical">
</RelativeLayout>
<LinearLayout
android:id="@+id/msgPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Gönder"/>
</LinearLayout>
</RelativeLayout>
我该如何解决这个问题?
答案 0 :(得分:3)
您可以使用LinearLayout。像这样的东西
List<Message> chatList = Database_IO.parseChat(user1, user2);
for (Message m : chatList) {
LayoutInflater li = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View message = li.inflate(R.layout.msgbubble, null);
TextView messageText = message.getRootView().findViewById(R.id.messageText);
messageText.setText(m.messageText);
LinearLayout messageContainer = findViewById(R.id.messageContainer);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.FILL_PARENT);
params.weight = 1.0f;
if (userID == m.sender.id) {
params.gravity = Gravity.RIGHT;
messageContainer.setGravity(Gravity.RIGHT);
}
if (userID == m.recipient.id) {
params.gravity = Gravity.LEFT;
messageContainer.setGravity(Gravity.LEFT);
}
messageContainer.setLayoutParams(params);
messageContainer.addView(message);
}
答案 1 :(得分:1)
终于弄明白我的问题是什么。我的messagebubble的宽度是“match_parent”,所以我看不到对齐。
我把它设为wrap_content并删除了LayoutParams,但它们仍无法正常工作。
这是我的最终代码
for (Message m : chatList) {
LayoutInflater li = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View message = li.inflate(R.layout.msgbubble, null);
TextView messageText = message.getRootView().findViewById(R.id.messageText);
messageText.setText(m.messageText);
LinearLayout msgBubble = message.findViewById(R.id.bubbleLayout);
if (userID == m.sender.id) {
msgBubble.setGravity(Gravity.RIGHT);
messageText.setBackgroundColor(getResources().getColor(R.color.inBlue));
}
if (userID == m.recipient.id) {
msgBubble.setGravity(Gravity.LEFT);
messageText.setBackgroundColor(getResources().getColor(R.color.outBlue));
}
messageContainer.addView(message);
}
感谢您的帮助,特别是Tej的精彩和鼓舞人心的答案。还要感谢其他人指出可能的性能杀手实现!