Android仿微信聊天界面布局
在Android开发中,很多时候都需要接入即时通信功能,那么就需要一个聊天的布局界面,下面就来给大家介绍一下,怎么来布局聊天界面。
1.第一步首先是制作9.patch图片,这个在Android sdk 目录下tools文件,找到draw9patch.bat文件双击打开。这是一个专门用来处理安卓里面图像的小工具,你可以对图片指定拉伸的效果,具体教程可以百度,很简单的。
2.编写主界面
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#d8e0e8"
- android:orientation="vertical">
- <ListView
- android:id="@+id/msg_list_view"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1"
- android:divider="#0000">
- </ListView>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <EditText
- android:id="@+id/input_text"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:hint="Typ something in here"
- android:maxLines="2"/>
- <Button
- android:id="@+id/send"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="send"/>
- </LinearLayout>
- </LinearLayout>
![](http://static.blog.csdn.net/images/save_snippets.png)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#d8e0e8" android:orientation="vertical"><ListView android:id="@+id/msg_list_view"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:divider="#0000"></ListView><LinearLayout android:layout_width="match_parent"android:layout_height="wrap_content"><EditText android:id="@+id/input_text"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:hint="Typ something in here"android:maxLines="2"/><Button android:id="@+id/send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="send"/></LinearLayout>
</LinearLayout>
主界面里我们可以看到有一个ListView用于加载信息,线性布局进行嵌套,里层里有一个编辑框和一个按钮。下图就是布局初步效果图:
3.编写ListView子项布局,新建一个布局文件
- <span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:padding="10dp">
- <LinearLayout
- android:id="@+id/left_layout"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="left"
- android:background="@drawable/message_left">
- <TextView
- android:id="@+id/left_msg"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:layout_margin="10dp"
- android:textColor="#fff"/>
- </LinearLayout>
- <LinearLayout
- android:id="@+id/right_Layout"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="right"
- android:background="@drawable/message_right"
- >
- <TextView
- android:id="@+id/right_msg"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_margin="10dp"/>
- </LinearLayout>
- </LinearLayout></span>
![](http://static.blog.csdn.net/images/save_snippets.png)
<span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" android:padding="10dp"><LinearLayout android:id="@+id/left_layout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="left"android:background="@drawable/message_left"><TextView android:id="@+id/left_msg"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:layout_margin="10dp"android:textColor="#fff"/></LinearLayout><LinearLayout android:id="@+id/right_Layout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="right"android:background="@drawable/message_right"><TextView android:id="@+id/right_msg"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_margin="10dp"/> </LinearLayout>
</LinearLayout></span>
这里最外层是线性布局,里边嵌套两个线性布局。这两个布局分别是给消息发送界面和消息接收界面。后面可以通过设置布局的显示和隐藏属性来显示我们需要的消息。这也是最近学习布局收获的一点,光学课本上的单个布局的特点没有应用到实际的应用里边,很难自己想象原来布局可以这样灵活。
效果图如下:
3.完成MainActivity.Java,写好布局文件以后开始写代码,这里需要大概写的几点是:
1.定义消息类的实体类Msg
public class Msg{ }
里面需要设置消息的类型:发送,接收,内容。
2.ListView适配器的建立
首先它需要继承ArrayAdapter,将泛型指定为Msg类。
这里运用到了很多书上说的ListView的优化,通过if_else判断,如果convertView为空就重新初始化加载布局,这时就需要加载很多东西,如果convertView不为空时,说明它之前有缓存,可以重用,那我们直接调用它,就大大提高了运行的效率。
还有通过创建ViewHolder这个内部类,可以对控件的实例进行缓存。当convertView为空时。将控件的实例存在ViewHolder里,调用setTag()方法,将ViewHolder对象存储在View里。当ViewHolder不为空时,调用View的setTag()方法,重新取出ViewHolder。这样就不用每次调用findViewById()方法获取控件。
3.收发消息布局的隐藏和显示
通过判断消息的类型,进行设置显示和隐藏消息。
if(msg.getType()==Msg.RECEIVED){
//如果是收到的消息,则显示左边消息布局,将右边消息布局隐藏
viewHolder.leftLayout.setVisibility(View.VISIBLE);
viewHolder.rightLayout.setVisibility(View.GONE);
viewHolder.leftMsg.setText(msg.getContent());
}else if(msg.getType()==Msg.SENT){
//如果是发出去的消息,显示右边布局的消息布局,将左边的消息布局隐藏
viewHolder.rightLayout.setVisibility(View.VISIBLE);
viewHolder.leftLayout.setVisibility(View.GONE);
viewHolder.rightMsg.setText(msg.getContent());
}
4.初始化消息initMsg()
写几条消息测试一下首发消息是否正确。
5.给按钮send设置监听器事件
这里用到的是send.setOnClickListener(new OnClickListener(){ },这一看就是匿名内部类的方式。
这里只是大体说了一下编写的大概内容,具体的可以看下面源代码:
- package com.example.chat_layout;
- import java.util.ArrayList;
- import java.util.List;
- import android.R.string;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.view.Window;
- import android.widget.Adapter;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- /*
- Created byYWHon 2017/2/26.
- */
- private ListView msgListView;
- private EditText inputText;
- private Button send;
- private MsgAdapter adapter;
- private List<Msg> msgList = new ArrayList<Msg>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);//设置窗口没有标题栏
- setContentView(R.layout.activity_main);
- initMsg();
- 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(){
- @Override
- public void onClick(View v) {
- String content = inputText.getText().toString();
- if(!"".equals(content)){
- Msg msg = new Msg(content, Msg.SENT);
- msgList.add(msg);
- adapter.notifyDataSetChanged();//有新消息时,刷新ListView中的显示
- msgListView.setSelection(msgList.size());//将ListView定位到最后一行
- inputText.setText("");//清空输入框的内容
- }
- }
- });
- }
- private void initMsg() {
- Msg msg1 = new Msg("I miss you!",Msg.RECEIVED);
- msgList.add(msg1);
- Msg msg2 = new Msg("I miss you,too!",Msg.SENT);
- msgList.add(msg2);
- Msg msg3 = new Msg("I will come back soon!",Msg.RECEIVED);
- msgList.add(msg3);
- }
- @Override
- public 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;
- }
- public class Msg{
- public static final int RECEIVED = 0;//收到一条消息
- public static final int 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;
- }
- }
- public class MsgAdapter extends ArrayAdapter<Msg>{
- private int resourceId;
- public MsgAdapter(Context context, int textViewresourceId, List<Msg> objects) {
- super(context, textViewresourceId, objects);
- resourceId = textViewresourceId;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- Msg msg = getItem(position);
- View view;
- ViewHolder viewHolder;
- if(convertView == null){
- view = LayoutInflater.from(getContext()).inflate(resourceId, null);
- viewHolder = new ViewHolder();
- viewHolder.leftLayout = (LinearLayout)view.findViewById(R.id.left_layout);
- viewHolder.rightLayout = (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 = convertView;
- viewHolder = (ViewHolder) view.getTag();
- }
- if(msg.getType()==Msg.RECEIVED){
- //如果是收到的消息,则显示左边消息布局,将右边消息布局隐藏
- viewHolder.leftLayout.setVisibility(View.VISIBLE);
- viewHolder.rightLayout.setVisibility(View.GONE);
- viewHolder.leftMsg.setText(msg.getContent());
- }else if(msg.getType()==Msg.SENT){
- //如果是发出去的消息,显示右边布局的消息布局,将左边的消息布局隐藏
- viewHolder.rightLayout.setVisibility(View.VISIBLE);
- viewHolder.leftLayout.setVisibility(View.GONE);
- viewHolder.rightMsg.setText(msg.getContent());
- }
- return view;
- }
- class ViewHolder{
- LinearLayout leftLayout;
- LinearLayout rightLayout;
- TextView leftMsg;
- TextView rightMsg;
- }
- }
- }
![](http://static.blog.csdn.net/images/save_snippets.png)
package com.example.chat_layout;import java.util.ArrayList;
import java.util.List;import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;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);initMsg();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(){@Overridepublic void onClick(View v) {String content = inputText.getText().toString();if(!"".equals(content)){Msg msg = new Msg(content, Msg.SENT);msgList.add(msg);adapter.notifyDataSetChanged();//有新消息时,刷新ListView中的显示msgListView.setSelection(msgList.size());//将ListView定位到最后一行inputText.setText("");//清空输入框的内容}}});}private void initMsg() {Msg msg1 = new Msg("I miss you!",Msg.RECEIVED);msgList.add(msg1);Msg msg2 = new Msg("I miss you,too!",Msg.SENT);msgList.add(msg2);Msg msg3 = new Msg("I will come back soon!",Msg.RECEIVED);msgList.add(msg3);}@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;}public class Msg{public static final int RECEIVED = 0;//收到一条消息public static final int 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;}}public class MsgAdapter extends ArrayAdapter<Msg>{private int resourceId;public MsgAdapter(Context context, int textViewresourceId, List<Msg> objects) {super(context, textViewresourceId, objects);resourceId = textViewresourceId;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {Msg msg = getItem(position);View view;ViewHolder viewHolder;if(convertView == null){view = LayoutInflater.from(getContext()).inflate(resourceId, null);viewHolder = new ViewHolder();viewHolder.leftLayout = (LinearLayout)view.findViewById(R.id.left_layout);viewHolder.rightLayout = (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 = convertView;viewHolder = (ViewHolder) view.getTag();}if(msg.getType()==Msg.RECEIVED){//如果是收到的消息,则显示左边消息布局,将右边消息布局隐藏viewHolder.leftLayout.setVisibility(View.VISIBLE);viewHolder.rightLayout.setVisibility(View.GONE);viewHolder.leftMsg.setText(msg.getContent());}else if(msg.getType()==Msg.SENT){//如果是发出去的消息,显示右边布局的消息布局,将左边的消息布局隐藏viewHolder.rightLayout.setVisibility(View.VISIBLE);viewHolder.leftLayout.setVisibility(View.GONE);viewHolder.rightMsg.setText(msg.getContent());}return view;}class ViewHolder{LinearLayout leftLayout;LinearLayout rightLayout;TextView leftMsg;TextView rightMsg;}}}
最后效果图:可以看到除了程序中初始化的3条消息,ok这是通过底下编辑器输入的,看一看到实现了聊天对话框的基本内容和要求。
Android仿微信聊天界面布局相关推荐
- android高仿微信下拉有页面,Android——(仿微信聊天界面布局实例)
今天看郭霖<第一行代码>书上写了一个聊天窗体的小例子,自己就练习学了一下.加上一些自己的理解整理了一下. 1.第一步首先是制作9.patch图片,这个在android sdk 目录下to ...
- android 仿微信聊天界面 以及语音录制功能,Android仿微信录制语音功能
本文实例为大家分享了Android仿微信录制语音的具体代码,供大家参考,具体内容如下 前言 我把录音分成了两部分 1.UI界面,弹窗读秒 2.一个类(包含开始.停止.创建文件名功能) 第一部分 由于6 ...
- Android仿微信聊天界面
今天说说android的仿微信聊天界面,我只想说两个字:坑爹 项目已经传到了github: https://github.com/hebiao6446/Hantu-android- 还好我写过iOS仿 ...
- android模拟微信聊天功能,android仿微信聊天界面 语音录制功能
本例为模仿微信聊天界面UI设计,文字发送以及语言录制UI. 1先看效果图: 第一:chat.xml设计 android:layout_width="fill_parent" and ...
- Android ListView仿微信聊天界面
这篇文章主要为大家详细介绍了ListView仿微信聊天界面,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 Android ListView仿聊天界面效果图的具体代码,供 ...
- android仿微信聊天功能,Android高仿微信聊天界面代码分享
微信聊天现在非常火,是因其界面漂亮吗,哈哈,也许吧.微信每条消息都带有一个气泡,非常迷人,看起来感觉实现起来非常难,其实并不难.下面小编给大家分享实现代码. 先给大家展示下实现效果图: OK,下面我们 ...
- android 微信高仿,Android高仿微信聊天界面代码分享
微信聊天现在非常火,是因其界面漂亮吗,哈哈,也许吧.微信每条消息都带有一个气泡,非常迷人,看起来感觉实现起来非常难,其实并不难.下面小编给大家分享实现代码. 先给大家展示下实现效果图: OK,下面我们 ...
- #解决仿微信聊天界面键盘遮盖聊天的界面
解决仿微信聊天界面键盘遮盖聊天的界面 注意聊天界面的activity一定不能处于全屏模式 //第一步设置属性 <!--模拟的布局文件--><?xml version="1. ...
- h5聊天室案例|仿微信聊天界面|多人群聊
html5仿微信聊天室weChatRoom案例|多人群聊互动|仿微信聊天界面 该项目是使用html5+css3+zepto+swiper+wcPop等技术开发的仿微信聊天室,捣鼓调试了很长时间,在手机 ...
最新文章
- OpenCV error: Cannot load info library for OpenCV
- Eclipse:xml文件中添加.xsd约束文件
- adb和adb shell
- python 报错 UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xd3 in position 解决方法
- mysql开机自启动设置
- iBeacon的第一篇(基于Swift实现)
- Linux系统初学者的常见问题解决集结
- 使用Xcode打包上传APP
- android程序员简历模板
- CAN总线通信协议详讲
- 确定有限自动机DFA和非确定有限自动机NFA
- 建模计算机处理器,实战建模渲染,用锐龙7 5800X拒绝拖稿
- STM32-点亮LED
- (附源码)SSM网络故障报修系统 毕业设计 291146
- 图片不超过200kb怎么调整?一分钟学会图片压缩到指定大小
- 使用gensim框架及Word2Vec词向量模型获取相似词
- 计算机输入出设备课件,《电脑输入设备》PPT课件.ppt
- 【iOS】—— ARC学习
- 九、系统的软中断导致CPU使用率升高,我该怎么办?
- 安卓恶意锁屏APP分析