Android学习随笔(9)------界面实践

来源:互联网 发布:windows svn配置钩子 编辑:程序博客网 时间:2024/04/29 05:00

学习流程来自《第一行代码》(第二版)
利用整一章所学习的控件,布局等知识来实现一个聊天界面的练习。

制作所需图片

Nine-Patch图片(可以指定哪些区域可以被拉伸)
Exler
这是未经过修改的png。
显示效果是这样的 :
Exler
可以看到整个图片的width被均匀拉伸了,这肯定是不能使用的。
就需要利用Nine-Patch图片来应对我们的需求。
在Android SDK目录下有一个tools文件夹,打开其中的draw9patch.bat文件。(需要jdk/bin配置到环境变量中,若是Android Studio的内置jdk 则需Android Studio 安装目录/jre/bin)。
如果没有在SDK/tools下找到这个文件的话可以直接在Android Studio中右击png的图片会出现Create 9 Patch file,也可进入编辑
Exler
在需要拉伸的地方加上黑点。
Exler

Exler
Exler
最后的制作出来的图片就是这样子的。
显示效果 :
Exler
可以看到我们加点的地方被拉伸,而不加点的地方都维持了原样。

编写

导入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"/>    <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="Type something here"            android:maxLines="2"/>        <Button            android:id="@+id/send"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Send"/>    </LinearLayout></LinearLayout>

利用RecyclerView显示发送与接受的消息
利用一个横向的线性布局显示文本编辑框和发送按钮,用于发送消息。
定义一个消息实体类Msg.java :

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;    }}

编写item的布局 :

<?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_horizontal"            android:layout_margin="10dp"            android:textColor="#fff" />    </LinearLayout></LinearLayout>

把之前做好的图片作为布局的背景,让后在布局之中添加TextView显示文字,消息框就能随着文字的大小而自动拉伸。到时候判断是收到消息还是发出消息,把另一个布局隐藏,显示出我们要的消息框就可以了。

编写RecyclerView的适配器itemAdapter.java :

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {    private List<Msg> mMsgList;    static class ViewHolder extends RecyclerView.ViewHolder {        LinearLayout rightLayout;        LinearLayout leftLayout;        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 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(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();    }}

MainActivity.java :

    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        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);    // 当有新消息时,刷新ListView中的显示                    msgRecyclerView.scrollToPosition(msgList.size() - 1);    // 将ListView定位到最后一行                    inputText.setText("");    // 清空输入框中的内容                }            }        });    }    private void initMsgs() {        Msg msg1 = new Msg("Hello guy.", Msg.TYPE_RECEIVED);        msgList.add(msg1);        Msg msg2 = new Msg("Hello. Who is that?", Msg.TYPE_SENT);        msgList.add(msg2);        Msg msg3 = new Msg("This is Tom. Nice talking to you. ", Msg.TYPE_RECEIVED);        msgList.add(msg3);        Log.d("admin",msgList.get(msgList.size() - 1).getContent());    }

完成对RecyclerView的配置,以及编写发送按钮的逻辑代码。
运行效果 :
Exler


此博文为个人学习笔记,仅供个人学习使用,希望对大家有帮助。

原创粉丝点击