我有这个聊天应用程序,但因为它只有一个textview
被追加我不知道如何通过设计让它更具代表性我从互联网上学到了代码,这是最简单的方法我能做到,你能帮助我吗?
这是代码。
private String chat_msg,chat_user_name;
private void append_chat_conversation(DataSnapshot dataSnapshot) {
Iterator i = dataSnapshot.getChildren().iterator();
while (i.hasNext()){
chat_msg = (String) ((DataSnapshot)i.next()).getValue();
chat_user_name = (String) ((DataSnapshot)i.next()).getValue();
chat_conversation.setGravity(Gravity.BOTTOM);
if(chat_user_name.contains(pref.getString("username","").toString())){
chat_conversation.append(chat_user_name + " : " + chat_msg + " \n");
}else {
chat_conversation.append(chat_user_name + " : " + chat_msg + " \n");
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/btn_send"
android:src="@drawable/send"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/msg_input"
android:layout_toLeftOf="@+id/btn_send"
android:layout_toStartOf="@+id/btn_send" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textSize="15sp"
android:textColor="#000000"
android:scrollbars = "vertical"
android:id="@+id/textView"
android:layout_above="@+id/msg_input" />
</RelativeLayout>
谢谢先生!!!
答案 0 :(得分:0)
尝试为每条消息动态添加TextView。
要动态添加TextView,请检查此示例代码
//Get the parent layout
RealtiveLayout relativeLayout = (RealtiveLayout)findViewById(R.id.parent_layout);
//New TextView
TextView textView = new TextView(this);
//Layout Params
textView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
textView.setText("Your text");
//Add to the parent
relativelayout.addView(textView1);