Android仿微信气泡聊天界面设计

Android仿微信气泡聊天界面设计


  微信的气泡聊天是仿iPhone自带短信而设计出来的,不过感觉还不错可以尝试一下仿着微信的气泡聊天做一个Demo,给大家分享一下!效果图如下:


  气泡聊天最终要的是素材,要用到9.png文件的素材,这样气泡会随着聊天内容的多少而改变气泡的大小且不失真。为了方便,我就直接在微信里面提取出来啦。


  聊天的内容是用ListView来显示的,将聊天的内容封装成一个ChatMsgEntity类的对象里面,然后交给自定义的ListView适配器将聊天内容显示出来。

  ChatMsgEntity.java比较简单,只是聊天的内容存储、设置和获取。

[java]  view plain copy
  1. package com.example.school;  
  2.   
  3. public class ChatMsgEntity {  
  4.     private static final String TAG = ChatMsgEntity.class.getSimpleName();  
  5.     //名字  
  6.     private String name;  
  7.     //日期  
  8.     private String date;  
  9.     //聊天内容  
  10.     private String text;  
  11.     //是否为对方发来的信息  
  12.     private boolean isComMeg = true;  
  13.   
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.   
  18.     public void setName(String name) {  
  19.         this.name = name;  
  20.     }  
  21.   
  22.     public String getDate() {  
  23.         return date;  
  24.     }  
  25.   
  26.     public void setDate(String date) {  
  27.         this.date = date;  
  28.     }  
  29.   
  30.     public String getText() {  
  31.         return text;  
  32.     }  
  33.   
  34.     public void setText(String text) {  
  35.         this.text = text;  
  36.     }  
  37.   
  38.     public boolean getMsgType() {  
  39.         return isComMeg;  
  40.     }  
  41.   
  42.     public void setMsgType(boolean isComMsg) {  
  43.         isComMeg = isComMsg;  
  44.     }  
  45.   
  46.     public ChatMsgEntity() {  
  47.     }  
  48.   
  49.     public ChatMsgEntity(String name, String date, String text, boolean isComMsg) {  
  50.         this.name = name;  
  51.         this.date = date;  
  52.         this.text = text;  
  53.         this.isComMeg = isComMsg;  
  54.     }  
  55. }  

  显示ListView的适配器ChatMsgViewAdpater.java:

[java]  view plain copy
  1. package com.example.school;  
  2.   
  3. import android.R.integer;  
  4. import android.content.Context;  
  5. import android.database.DataSetObserver;  
  6.   
  7. import android.util.Log;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.view.View.OnLongClickListener;  
  12. import android.view.ViewGroup;  
  13.   
  14. import android.widget.BaseAdapter;  
  15. import android.widget.CheckBox;  
  16. import android.widget.LinearLayout;  
  17. import android.widget.TextView;  
  18.   
  19. import java.util.ArrayList;  
  20. import java.util.List;  
  21.   
  22. public class ChatMsgViewAdapter extends BaseAdapter {  
  23.       
  24.     //ListView视图的内容由IMsgViewType决定  
  25.     public static interface IMsgViewType  
  26.     {  
  27.         //对方发来的信息  
  28.         int IMVT_COM_MSG = 0;  
  29.         //自己发出的信息  
  30.         int IMVT_TO_MSG = 1;  
  31.     }  
  32.       
  33.     private static final String TAG = ChatMsgViewAdapter.class.getSimpleName();  
  34.     private List<ChatMsgEntity> data;  
  35.     private Context context;    
  36.     private LayoutInflater mInflater;  
  37.   
  38.     public ChatMsgViewAdapter(Context context, List<ChatMsgEntity> data) {  
  39.         this.context = context;  
  40.         this.data = data;  
  41.         mInflater = LayoutInflater.from(context);  
  42.     }  
  43.   
  44.     //获取ListView的项个数  
  45.     public int getCount() {  
  46.         return data.size();  
  47.     }  
  48.   
  49.     //获取项  
  50.     public Object getItem(int position) {  
  51.         return data.get(position);  
  52.     }  
  53.   
  54.     //获取项的ID  
  55.     public long getItemId(int position) {  
  56.         return position;  
  57.     }  
  58.   
  59.     //获取项的类型  
  60.     public int getItemViewType(int position) {  
  61.         // TODO Auto-generated method stub  
  62.         ChatMsgEntity entity = data.get(position);  
  63.           
  64.         if (entity.getMsgType())  
  65.         {  
  66.             return IMsgViewType.IMVT_COM_MSG;  
  67.         }else{  
  68.             return IMsgViewType.IMVT_TO_MSG;  
  69.         }  
  70.           
  71.     }  
  72.   
  73.     //获取项的类型数  
  74.     public int getViewTypeCount() {  
  75.         // TODO Auto-generated method stub  
  76.         return 2;  
  77.     }  
  78.       
  79.     //获取View  
  80.     public View getView(int position, View convertView, ViewGroup parent) {  
  81.           
  82.         ChatMsgEntity entity = data.get(position);  
  83.         boolean isComMsg = entity.getMsgType();  
  84.               
  85.         ViewHolder viewHolder = null;     
  86.         if (convertView == null)  
  87.         {  
  88.               if (isComMsg)  
  89.               {  
  90.                   //如果是对方发来的消息,则显示的是左气泡  
  91.                   convertView = mInflater.inflate(R.layout.chatting_item_msg_text_left, null);  
  92.               }else{  
  93.                   //如果是自己发出的消息,则显示的是右气泡  
  94.                   convertView = mInflater.inflate(R.layout.chatting_item_msg_text_right, null);  
  95.               }  
  96.   
  97.               viewHolder = new ViewHolder();  
  98.               viewHolder.tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);  
  99.               viewHolder.tvUserName = (TextView) convertView.findViewById(R.id.tv_username);  
  100.               viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tv_chatcontent);  
  101.               viewHolder.isComMsg = isComMsg;  
  102.                 
  103.               convertView.setTag(viewHolder);  
  104.         }else{  
  105.             viewHolder = (ViewHolder) convertView.getTag();  
  106.         }  
  107.         viewHolder.tvSendTime.setText(entity.getDate());  
  108.         viewHolder.tvUserName.setText(entity.getName());  
  109.         viewHolder.tvContent.setText(entity.getText());  
  110.           
  111.         return convertView;  
  112.     }  
  113.       
  114.     //通过ViewHolder显示项的内容  
  115.     static class ViewHolder {   
  116.         public TextView tvSendTime;  
  117.         public TextView tvUserName;  
  118.         public TextView tvContent;  
  119.         public boolean isComMsg = true;  
  120.     }  
  121.       
  122. }  

  对方发来消息的显示布局chatting_item_msg_text_left.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.         android:layout_width="fill_parent"  
  4.         android:layout_height="wrap_content"  
  5.         android:orientation="vertical"  
  6.         android:padding="6dp">  
  7.         <LinearLayout  
  8.             android:layout_width="fill_parent"  
  9.             android:layout_height="wrap_content"  
  10.             android:orientation="vertical"   
  11.             android:gravity="center_horizontal">  
  12.             <TextView  
  13.                 android:id="@+id/tv_sendtime"  
  14.                 android:layout_width="wrap_content"  
  15.                 android:layout_height="wrap_content"  
  16.                 style="@style/chat_text_date_style"/>  
  17.         </LinearLayout>  
  18.         <RelativeLayout  
  19.             android:layout_width="fill_parent"  
  20.             android:layout_height="wrap_content"  
  21.             android:layout_marginTop="5dp" >  
  22.                 <ImageView   
  23.                    android:id="@+id/iv_userhead"   
  24.                    android:layout_width="wrap_content"  
  25.                    android:layout_height="wrap_content"  
  26.                    android:focusable="false"   
  27.                    android:layout_alignParentLeft="true"   
  28.                    android:layout_alignParentTop="true"   
  29.                    android:background="@drawable/mini_avatar_shadow"/>  
  30.                 <TextView   
  31.                 android:id="@+id/tv_chatcontent"   
  32.                 android:layout_toRightOf="@id/iv_userhead"  
  33.                 android:layout_marginLeft="10dp"  
  34.                 android:layout_width="wrap_content"  
  35.                 android:layout_height="wrap_content"  
  36.                 android:background="@drawable/chatfrom_bg"   
  37.                 style="@style/chat_content_date_style"/>     
  38.                 <TextView   
  39.                 android:id="@+id/tv_username"   
  40.                 android:layout_width="wrap_content"  
  41.                 android:layout_height="wrap_content"  
  42.                 android:layout_below="@id/iv_userhead"  
  43.                 android:layout_alignParentLeft="true"  
  44.                 android:layout_toLeftOf="@id/tv_chatcontent"  
  45.                 style="@style/chat_text_name_style"/>  
  46.         </RelativeLayout>  
  47. </LinearLayout>  

  chatting_item_msg_text_right.xml和上面差不多,只是气泡和头像的方向在右边,就不贴代码了。

  

  主界面的布局chat.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="#f0f0e0" >  
  7.     <RelativeLayout  
  8.         android:id="@+id/rl_layout"   
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:background="@drawable/title" >  
  12.         <Button   
  13.             android:id="@+id/btn_back"   
  14.             android:gravity="center_vertical"   
  15.             android:layout_marginLeft="5dp"   
  16.             android:paddingLeft="18dp"   
  17.             android:textSize="18.0sp"   
  18.             android:textColor="#ffffff"    
  19.             android:background="@drawable/selector_btn_back"   
  20.             android:layout_width="70dp"   
  21.             android:layout_height="wrap_content"   
  22.             android:text="@string/back" />  
  23.         <TextView  
  24.             android:layout_width="wrap_content"  
  25.             android:layout_height="wrap_content"  
  26.             android:text="@string/school_title_name"  
  27.             android:layout_centerInParent="true"  
  28.             style="@style/my_txt"/>  
  29.         <ImageView   
  30.             android:layout_width="wrap_content"  
  31.             android:layout_height="wrap_content"  
  32.             android:background="@drawable/campus_info"  
  33.             android:layout_centerVertical="true"  
  34.             android:layout_alignParentRight="true"  
  35.             android:layout_marginRight="15dp"/>  
  36.         </RelativeLayout>  
  37.     <RelativeLayout  
  38.         android:id="@+id/rl_bottom"  
  39.         android:layout_width="fill_parent"  
  40.         android:layout_height="wrap_content"  
  41.         android:layout_alignParentBottom="true"  
  42.         android:background="@drawable/layout_bg1" >  
  43.         <Button  
  44.             android:id="@+id/btn_send"  
  45.             android:layout_width="60dp"  
  46.             android:layout_height="40dp"  
  47.             android:layout_alignParentRight="true"  
  48.             android:layout_marginRight="10dp"  
  49.             android:layout_centerVertical="true"  
  50.             android:text="@string/send" />  
  51.         <EditText  
  52.             android:id="@+id/et_sendmessage"  
  53.             android:layout_width="fill_parent"  
  54.             android:layout_height="40dp"  
  55.             android:layout_toLeftOf="@id/btn_send"  
  56.             android:layout_marginLeft="10dp"  
  57.             android:layout_marginRight="10dp"  
  58.             android:background="@drawable/edittext1"  
  59.             android:layout_centerVertical="true"  
  60.             android:singleLine="true"  
  61.             android:textSize="18sp"/>  
  62.     </RelativeLayout>  
  63.     <ListView  
  64.         android:id="@+id/listview"  
  65.         android:layout_below="@id/rl_layout"  
  66.         android:layout_above="@id/rl_bottom"  
  67.         android:layout_width="fill_parent"  
  68.         android:layout_height="fill_parent"  
  69.         android:layout_marginLeft="10.0dip"   
  70.         android:layout_marginTop="10.0dip"   
  71.         android:layout_marginRight="10.0dip"  
  72.         android:divider="@null"  
  73.         android:dividerHeight="5dp"  
  74.         android:scrollbars="none"  
  75.         android:cacheColorHint="#00000000"/>  
  76. </RelativeLayout>  

  ChatActivity.java

[java]  view plain copy
  1. package com.example.chat;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Calendar;  
  5. import java.util.List;  
  6.   
  7. import com.example.school.R;  
  8.   
  9. import android.os.Bundle;  
  10. import android.app.Activity;  
  11. import android.content.Intent;  
  12. import android.view.KeyEvent;  
  13. import android.view.Menu;  
  14. import android.view.View;  
  15. import android.view.Window;  
  16. import android.view.View.OnClickListener;  
  17. import android.widget.AdapterView;  
  18. import android.widget.AdapterView.OnItemClickListener;  
  19. import android.widget.Button;  
  20. import android.widget.EditText;  
  21. import android.widget.ListView;  
  22. import android.widget.TextView;  
  23.   
  24. public class ChatActivity extends Activity implements OnClickListener {  
  25.     private Button mBtnSend;  
  26.     private Button mBtnBack;  
  27.     private EditText mEditTextContent;  
  28.     //聊天内容的适配器  
  29.     private ChatMsgViewAdapter mAdapter;  
  30.     private ListView mListView;  
  31.     //聊天的内容  
  32.     private List<ChatMsgEntity> mDataArrays = new ArrayList<ChatMsgEntity>();  
  33.       
  34.     @Override  
  35.     public void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏  
  38.         setContentView(R.layout.chat);  
  39.         initView();  
  40.         initData();  
  41.     }  
  42.       
  43.     //初始化视图  
  44.     private void initView() {  
  45.         mListView = (ListView) findViewById(R.id.listview);  
  46.         mBtnBack = (Button) findViewById(R.id.btn_back);  
  47.         mBtnBack.setOnClickListener(this);  
  48.         mBtnSend = (Button) findViewById(R.id.btn_send);  
  49.         mBtnSend.setOnClickListener(this);  
  50.         mEditTextContent = (EditText) findViewById(R.id.et_sendmessage);  
  51.     }  
  52.   
  53.     private String[] msgArray = new String[]{"  孩子们,要好好学习,天天向上!要好好听课,不要翘课!不要挂科,多拿奖学金!三等奖学金的争取拿二等,二等的争取拿一等,一等的争取拿励志!",   
  54.             "姚妈妈还有什么吩咐...",   
  55.             "还有,明天早上记得跑操啊,不来的就扣德育分!",   
  56.             "德育分是什么?扣了会怎么样?",   
  57.             "德育分会影响奖学金评比,严重的话,会影响毕业",   
  58.             "哇!学院那么不人道?",  
  59.             "你要是你不听话,我当场让你不能毕业!",   
  60.             "姚妈妈,我知错了(- -我错在哪了...)"};  
  61.   
  62.     private String[]dataArray = new String[]{"2012-09-01 18:00""2012-09-01 18:10",   
  63.             "2012-09-01 18:11""2012-09-01 18:20",   
  64.             "2012-09-01 18:30""2012-09-01 18:35",   
  65.             "2012-09-01 18:40""2012-09-01 18:50"};  
  66.     private final static int COUNT = 8;  
  67.       
  68.     //初始化要显示的数据  
  69.     private void initData() {  
  70.         for(int i = 0; i < COUNT; i++) {  
  71.             ChatMsgEntity entity = new ChatMsgEntity();  
  72.             entity.setDate(dataArray[i]);  
  73.             if (i % 2 == 0)  
  74.             {  
  75.                 entity.setName("姚妈妈");  
  76.                 entity.setMsgType(true);  
  77.             }else{  
  78.                 entity.setName("Shamoo");  
  79.                 entity.setMsgType(false);  
  80.             }  
  81.               
  82.             entity.setText(msgArray[i]);  
  83.             mDataArrays.add(entity);  
  84.         }  
  85.         mAdapter = new ChatMsgViewAdapter(this, mDataArrays);  
  86.         mListView.setAdapter(mAdapter);  
  87.     }  
  88.       
  89.     public void onClick(View view) {  
  90.         // TODO Auto-generated method stub  
  91.         switch(view.getId()) {  
  92.         case R.id.btn_back:  
  93.             back();  
  94.             break;  
  95.         case R.id.btn_send:  
  96.             send();  
  97.             break;  
  98.         }  
  99.     }  
  100.   
  101.     private void send()  
  102.     {  
  103.         String contString = mEditTextContent.getText().toString();  
  104.         if (contString.length() > 0)  
  105.         {  
  106.             ChatMsgEntity entity = new ChatMsgEntity();  
  107.             entity.setDate(getDate());  
  108.             entity.setName("");  
  109.             entity.setMsgType(false);  
  110.             entity.setText(contString);  
  111.             mDataArrays.add(entity);  
  112.             mAdapter.notifyDataSetChanged();  
  113.             mEditTextContent.setText("");  
  114.             mListView.setSelection(mListView.getCount() - 1);  
  115.         }  
  116.     }  
  117.       
  118.     //获取日期  
  119.     private String getDate() {  
  120.         Calendar c = Calendar.getInstance();  
  121.         String year = String.valueOf(c.get(Calendar.YEAR));  
  122.         String month = String.valueOf(c.get(Calendar.MONTH));  
  123.         String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + 1);  
  124.         String hour = String.valueOf(c.get(Calendar.HOUR_OF_DAY));  
  125.         String mins = String.valueOf(c.get(Calendar.MINUTE));  
  126.         StringBuffer sbBuffer = new StringBuffer();  
  127.         sbBuffer.append(year + "-" + month + "-" + day + " " + hour + ":" + mins);   
  128.         return sbBuffer.toString();  
  129.     }  
  130.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  131.         back();  
  132.         return true;  
  133.     }  
  134.       
  135.     private void back() {  
  136.         finish();  
  137.     }  
  138. }  

本文转自: http://blog.csdn.net/pocoyoshamoo/article/details/9674385


  

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现一个仿微信聊天界面,你可以使用以下代码作为参考: 首先,你可以使用一个LinearLayout来作为整个界面的容器。设置它的属性如下: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> ``` 在LinearLayout中,你可以加入一个TextView来显示聊天界面的标题。设置其属性如下: ``` <TextView android:id="@id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="这是微信聊天界面" android:textSize="25sp" /> ``` 接下来,你可以添加一个LinearLayout作为底部导航栏,用来显示各个功能模块的图标和文本。设置其属性如下: ``` <LinearLayout android:id="@id/id_tab_weixin" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <ImageButton android:id="@id/weixin_img" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#000000" android:contentDescription="@string/app_name" app:srcCompat="@drawable/tab_weixin_normal" /> <TextView android:id="@id/微信" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center" android:text="微信" android:textColor="#ffffff" android:textSize="15dp" /> </LinearLayout> ``` 这样,你就可以实现一个简单的仿微信聊天界面了。你可以根据需要进一步添加和修改界面元素。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [android 仿微信demo————微信消息界面实现(移动端)](https://blog.csdn.net/weixin_42768634/article/details/117898771)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Android studio实现仿微信界面](https://blog.csdn.net/m0_51762092/article/details/120677239)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值