聊天界面

本文详细介绍了一个简单的聊天界面UI设计过程,包括布局XML代码、消息显示样式、头像添加等步骤,实现了类似微信的聊天界面。

学了很多的ui的知识,这里就来实现个聊天的界面,首先来实现个layout的xml,代码如下:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical">  
  7.   
  8.     <ListView  
  9.         android:id="@+id/msg_list_view"  
  10.         android:layout_height="0dp"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_weight="1"  
  13.         android:divider="#0000"/>  
  14.   
  15.     <LinearLayout  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content">  
  18.         <EditText  
  19.             android:id="@+id/input_text"  
  20.             android:layout_height="wrap_content"  
  21.             android:layout_width="0dp"  
  22.             android:layout_weight="1"  
  23.             android:hint="输入你想说的话"  
  24.             android:maxLines="2"/>  
  25.         <Button  
  26.             android:id="@+id/send"  
  27.             android:layout_height="wrap_content"  
  28.             android:layout_width="wrap_content"  
  29.             android:text="发送"/>  
  30.   
  31.     </LinearLayout>  
  32.   
  33. </LinearLayout>  

    这里主要是listview来实现聊天的记录显示,edittext用于输入信息,button用于按钮发送。效果如下:


    然后实现个listView的界面,这里用到了两张准备好的图片,是*.9.png,也就是9宫格的图片,主要的作用就是为了防止不该拉伸的地方拉伸,这里的界面用到了气泡,所以只要拉伸中间的背景就好了,两边的就不要拉伸了,新建msg_item.xml,代码如下:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     android:padding="10dp">  
  7.   
  8.     <LinearLayout  
  9.         android:id="@+id/left_layout"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_gravity="left"  
  13.         android:background="@drawable/left_messages">  
  14.   
  15.         <TextView  
  16.             android:id="@+id/left_msg"  
  17.             android:layout_width="wrap_content"  
  18.             android:layout_height="wrap_content"  
  19.             android:layout_gravity="center"  
  20.             android:layout_margin="10dp"  
  21.             android:textColor="#fff"/>  
  22.   
  23.     </LinearLayout>  
  24.   
  25.     <LinearLayout  
  26.         android:id="@+id/right_layout"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_width="wrap_content"  
  29.         android:layout_gravity="right"  
  30.         android:background="@drawable/right_messages">  
  31.   
  32.         <TextView  
  33.             android:id="@+id/right_msg"  
  34.             android:layout_width="wrap_content"  
  35.             android:layout_height="wrap_content"  
  36.             android:layout_gravity="center"  
  37.             android:layout_margin="10dp"/>  
  38.   
  39.     </LinearLayout>  
  40.   
  41. </LinearLayout>  
    效果如下图所示:



    这里的图片是网上找的png,然后ps改变大小,最后在android studio中,右击选择新建9-patch图片,然后选择拉伸的位置,如下图所示:


    这样,绿色显示的是整个背景图片,然后中间的红色就是可以拉伸的地方。其中相对于png图片,*.9.png图片主要多了四周的黑线条。这里不再详细讲解*.9.png图片的制作了。

    接着新建Msg类,代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.jared.uitest;  
  2.   
  3. /** 
  4.  * Created by jared on 16/2/10. 
  5.  */  
  6. public class Msg {  
  7.     public static final int TYPE_RECEIVED = 0;  
  8.     public static final int TYPE_SEND = 1;  
  9.   
  10.     private String content;  
  11.     private int type;  
  12.   
  13.     public Msg(String content, int type) {  
  14.         this.content = content;  
  15.         this.type = type;  
  16.     }  
  17.   
  18.     public String getContent() {  
  19.         return content;  
  20.     }  
  21.   
  22.     public int getType() {  
  23.         return type;  
  24.     }  
  25. }  

    然后就是Adapter了,MsgAdapter代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.jared.uitest;  
  2.   
  3. import android.content.Context;  
  4. import android.view.LayoutInflater;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.ArrayAdapter;  
  8. import android.widget.LinearLayout;  
  9. import android.widget.TextView;  
  10.   
  11. import java.util.List;  
  12.   
  13. /** 
  14.  * Created by jared on 16/2/10. 
  15.  */  
  16. public class MsgAdapter extends ArrayAdapter<Msg> {  
  17.     private int resourceId;  
  18.   
  19.     public MsgAdapter(Context context, int textViewResourceId, List<Msg> objects) {  
  20.         super(context, textViewResourceId, objects);  
  21.         resourceId = textViewResourceId;  
  22.     }  
  23.   
  24.     public View getView(int position, View convertView, ViewGroup parent) {  
  25.         Msg msg = getItem(position);  
  26.         View view;  
  27.         ViewHolder viewHolder;  
  28.         if(convertView == null) {  
  29.             view = LayoutInflater.from(getContext()).inflate(resourceId, null);  
  30.             viewHolder = new ViewHolder();  
  31.             viewHolder.leftLayout = (LinearLayout)view.findViewById(R.id.left_layout);  
  32.             viewHolder.rightLayout = (LinearLayout)view.findViewById(R.id.right_layout);  
  33.             viewHolder.leftMsg = (TextView)view.findViewById(R.id.left_msg);  
  34.             viewHolder.rightMsg = (TextView)view.findViewById(R.id.right_msg);  
  35.             view.setTag(viewHolder);  
  36.         } else {  
  37.             view = convertView;  
  38.             viewHolder = (ViewHolder) view.getTag();  
  39.         }  
  40.         if(msg.getType() == Msg.TYPE_RECEIVED) {  
  41.             viewHolder.leftLayout.setVisibility(View.VISIBLE);  
  42.             viewHolder.rightLayout.setVisibility(View.GONE);  
  43.             viewHolder.leftMsg.setText(msg.getContent());  
  44.         } else if(msg.getType() == Msg.TYPE_SEND) {  
  45.             viewHolder.rightLayout.setVisibility(View.VISIBLE);  
  46.             viewHolder.leftLayout.setVisibility(View.GONE);  
  47.             viewHolder.rightMsg.setText(msg.getContent());  
  48.         }  
  49.         return view;  
  50.     }  
  51.   
  52.     class ViewHolder {  
  53.         LinearLayout leftLayout;  
  54.         LinearLayout rightLayout;  
  55.         TextView leftMsg;  
  56.         TextView rightMsg;  
  57.     }  
  58. }  

    最后我们来实现MainActivity的代码:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.jared.uitest;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v7.app.ActionBar;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.EditText;  
  9. import android.widget.ListView;  
  10.   
  11. import java.util.ArrayList;  
  12. import java.util.List;  
  13.   
  14. public class MainActivity extends AppCompatActivity {  
  15.   
  16.     private ListView msgListView;  
  17.     private EditText inputText;  
  18.     private Button send;  
  19.     private MsgAdapter adapter;  
  20.   
  21.     private List<Msg> msgList = new ArrayList<Msg>();  
  22.   
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.   
  27.         ActionBar actionBar = getSupportActionBar();  
  28.         actionBar.hide();  
  29.   
  30.         setContentView(R.layout.activity_main);  
  31.   
  32.         initMsgs();  
  33.         adapter = new MsgAdapter(MainActivity.this, R.layout.msg_item, msgList);  
  34.         inputText = (EditText)findViewById(R.id.input_text);  
  35.         send = (Button)findViewById(R.id.send);  
  36.         msgListView = (ListView)findViewById(R.id.msg_list_view);  
  37.         msgListView.setAdapter(adapter);  
  38.         send.setOnClickListener(new View.OnClickListener() {  
  39.             @Override  
  40.             public void onClick(View view) {  
  41.                 String content = inputText.getText().toString();  
  42.                 if(!"".equals(content)) {  
  43.                     Msg msg = new Msg(content, Msg.TYPE_SEND);  
  44.                     msgList.add(msg);  
  45.                     adapter.notifyDataSetChanged();  
  46.                     msgListView.setSelection(msgList.size());  
  47.                     inputText.setText("");  
  48.                 }  
  49.             }  
  50.         });  
  51.     }  
  52.   
  53.     private void initMsgs() {  
  54.         Msg msg1 = new Msg("Hello, how are you?", Msg.TYPE_RECEIVED);  
  55.         msgList.add(msg1);  
  56.         Msg msg2 = new Msg("Fine, thank you, and you?", Msg.TYPE_SEND);  
  57.         msgList.add(msg2);  
  58.         Msg msg3 = new Msg("I am fine, too!", Msg.TYPE_RECEIVED);  
  59.         msgList.add(msg3);  
  60.     }  
  61. }  

    运行看下效果,输入一些文字,然后发送:


    上面这个界面已经和微信的也有点类似了,那么下面再加上个头像,那么会不会更好呢?我们来试试。首先准备两张头像的图片,简单命名为head1,head2,这里的头像最好喝聊天背景的height像素相同,本例子是45。然后修改msg_item.xml文件,代码如下:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     android:padding="10dp">  
  7.   
  8.     <LinearLayout  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:orientation="horizontal">  
  12.   
  13.         <ImageView  
  14.             android:id="@+id/head_left"  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content"  
  17.             android:layout_gravity="left"  
  18.             android:src="@drawable/head1"/>  
  19.   
  20.         <LinearLayout  
  21.             android:id="@+id/left_layout"  
  22.             android:layout_height="wrap_content"  
  23.             android:layout_width="wrap_content"  
  24.             android:layout_gravity="left"  
  25.             android:background="@drawable/left_messages">  
  26.   
  27.             <TextView  
  28.                 android:id="@+id/left_msg"  
  29.                 android:layout_width="wrap_content"  
  30.                 android:layout_height="wrap_content"  
  31.                 android:layout_gravity="center"  
  32.                 android:layout_margin="10dp"  
  33.                 android:textColor="#fff"/>  
  34.         </LinearLayout>  
  35.     </LinearLayout>  
  36.   
  37.     <LinearLayout  
  38.         android:layout_width="wrap_content"  
  39.         android:layout_height="wrap_content"  
  40.         android:layout_gravity="right"  
  41.         android:orientation="horizontal">  
  42.   
  43.     <LinearLayout  
  44.         android:id="@+id/right_layout"  
  45.         android:layout_height="wrap_content"  
  46.         android:layout_width="wrap_content"  
  47.         android:layout_gravity="right"  
  48.         android:background="@drawable/right_messages">  
  49.   
  50.         <TextView  
  51.             android:id="@+id/right_msg"  
  52.             android:layout_width="wrap_content"  
  53.             android:layout_height="wrap_content"  
  54.             android:layout_gravity="center"  
  55.             android:layout_margin="10dp"/>  
  56.   
  57.     </LinearLayout>  
  58.         <ImageView  
  59.             android:id="@+id/head_right"  
  60.             android:layout_width="wrap_content"  
  61.             android:layout_height="wrap_content"  
  62.             android:src="@drawable/head2"/>  
  63.     </LinearLayout>  
  64.   
  65. </LinearLayout>  

    这里主要是头像的ImageView和聊天记录的TextView需要在同一行,所以这里LinearLayout里面又一次嵌套了LinearLayout。

    然后修改MsgAdapter代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.jared.uitest;  
  2.   
  3. import android.content.Context;  
  4. import android.view.LayoutInflater;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.ArrayAdapter;  
  8. import android.widget.ImageView;  
  9. import android.widget.LinearLayout;  
  10. import android.widget.TextView;  
  11.   
  12. import java.util.List;  
  13.   
  14. /** 
  15.  * Created by jared on 16/2/10. 
  16.  */  
  17. public class MsgAdapter extends ArrayAdapter<Msg> {  
  18.     private int resourceId;  
  19.   
  20.     public MsgAdapter(Context context, int textViewResourceId, List<Msg> objects) {  
  21.         super(context, textViewResourceId, objects);  
  22.         resourceId = textViewResourceId;  
  23.     }  
  24.   
  25.     public View getView(int position, View convertView, ViewGroup parent) {  
  26.         Msg msg = getItem(position);  
  27.         View view;  
  28.         ViewHolder viewHolder;  
  29.         if(convertView == null) {  
  30.             view = LayoutInflater.from(getContext()).inflate(resourceId, null);  
  31.             viewHolder = new ViewHolder();  
  32.             viewHolder.leftLayout = (LinearLayout)view.findViewById(R.id.left_layout);  
  33.             viewHolder.rightLayout = (LinearLayout)view.findViewById(R.id.right_layout);  
  34.             viewHolder.leftMsg = (TextView)view.findViewById(R.id.left_msg);  
  35.             viewHolder.rightMsg = (TextView)view.findViewById(R.id.right_msg);  
  36.             viewHolder.head1 = (ImageView)view.findViewById(R.id.head_left);  
  37.             viewHolder.head2 = (ImageView)view.findViewById(R.id.head_right);  
  38.             view.setTag(viewHolder);  
  39.         } else {  
  40.             view = convertView;  
  41.             viewHolder = (ViewHolder) view.getTag();  
  42.         }  
  43.         if(msg.getType() == Msg.TYPE_RECEIVED) {  
  44.             viewHolder.leftLayout.setVisibility(View.VISIBLE);  
  45.             viewHolder.head1.setVisibility(View.VISIBLE);  
  46.             viewHolder.rightLayout.setVisibility(View.GONE);  
  47.             viewHolder.head2.setVisibility(View.GONE);  
  48.             viewHolder.leftMsg.setText(msg.getContent());  
  49.         } else if(msg.getType() == Msg.TYPE_SEND) {  
  50.             viewHolder.rightLayout.setVisibility(View.VISIBLE);  
  51.             viewHolder.head2.setVisibility(View.VISIBLE);  
  52.             viewHolder.leftLayout.setVisibility(View.GONE);  
  53.             viewHolder.head1.setVisibility(View.GONE);  
  54.             viewHolder.rightMsg.setText(msg.getContent());  
  55.         }  
  56.         return view;  
  57.     }  
  58.   
  59.     class ViewHolder {  
  60.         LinearLayout leftLayout;  
  61.         LinearLayout rightLayout;  
  62.         TextView leftMsg;  
  63.         TextView rightMsg;  
  64.         ImageView head1;  
  65.         ImageView head2;  
  66.     }  
  67. }  

    这里viewHolder中添加了head1和head2,然后获取资源中也添加了,并且添加了显示和隐藏的代码。

    图片,代码都准备好了,那就运行看看效果了:


    基本上和微信的聊天界面也有几分相似了。学习了这么多ui的知识,总算可以简单地开发一个界面了。ui就先学到这里,接下去继续学习别的知识吧。


一直以来都比较羡慕那些,界面美观,功能强大的即时通讯软件,自己慢慢摸索了好长时间,用MFC做了一个聊天程序主窗口演示小程序。 该程序主要自定义了CRichEditCtrl控件,使用XML技术与GDI处理各种图片及QQ表情组件,串行化数据等,使得程序拥有了一般聊天程序应用的特色(同时仿FeiQ程序的自动释放程序运行需要的资源到安装目录): 支持字体设置、图文混排、表情及各种格式图片的插入、发送与保存,聊天记录的保存等等... 1、聊天输入、输出窗口:使用CRichEditCtrl控件,扩展后方便了字体设置、图文混排、表情及各种格式图片的插入,及窗口上右键菜单的功能。 2、聊天表情:这一部分是我精心制作的,使用GDI处理各种图片使得程序支持各种格式图片的预览、插入、保存。使用XML技术,方便快捷管理表情数据。自动释放表情图片资源(程序所在目录)及QQ表情组件(放到\System32\ImageOle.dll),程序自动注册组件,让程序支持GIF格式表情的插入。程序仿FeiQ自动生成表情页面缓存图片,快捷增加大量表情。本程序生成缓存图时,使用多线程技术,增加、修改完后立即更新页面缓存图,几千张图片,马上生成完成。 3、表情管理:支持表情的添加、删除、修改,移动,导入、导出表情库等,修改完后自动删除需要更新的页面缓存图,及程序程序无关的文件等。 4、聊天记录:使用串行化数据技术,将聊天记录生成到Log.dat文件中,可分页浏览记录。 本程序本人精心制件,最近一段时间在弄一个即时通讯软件,程序马上完工,完工后立即上传,由于弄程序花了不少精力,出于私心暂时不公布了。。。。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值