我在我的机器人上创建了一个客户端程序,当我收到长消息时,它并没有显示整个消息。我曾尝试将android:orientation="horizontal"
添加到XML文件中,但这并没有解决任何问题。
package com.example.marcus.chatclient1;
import android.app.Activity;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import static android.R.layout.list_content;
public class chat extends Activity {
Handler hanGET;
String string = "test";
String name;
EditText msgBox;
Button sButton;
ListView lv;
TextView errorT;
ObjectOutputStream o;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
final Button join = (Button) findViewById(R.id.joinButton);
final EditText nameT = (EditText) findViewById(R.id.editTextName);
msgBox = (EditText)findViewById(R.id.msgField);
sButton = (Button)findViewById(R.id.sendButton);
errorT = (TextView) findViewById(R.id.errorText);
lv = (ListView)findViewById(R.id.ChatList);
msgBox.setVisibility(View.INVISIBLE);
sButton.setVisibility(View.INVISIBLE);
lv.setVisibility(View.INVISIBLE);
join.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
name = nameT.getText().toString();
join.setVisibility(View.INVISIBLE);
nameT.setVisibility(View.INVISIBLE);
lv.setVisibility(View.VISIBLE);
msgBox.setVisibility(View.VISIBLE);
sButton.setVisibility(View.VISIBLE);
new Thread(new ClientThread()).start();
final ArrayList<String> ar = new ArrayList<String>();
final ArrayAdapter ad = new ArrayAdapter(getApplicationContext(),android.R.layout.simple_gallery_item, ar);
lv.setBackgroundColor(Color.BLACK);
lv.setAdapter(ad);
hanGET = new Handler(){
public void handleMessage(Message message) {
ar.add(string);
ad.notifyDataSetChanged();
}
};
}
});
}
class ClientThread implements Runnable {
public void run() {
Message message;
Socket s;
try {
s = new Socket("192.168.0.15", 55555);
o = new ObjectOutputStream(s.getOutputStream());
o.writeObject(name);
ObjectInputStream in = new ObjectInputStream(s.getInputStream());
sButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
String m = msgBox.getText().toString();
o.writeObject(m);
} catch (IOException e) {
e.printStackTrace();
}
}
});
while(true) {
try {
string = in.readObject().toString();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
message = Message.obtain();
hanGET.sendMessage(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.marcus.chatclient1.chat">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Join"
android:id="@+id/joinButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="@+id/editTextName"
android:layout_marginBottom="31dp"
android:layout_above="@+id/joinButton"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/errorText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ChatList"
android:layout_alignParentTop="true"
android:clickable="false"
android:layout_alignBottom="@+id/joinButton"
android:choiceMode="none"
tools:listitem="@android:layout/simple_gallery_item"
tools:listfooter="@layout/activity_chat"
android:visibility="visible"
android:layout_centerHorizontal="true"
android:headerDividersEnabled="false" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/msgField"
android:layout_below="@+id/ChatList"
android:layout_toRightOf="@+id/errorText"
android:layout_toEndOf="@+id/errorText"
android:visibility="visible" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="@+id/sendButton"
android:layout_alignBottom="@+id/msgField"
android:layout_toRightOf="@+id/joinButton"
android:layout_toEndOf="@+id/joinButton"
android:visibility="visible" />
</RelativeLayout>
答案 0 :(得分:0)
ListView必须将layout_height
属性设置为wrap_content
。 ListView内容高度是动态的,因为您不总是具有相同数量的单元格。因此,您需要匹配父级大小或具有固定的高度。