网上借的
整个工程:
package com.ql.app; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; public class ChatActivity extends Activity { private ChattingAdapter adapter; private List<ChatMessage> messages = new ArrayList<ChatMessage>(); private ListView listView; private Button btn_send; private EditText textEditor; private ImageView btn_insert; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); messages.add(new ChatMessage(ChatMessage.MESSAGE_FROM, "hello,android.")); messages.add(new ChatMessage(ChatMessage.MESSAGE_TO, "hello,java.")); messages.add(new ChatMessage(ChatMessage.MESSAGE_FROM, "请教高手,怎么可以给gridview设置边框啊,现在这个显示效果实在太难看了。")); messages.add(new ChatMessage(ChatMessage.MESSAGE_TO, "那就加点背景,加点属性什么的不就行了吗")); messages.add(new ChatMessage(ChatMessage.MESSAGE_FROM, "在主题中,将gridview的cssclass设置为gridview")); messages.add(new ChatMessage(ChatMessage.MESSAGE_TO, "谢谢")); listView=(ListView)findViewById(R.id.listView); adapter = new ChattingAdapter(this, messages); listView.setAdapter(adapter); btn_send = (Button) findViewById(R.id.btn_send); textEditor = (EditText) findViewById(R.id.text_editor); btn_insert=(ImageView) findViewById(R.id.btn_insert); btn_send.setOnClickListener(listener); btn_insert.setOnClickListener(listener); } private View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_send: String str = textEditor.getText().toString(); String sendStr=str.trim(); if(!sendStr.equals("")){ sendMessage(sendStr); } textEditor.setText(""); break; case R.id.btn_insert: Intent i = new Intent(); i.setType("image/*"); i.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(i, Activity.DEFAULT_KEYS_SHORTCUT); // Intent it = new Intent("android.media.action.IMAGE_CAPTURE"); // startActivityForResult(it, Activity.DEFAULT_KEYS_DIALER); break; default: break; } } // 模拟发送消息 private void sendMessage(String sendStr) { messages.add(new ChatMessage(ChatMessage.MESSAGE_TO, sendStr)); adapter.notifyDataSetChanged(); } }; }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:focusable="false" android:focusableInTouchMode="false" android:background="@drawable/nav_page" android:gravity="center_horizontal" > <ListView android:id="@+id/listView" android:background="@null" android:scrollbars="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:listSelector="@null" android:transcriptMode="alwaysScroll" android:cacheColorHint="#00000000" android:divider="@null" android:layout_weight="1.0" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/txt_msg_bg" android:paddingRight="7.0dip" > <ImageView android:id="@+id/btn_insert" android:layout_gravity="center_vertical" android:paddingLeft="15.0dip" android:paddingTop="5.0dip" android:paddingRight="7.0dip" android:paddingBottom="5.0dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/sms_insert" /> <EditText android:id="@+id/text_editor" android:layout_width="0.0dip" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:background="@drawable/sms_embeded_text_editor_bg" android:focusable="true" android:nextFocusRight="@+id/send_button" android:layout_marginLeft="7.0dip" android:layout_marginTop="5.0dip" android:layout_marginRight="7.0dip" android:layout_marginBottom="5.0dip" android:minHeight="34.0dip" android:hint="输入消息" android:maxLines="8" android:maxLength="2000" android:capitalize="sentences" android:autoText="true" android:layout_weight="1.0" android:inputType="textCapSentences|textAutoCorrect|textMultiLine|textShortMessage" android:imeOptions="actionSend|flagNoEnterAction" /> <Button android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center_vertical" android:background="@drawable/sms_send_button_bg" android:paddingLeft="11.0dip" android:paddingRight="11.0dip" android:nextFocusLeft="@id/text_editor" /> </LinearLayout> </LinearLayout>
整个工程: