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

今天药忘吃喽~ 2022-06-06 09:15 248阅读 0赞

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

制作所需图片

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的依赖库
编写主界面 :

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <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" >
  3. <android.support.v7.widget.RecyclerView android:id="@+id/msg_recycler_view" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/>
  4. <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
  5. <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"/>
  6. <Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send"/>
  7. </LinearLayout>
  8. </LinearLayout>

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

  1. public class Msg {
  2. public static final int TYPE_RECEIVED = 0; // 消息类型为接收
  3. public static final int TYPE_SENT = 1; // 发送
  4. private String content; // 消息内容
  5. private int type; // 消息类型
  6. public Msg(String content, int type) {
  7. this.content = content;
  8. this.type = type;
  9. }
  10. public String getContent() {
  11. return content;
  12. }
  13. public int getType() {
  14. return type;
  15. }
  16. }

编写item的布局 :

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <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" >
  3. <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">
  4. <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" />
  5. </LinearLayout>
  6. <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">
  7. <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" />
  8. </LinearLayout>
  9. </LinearLayout>

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

编写RecyclerView的适配器itemAdapter.java :

  1. public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
  2. private List<Msg> mMsgList;
  3. static class ViewHolder extends RecyclerView.ViewHolder {
  4. LinearLayout rightLayout;
  5. LinearLayout leftLayout;
  6. TextView leftMsg;
  7. TextView rightMsg;
  8. public ViewHolder(View view) {
  9. super(view);
  10. leftLayout = (LinearLayout) view.findViewById(R.id.left_layout);
  11. rightLayout = (LinearLayout) view.findViewById(R.id.right_layout);
  12. leftMsg = (TextView) view.findViewById(R.id.left_msg);
  13. rightMsg = (TextView) view.findViewById(R.id.right_msg);
  14. }
  15. }
  16. public MsgAdapter(List<Msg> msgList) {
  17. mMsgList = msgList;
  18. }
  19. @Override
  20. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  21. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item, parent, false);
  22. return new ViewHolder(view);
  23. }
  24. @Override
  25. public void onBindViewHolder(ViewHolder holder, int position) {
  26. Msg msg = mMsgList.get(position);
  27. if (msg.getType() == Msg.TYPE_RECEIVED) { // 如果是收到的消息,显示左边消息布局,将右边隐藏
  28. holder.leftLayout.setVisibility(View.VISIBLE);
  29. holder.rightLayout.setVisibility(View.GONE);
  30. holder.leftMsg.setText(msg.getContent());
  31. } else if (msg.getType() == Msg.TYPE_SENT) { // 如果是发送的消息,显示右边,隐藏左边
  32. holder.rightLayout.setVisibility(View.VISIBLE);
  33. holder.leftLayout.setVisibility(View.GONE);
  34. holder.rightMsg.setText(msg.getContent());
  35. }
  36. }
  37. @Override
  38. public int getItemCount() {
  39. return mMsgList.size();
  40. }
  41. }

MainActivity.java :

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_main);
  5. initMsgs(); // 初始化消息数据
  6. inputText = (EditText) findViewById(R.id.input_text);
  7. send = (Button) findViewById(R.id.send);
  8. msgRecyclerView = (RecyclerView) findViewById(R.id.msg_recycler_view);
  9. LinearLayoutManager layoutManager = new LinearLayoutManager(this);
  10. msgRecyclerView.setLayoutManager(layoutManager);
  11. adapter = new MsgAdapter(msgList);
  12. msgRecyclerView.setAdapter(adapter);
  13. send.setOnClickListener(new View.OnClickListener() {
  14. @Override
  15. public void onClick(View v) {
  16. String content = inputText.getText().toString();
  17. if (!"".equals(content)) {
  18. Msg msg = new Msg(content, Msg.TYPE_SENT);
  19. msgList.add(msg);
  20. adapter.notifyItemInserted(msgList.size()-1); // 当有新消息时,刷新ListView中的显示
  21. msgRecyclerView.scrollToPosition(msgList.size() - 1); // 将ListView定位到最后一行
  22. inputText.setText(""); // 清空输入框中的内容
  23. }
  24. }
  25. });
  26. }
  27. private void initMsgs() {
  28. Msg msg1 = new Msg("Hello guy.", Msg.TYPE_RECEIVED);
  29. msgList.add(msg1);
  30. Msg msg2 = new Msg("Hello. Who is that?", Msg.TYPE_SENT);
  31. msgList.add(msg2);
  32. Msg msg3 = new Msg("This is Tom. Nice talking to you. ", Msg.TYPE_RECEIVED);
  33. msgList.add(msg3);
  34. Log.d("admin",msgList.get(msgList.size() - 1).getContent());
  35. }

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


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

发表评论

表情:
评论列表 (有 0 条评论,248人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android学习随笔(1)

    学习流程来自《第一行代码》(第二版) 最近开始了Android的学习,看到很多人都推荐这一本书,就决定按照这一本书的讲解流程熟悉一下Android。 环境配置 ![