1.运行结果
2.实验代码
[java] view plain copy
1. package cn.edu.bzu.message;
2.
3. import java.util.ArrayList;
4. import java.util.List;
5.
6. import android.os.Bundle;<pre name="code" class="html"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
7. android:layout_width="match_parent"
8. android:layout_height="match_parent"
9. android:orientation="vertical"
10. android:padding="10dp" >
11.
12. <LinearLayout
13. android:id="@+id/left_layout"
14. android:layout_width="wrap_content"
15. android:layout_height="wrap_content"
16. android:layout_gravity="left"
17. android:background="@drawable/image" >
18.
19. <TextView
20. android:id="@+id/left_msg"
21. android:layout_width="wrap_content"
22. android:layout_height="wrap_content"
23. android:layout_gravity="center"
24. android:layout_margin="10dp"
25. android:textColor="#fff" />
26. </LinearLayout>
27.
28. <LinearLayout
29. android:id="@+id/right_layout"
30. android:layout_width="215dp"
31. android:layout_height="wrap_content"
32. android:layout_gravity="right"
33. android:background="@drawable/picture" >
34.
35. <TextView
36. android:id="@+id/right_msg"
37. android:layout_width="wrap_content"
38. android:layout_height="wrap_content"
39. android:layout_gravity="center"
40. android:layout_margin="10dp" />
41. </LinearLayout>
42.
43. </LinearLayout>
import android.app.Activity;import android.view.Menu;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>();@Overrideprotected 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);send = (Button) findViewById(R.id.send);msgListView = (ListView) findViewById(R.id.msg);msgListView.setAdapter(adapter);send.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString 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("");}}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}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 to meet you.", Msg.TYPE_RECEIVED);msgList.add(msg3);}}
[java] view plain copy
1. package cn.edu.bzu.message;
2.
3. public class Msg {
4. public static final int TYPE_RECEIVED = 0;
5. public static final int TYPE_SENT = 1;
6. private String content;
7. private int type;
8.
9. public Msg(String content, int type) {
10. this.content = content;
11. this.type = type;
12. }
13.
14. public String getContent() {
15. return content;
16. }
17.
18. public int getType() {
19. return type;
20. }
21. }
[java] view plain copy
1. package cn.edu.bzu.message;
2.
3. import java.util.List;
4.
5. import android.content.Context;
6. import android.view.LayoutInflater;
7. import android.view.View;
8. import android.view.ViewGroup;
9. import android.widget.ArrayAdapter;
10. import android.widget.LinearLayout;
11. import android.widget.TextView;
12.
13. public class MsgAdapter extends ArrayAdapter<Msg> {
14. private int resourceId;
15.
16. public MsgAdapter(Context context, int textViewResourceId, List<Msg> objects) {
17. super(context, textViewResourceId, objects);
18. resourceId = textViewResourceId;
19. }
20.
21. public View getView(int position, View convertView, ViewGroup parent) {
22. Msg msg = getItem(position);
23. View view;
24. ViewHolder viewHolder;
25. if (convertView == null) {
26. view = LayoutInflater.from(getContext()).inflate(resourceId, null);
27. viewHolder = new ViewHolder();
28. viewHolder.leftLayout = (LinearLayout) view
29. .findViewById(R.id.left_layout);
30. viewHolder.rightLayout = (LinearLayout) view
31. .findViewById(R.id.right_layout);
32. view.setTag(viewHolder);
33.
34. } else {
35. view = convertView;
36. viewHolder = (ViewHolder) view.getTag();
37. }
38. if (msg.getType() == Msg.TYPE_RECEIVED) {
39. viewHolder.leftLayout.setVisibility(View.VISIBLE);
40. viewHolder.rightLayout.setVisibility(View.GONE);
41. viewHolder.leftMsg.setText(msg.getContent());
42. } else if (msg.getType() == Msg.TYPE_SENT) {
43. viewHolder.rightLayout.setVisibility(View.VISIBLE);
44. viewHolder.leftLayout.setVisibility(View.GONE);
45. viewHolder.rightMsg.setText(msg.getContent());
46. }
47. return view;
48. }
49.
50. class ViewHolder {
51. LinearLayout leftLayout;
52. LinearLayout rightLayout;
53. TextView leftMsg;
54. TextView rightMsg;
55. }
56. }
[java] view plain copy
1. <pre name="code" class="html"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2. xmlns:tools="http://schemas.android.com/tools"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:paddingBottom="@dimen/activity_vertical_margin"
6. android:paddingLeft="@dimen/activity_horizontal_margin"
7. android:paddingRight="@dimen/activity_horizontal_margin"
8. android:paddingTop="@dimen/activity_vertical_margin"
9. tools:context=".MainActivity" >
10.
11. <TextView
12. android:layout_width="wrap_content"
13. android:layout_height="wrap_content"
14. android:text="@string/hello_world" />
15. </RelativeLayout>