我是一个相对业余的编码人员,一直在逐步学习android编程。我对现在流行的围绕消息交换的消息的气泡感到怀疑。
我一直试图建立一个聊天界面,但经过反复试验,提出了类似这样的内容。
聊天窗口的代码如下:
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="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/ranklabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ranklabel" />
<TextView
android:id="@+id/rank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/ranklabel" />
<TextView
android:id="@+id/countrylabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ranklabel"
android:text="@string/countrylabel" />
<TextView
android:id="@+id/country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/rank"
android:layout_toRightOf="@+id/countrylabel" />
<TextView
android:id="@+id/populationlabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/countrylabel"
android:text="@string/populationlabel" />
<TextView
android:id="@+id/population"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/country"
android:layout_toRightOf="@+id/populationlabel" />
<EditText
android:id="@+id/reply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/replyMsg"
android:ems="10"
android:hint="Reply to the user here..."
android:inputType="textMultiLine"
android:maxLength="160" />
<Button
android:id="@+id/replyMsg"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/roundbutton"
android:text="GO"
android:textColor="#ffffff"
/>
<TextView
android:id="@+id/msgView"
android:layout_width="400dp"
android:layout_height="200dp"
android:layout_above="@+id/replyMsg"
android:layout_alignParentLeft="true"
android:layout_below="@+id/population"
android:maxLines = "1999"
android:scrollbars = "vertical" />
JAVA
final EditText reply = (EditText) findViewById(R.id.reply);
final TextView msgView = (TextView) findViewById(R.id.msgView);
Button replyMsg = (Button) findViewById(R.id.replyMsg);
msgView.setMovementMethod(new ScrollingMovementMethod());
replyMsg.setOnClickListener(new onClickListener() {
public void onClick(View v) {
msgView.append("\n> " + reply.getText());
}
这是用于在文本视图中显示消息的代码,该消息后附加/ n以显示在回复窗口中键入的每个句子。
我需要您的帮助,以便在聊天窗口中显示消息,而不是看起来像这样:
“ >你好”
您可以自定义可绘制形状并将其分配给带有一些角的textView
tvBackGround.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/colorFour" />
<stroke
android:color="@color/colorTwo"
android:width="2dp" />
<corners
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp" />
以及您的Java中
msgView.setBackground(R.drawable.tvBackGround)
代码应输入onCreate()
。如果您正在某个活动上运行它,然后进入onCreateView()
,如果您正在某个片段上运行它