微信聊天窗口的信息效果类似iphone上的短信效果,以气泡的形式展现,在android上,实现这种效果主要用到listview和baseadapter,配合布局以及相关素材,就可以自己做出这个效果,素材可以下一个微信的apk,然后把后缀名改成zip,直接解压,就可以得到微信里面的所有素材了。首先看一下我实现的效果:
以下是工程目录结构:
接下来就是如何实现这个效果的代码:
main.xml,这个是主布局文件,显示listview和上下两部分内容。
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f0f0e0" >
<relativelayout
android:id="@+id/rl_top"
android:layout_width="fill_parent"
android:layout_alignparenttop="true"
android:layout_height="wrap_content">
<textview
android:layout_width="fill_parent"
android:layout_height="44dp"
android:gravity="center"
android:textsize="18sp"
android:background="#486a9a"
android:textcolor="@android:color/white"
android:text="chat"/>
</relativelayout>
<relativelayout
android:id="@+id/rl_bottom"
android:layout_alignparentbottom="true"
android:layout_width="fill_parent"
android:background="#486a9a"
android:paddingtop="5dp"
android:layout_height="wrap_content">
<button
android:id="@+id/btn_send"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignparentright="true"
android:layout_centervertical="true"
android:layout_marginright="10dp"
android:text="send" />
<edittext
android:id="@+id/et_content"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_centervertical="true"
android:layout_marginleft="10dp"
android:layout_marginright="10dp"
android:layout_toleftof="@id/btn_send"
android:textsize="16sp"/>
</relativelayout>
<listview
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/rl_bottom"
android:layout_below="@id/rl_top"
android:layout_marginleft="10dp"
android:layout_marginright="10dp"
android:layout_margintop="10dp"
android:cachecolorhint="#00000000"
android:divider="@null"
android:listselector="#00000000"
android:dividerheight="3dp"
android:scrollbars="none"/>
</relativelayout>
然后就是listview中两种类型item的布局文件,分别是接收信息的item效果和发送信息的item效果
chat_from_item.xml是接收信息的item布局:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:paddingbottom="5dp"
android:layout_height="wrap_content" >
<textview
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#bfbfbf"
android:paddingtop="2dp"
android:paddingbottom="2dp"
android:paddingleft="4dp"
android:paddingright="4dp"
android:textcolor="#ffffff"
android:textsize="12sp" />
<relativelayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margintop="5dp" >
<imageview
android:id="@+id/iv_user_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignparentleft="true"
android:layout_alignparenttop="true"
android:background="@drawable/mypic"
android:focusable="false" />
<textview
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginleft="5dp"
android:layout_torightof="@+id/iv_user_image"
android:background="@drawable/chatfrom_bg"
android:gravity="left|center"
android:clickable="true"
android:focusable="true"
android:linespacingextra="2dp"
android:minheight="50dp"
android:textcolor="#ff000000"
android:textsize="14sp" />
</relativelayout>
</linearlayout>
chat_to_item.xml是发送信息item的布局:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:paddingbottom="5dp"
android:layout_height="wrap_content" >
<textview
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#bfbfbf"
android:layout_gravity="center_horizontal"
android:paddingtop="2dp"
android:paddingbottom="2dp"
android:paddingleft="4dp"
android:paddingright="4dp"
android:textcolor="#ffffff"
android:textsize="12sp" />
<relativelayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margintop="5dp" >
<imageview
android:id="@+id/iv_user_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignparentright="true"
android:layout_alignparenttop="true"
android:background="@drawable/mypic"
android:focusable="false" />
<textview
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginright="5dp"
android:layout_toleftof="@+id/iv_user_image"
android:background="@drawable/chatto_bg"
android:gravity="left|center"
android:clickable="true"
android:focusable="true"
android:linespacingextra="2dp"
android:textcolor="#ff000000"
android:textsize="14sp" />
</relativelayout>
</linearlayout>
布局完成后新建一个实体类chatentity.java:
复制代码 代码如下:
public class chatentity {
private int userimage;
private string content;
private string chattime;
private boolean iscomemsg;
public int getuserimage() {
return userimage;
}
public void setuserimage(int userimage) {
this.userimage = userimage;
}
public string getcontent() {
return content;
}
public void setcontent(string content) {
this.content = content;
}
public string getchattime() {
return chattime;
}
public void setchattime(string chattime) {
this.chattime = chattime;
}
public boolean iscomemsg() {
return iscomemsg;
}
public void setcomemsg(boolean iscomemsg) {
this.iscomemsg = iscomemsg;
}
}
最后就是主activity,这里面包括了自己写的baseadapter:
复制代码 代码如下:
public class chatdemoactivity extends activity {
private button sendbutton
80后的玩具