一 效果图
二 关键代码
MainActivity.java
package com.example.helloandroid;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView msgListView;
private EditText inputText;
private Button send;
private MsgAdapter adapter;
private List<Msg>msgList=new ArrayList<Msg>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initMsgs();
adapter=new MsgAdapter(MainActivity.this, R.layout.msg_item, msgList);
inputText=(EditText)findViewById(R.id.input_text);
send=(Button)findViewById(R.id.send);
msgListView=(ListView)findViewById(R.id.msg_list_view);
msgListView.setAdapter(adapter);
send.setOnClickListener(new OnClickListener(){
public void onClick(View v){
String content=inputText.getText().toString();
if(!"".equals(content)){
Msg msg=new Msg(content, Msg.TYPE_SENT);
msgList.add(msg);
adapter.notifyDataSetChanged();
msgListView.setSelection(msgList.size());
inputText.setText("");
}
}
});
}
private void initMsgs(){
Msg msg1=new Msg("Hello guy.",Msg.TYPE_RECEIVED);
msgList.add(msg1);
Msg msg2=new Msg("Hello.Who is that?",Msg.TYPE_SENT);
msgList.add(msg2);
Msg msg3=new Msg("This is Tom.Nice talking ti you.",Msg.TYPE_RECEIVED);
msgList.add(msg3);
}
MsgAdapter.java
package com.example.helloandroid;
import java.util.List;</p><p>import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;</p><p>public class MsgAdapter extends ArrayAdapter<Msg>
{
private int resourceId;</p><p> public MsgAdapter(Context context, int textViewResourceId, List<Msg> objects)
{
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}</p><p> public View getView(int position, View converView, ViewGroup parents)
{
Msg msg = getItem(position);
View view;
ViewHolder viewHolder;
if (converView == null)
{
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
viewHolder = new ViewHolder();
viewHolder.leftLaylout = (LinearLayout) view
.findViewById(R.id.left_layout);
viewHolder.rightLaylout= (LinearLayout) view
.findViewById(R.id.right_layout);
viewHolder.leftMsg = (TextView) view.findViewById(R.id.left_msg);
viewHolder.rightMsg = (TextView) view.findViewById(R.id.right_msg);
view.setTag(viewHolder);
} else
{
view = converView;
viewHolder = (ViewHolder) view.getTag();
}
if (msg.getType() == Msg.TYPE_RECEIVED)
{
viewHolder.leftLaylout.setVisibility(View.VISIBLE);
viewHolder.rightLaylout.setVisibility(View.GONE);
viewHolder.leftMsg.setText(msg.getContent());
} else if (msg.getType()== Msg.TYPE_SENT)
{
viewHolder.rightLaylout.setVisibility(View.VISIBLE);
viewHolder.leftLaylout.setVisibility(View.GONE);
viewHolder.rightMsg.setText(msg.getContent());</p><p> }
return view;</p><p> }</p><p> class ViewHolder
{
LinearLayout leftLaylout;
LinearLayout rightLaylout;
TextView leftMsg;
TextView rightMsg;</p><p> }
}
Msg.java
package com.example.helloandroid;
public class Msg
{
public static final int TYPE_RECEIVED = 0;
public static final int TYPE_SENT = 1;
private String content;
private int type;
public Msg(String content, int type) {
this.content = content;
this.type = type;
}
public String getContent() {
return content;
}
public int getType() {
return type;
}
}
msg_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="<a target=_blank href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:background="#0000ff"></p><p> <LinearLayout
android:id="@+id/left_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/message_left"
android:gravity="left" ></p><p> <TextView
android:id="@+id/left_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:textColor="#fff" />
</LinearLayout></p><p> <LinearLayout
android:id="@+id/right_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/message_right"
android:gravity="right" ></p><p> <TextView
android:id="@+id/right_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center" />
</LinearLayout></p><p></LinearLayout></p><p>5.activity_main.xml</p><p><LinearLayout xmlns:android="<a target=_blank href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d8e0e8"
android:orientation="vertical" ></p><p> <ListView
android:id="@+id/msg_list_view"
android:layout_width="match_parent"
android:layout_height="372dp"
android:layout_weight="1.03"
android:divider="#0000" >
</ListView></p><p> <LinearLayout
android:layout_width="match_parent"
android:layout_height="62dp" ></p><p> <EditText
android:id="@+id/input_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:hint="Type something here"
android:maxLines="2" /></p><p> <Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" />
</LinearLayout></p><p></LinearLayout>