美文网首页
Android06-点九图的使用,聊天窗口的搭建

Android06-点九图的使用,聊天窗口的搭建

作者: figure_ai | 来源:发表于2017-04-02 09:18 被阅读0次

    Nine_Patch图片:一种被特殊处理过的图片,能够制定哪些区域可以被拉伸,哪些区域不可以

    • 在Android Studio中使用.9图很简单:直接将图片名称以”.9.png”结束。
      使用.9图必须注意一点:文件的后缀名必须是.9.png,不能是.png或者是.9.png.png,这样的命名都会导致编译失败。
    • 命名完成后双击图片即可进行编辑
    点九图02.gif

    第一步:创建项目导入图片

    第二步:创建界面布局文件

    注:这里因为用到RecyclerView所以要先天际啊RecyclerView相关库的依赖。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#d8e0e8"
        >
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/msg_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
    
        </android.support.v7.widget.RecyclerView>
    
        <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:layout_marginBottom="0dp"
                android:hint="请输入信息"
                android:maxLines="2"/>
    
            <Button
                android:id="@+id/send"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Send"/>
        </LinearLayout>
    </LinearLayout>
    

    第三步:创建信息窗口布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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:layout_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>
    
    

    第四步:创建信息实体类

    package com.example.anwser_mac.uibestpractice;
    
    /**
     * Created by anwser_mac on 2017/4/1.
     */
    
    public class Msg {
    
        public static final int TYPE_RECEIVED = 0;
    
        public static final int TYPE_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;
        }
    }
    
    

    第五步:自定义RecyclerView适配器

    package com.example.anwser_mac.uibestpractice;
    
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    import java.util.List;
    
    /**
     * Created by anwser_mac on 2017/4/1.
     */
    
    public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.Viewholder> {
    
        private List<Msg> mMsgList;
    
        static class Viewholder extends RecyclerView.ViewHolder {
    
            android.widget.LinearLayout leftLayout;
    
            android.widget.LinearLayout rightLayout;
    
            TextView leftMsg;
    
            TextView rightMsg;
    
            public Viewholder(View view) {
                super(view);
                leftLayout = (LinearLayout) view.findViewById(R.id.left_layout);
                rightLayout = (LinearLayout) view.findViewById(R.id.right_layout);
                leftMsg = (TextView) view.findViewById(R.id.left_msg);
                rightMsg = (TextView) view.findViewById(R.id.right_msg);
            }
        }
    
    
        public MsgAdapter(List<Msg> msgList) {
            mMsgList = msgList;
        }
    
        @Override
        public MsgAdapter.Viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
            return new Viewholder(view);
        }
    
        @Override
        public void onBindViewHolder(MsgAdapter.Viewholder holder, int position) {
            Msg msg = mMsgList.get(position);
            if (msg.getType() == Msg.TYPE_RECEIVED) {
                //如果是收到的消息,则显示在左边的消息布局,将右边的消息布局隐藏
                holder.leftLayout.setVisibility(View.VISIBLE);
                holder.rightLayout.setVisibility(View.GONE);
                holder.leftMsg.setText(msg.getContent());
            } else if(msg.getType() == Msg.TYPE_SENT) {
                //如果是发出的消息,则显示在右边的消息布局,将左边的消息布局隐藏
                holder.rightLayout.setVisibility(View.VISIBLE);
                holder.leftLayout.setVisibility(View.GONE);
                holder.rightMsg.setText(msg.getContent());
            }
        }
    
        @Override
        public int getItemCount() {
            return mMsgList.size();
        }
    }
    
    

    第六步:加载数据

    package com.example.anwser_mac.uibestpractice;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
    
        private List<Msg> msgList = new ArrayList<>();
    
        private EditText inputText;
    
        private Button send;
    
        private RecyclerView msgRecyclerView;
    
        private MsgAdapter adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout);
    
            //初始化消息数据
            initMsgs();
            inputText = (EditText) findViewById(R.id.input_text);
            send = (Button) findViewById(R.id.send);
            msgRecyclerView = (RecyclerView) findViewById(R.id.msg_recycler_view);
            LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            msgRecyclerView.setLayoutManager(layoutManager);
            adapter = new MsgAdapter(msgList);
            msgRecyclerView.setAdapter(adapter);
    
            send.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String content = inputText.getText().toString();
                    if (!"".equals(content)) {
                        Msg msg = new Msg(content, Msg.TYPE_SENT);
                        msgList.add(msg);
                        //当有新消息时,刷新显示
                        adapter.notifyItemInserted(msgList.size()-1);
                        //定位到最后一行
                        msgRecyclerView.scrollToPosition(msgList.size()-1);
                        //清空输入框的内容
                        inputText.setText("");
                    }
                }
            });
    
        }
    
    
        private void initMsgs() {
            Msg msg1 = new Msg("很高兴认识你", Msg.TYPE_RECEIVED);
            msgList.add(msg1);
            Msg msg2 = new Msg("你高兴得太早了", Msg.TYPE_SENT);
            msgList.add(msg2);
            Msg msg3 = new Msg("你丑开", Msg.TYPE_RECEIVED);
            msgList.add(msg3);
        }
    }
    
    
    点九图使用实例.gif

    相关文章

      网友评论

          本文标题:Android06-点九图的使用,聊天窗口的搭建

          本文链接:https://www.haomeiwen.com/subject/xaxwottx.html