Android仿微信聊天界面布局
来源:互联网 发布:用java写九九乘法表 编辑:程序博客网 时间:2024/05/20 16:13
在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>
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>
效果图如下:
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;
- }
- }
- }
- Android仿微信聊天界面布局
- Android——(仿微信聊天界面布局实例)
- android 仿微信聊天界面
- android仿微信聊天界面
- Android仿微信聊天界面
- 即时聊天 界面布局
- Android 仿微信QQ聊天界面
- Android 仿微信QQ聊天界面
- Android仿微信语音聊天界面
- Android仿微信语音聊天界面
- 【android】软键盘 - 仿 qq/微信 聊天界面布局
- Android即时通讯聊天界面的通用定义XML布局
- Android 实现聊天界面
- Android 聊天界面适配器
- Android UI-聊天界面
- Android UI-聊天界面
- Android UI-聊天界面
- Android 聊天界面对话
- 软素质学堂
- 九度OJ学习笔记 题目1070
- 新手上路
- mybatis延迟加载
- Django入门探索(二)
- Android仿微信聊天界面布局
- 数组填充 fillArray
- Spring学习(1)------初识Spring
- Source Insight函数调用关系显示设置
- UITabview之用法
- Android LayoutInflater 源码解析
- android中string.xml中%1$s、%1$d等的用法
- 我用的是jdk1.7,但switch传string的参数报错
- Mysql 优化之 Explain 执行计划