2017-02-10 56 views
-4

我有這個聊天應用程序,但因爲它只有一個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

嘗試爲每條消息動態添加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); 
+0

如何添加現有的一個先生低於新的看法?因爲它只是重疊 –

+0

使用LinearLayout而不是RelativeLayout並將方向設置爲垂直。 –

+0

當我將其更改爲LinearLayout先生時,文本不再顯示。 –