我正在构建一个非常简单的在线聊天室应用程序。我现在所取得的成就如下:
Robert:blah..
Tom: yes blah..
David(You): That sounds cool!
Lily: Do you know blah...
Robert:blah..
Robert:blah..
David(You): Wow! Blah...
我面临的新功能/问题是我希望当前用户的谈话显示在右侧。如下所示(这是大卫在他的屏幕上看到的):
Robert:blah..
Tom: yes blah..
That sounds cool! : David(You)
Lily: Do you know blah...
Robert:blah..
Robert:blah..
Wow! Blah... : David(You)
上面的每一行都是一个 TextView,每当有新消息时我都会动态创建 TextView,然后将它们添加到LinearLayout
包含这些 TextView 的内容中。为了让大卫(当前用户)的谈话在右边,我尝试设置 align rightLinearLayout
并将RelativeLayout
. 但后来我意识到,一旦我使用RelativeLayout
,所有的谈话都在同一条线上相互重叠,因为我没有设置它们的高度。任何人都可以阐明如何实现这一点吗?我的代码如下:
...
//new messages are stored in lines[]
for (int i = 0; i < lines.length; i++) {
TextView newLine = new TextView(getBaseContext());
newLine.setText(lines[i]);
// check if the speaker of this line is user himself
if (speakerName.equals(userName)) {
//change the layout of this textView to make it align right..
}
myLinearView.addView(newLine);//add to linearView that contains these talks
}