当前位置:   article > 正文

AS-制作简易微信界面

AS-制作简易微信界面

#设计目标

利用 android studio 制作一个类微信页面的框架,包含上中下布局,四个 tab 页面,点击底部按钮实现跳转功能,同时需要在任意一个 tab 页中实现列表效果。

#开发技术

  1. Android Studio
  2. Pixel 3a API 34
  3. activity
  4. xml
  5. fragment

#框架实现及代码

1、将botton所需的图片导入drawable目录下

2、top栏设计

代码如下:

  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. <TextView
  7. android:id="@+id/textView"
  8. android:layout_width="wrap_content"
  9. android:layout_height="55dp"
  10. android:layout_weight="1"
  11. android:gravity="center"
  12. android:text="微信"
  13. android:textColor="@color/black"
  14. android:textSize="30sp" />
  15. </LinearLayout>

3、bottom栏设计

代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="horizontal"
  7. >
  8. <LinearLayout
  9. android:id="@+id/talk"
  10. android:layout_width="58dp"
  11. android:layout_height="121dp"
  12. android:layout_weight="1"
  13. android:gravity="center"
  14. android:orientation="vertical">
  15. <ImageView
  16. android:id="@+id/imageView1"
  17. android:layout_width="107dp"
  18. android:layout_height="233dp"
  19. android:layout_weight="1"
  20. android:src="@drawable/message" />
  21. <TextView
  22. android:id="@+id/textView1"
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:gravity="center"
  26. android:text="消息"
  27. android:textColor="@color/black"
  28. android:textSize="25sp" />
  29. </LinearLayout>
  30. <LinearLayout
  31. android:id="@+id/book"
  32. android:layout_width="46dp"
  33. android:layout_height="121dp"
  34. android:layout_weight="1"
  35. android:gravity="center"
  36. android:orientation="vertical">
  37. <ImageView
  38. android:id="@+id/imageView2"
  39. android:layout_width="match_parent"
  40. android:layout_height="261dp"
  41. android:layout_weight="1"
  42. android:src="@drawable/tele"/>
  43. <TextView
  44. android:id="@+id/textView2"
  45. android:layout_width="match_parent"
  46. android:layout_height="wrap_content"
  47. android:gravity="center"
  48. android:text="通讯录"
  49. android:textColor="@color/black"
  50. android:textSize="25sp" />
  51. </LinearLayout>
  52. <LinearLayout
  53. android:id="@+id/look"
  54. android:layout_width="60dp"
  55. android:layout_height="123dp"
  56. android:layout_weight="1"
  57. android:gravity="center"
  58. android:orientation="vertical">
  59. <ImageView
  60. android:id="@+id/imageView3"
  61. android:layout_width="match_parent"
  62. android:layout_height="261dp"
  63. android:layout_weight="1"
  64. android:src="@drawable/discover" />
  65. <TextView
  66. android:id="@+id/textView3"
  67. android:layout_width="match_parent"
  68. android:layout_height="wrap_content"
  69. android:gravity="center"
  70. android:text="发现"
  71. android:textColor="@color/black"
  72. android:textSize="25sp" />
  73. </LinearLayout>
  74. <LinearLayout
  75. android:id="@+id/me"
  76. android:layout_width="49dp"
  77. android:layout_height="121dp"
  78. android:layout_weight="1"
  79. android:gravity="center"
  80. android:orientation="vertical">
  81. <ImageView
  82. android:id="@+id/imageView5"
  83. android:layout_width="wrap_content"
  84. android:layout_height="wrap_content"
  85. android:layout_weight="1"
  86. app:srcCompat="@drawable/me" />
  87. <TextView
  88. android:id="@+id/textView4"
  89. android:layout_width="match_parent"
  90. android:layout_height="wrap_content"
  91. android:gravity="center"
  92. android:text="我"
  93. android:textColor="@color/black"
  94. android:textSize="25sp" />
  95. </LinearLayout>
  96. </LinearLayout>

4、tab页面设计

用户点击时分别显示的四个页面的设计大致相同,如图

代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".BlankFragment1">
  7. <TextView
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:gravity="center"
  11. android:text="这是微信聊天界面"
  12. android:textSize="35sp" />
  13. </FrameLayout>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".BlankFragment2">
  7. <TextView
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:gravity="center"
  11. android:text="这是通讯录界面"
  12. android:textSize="35sp" />
  13. </FrameLayout>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".BlankFragment3">
  7. <TextView
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:gravity="center"
  11. android:text="这是发现界面"
  12. android:textSize="35sp" />
  13. </FrameLayout>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".BlankFragment4">
  7. <TextView
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:gravity="center"
  11. android:text="这是我的界面"
  12. android:textSize="35sp"/>
  13. </FrameLayout>

5、activity-main

通过activity-main文件将top以及bottom文件结合到一起。

页面如图:

代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:orientation="vertical">
  12. <include
  13. layout="@layout/top"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content" />
  16. <FrameLayout
  17. android:id="@+id/content"
  18. android:layout_width="415dp"
  19. android:layout_height="wrap_content"
  20. android:layout_weight="1"/>
  21. <include
  22. layout="@layout/bottom"
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content" />
  25. </LinearLayout>
  26. </androidx.constraintlayout.widget.ConstraintLayout>

6、创建fragment

需要运用fragmentmanager 来对四个不同的 tab 进行控制,让他们叠加在一起的同时,实现在不同的点击动作发生时进行一个 tab 的显示及其余三个的隐藏,以此达到点击切换的效果。由于有四个事件,因此我们需要创建三个fragment。

代码如下:

BlankFragment1

  1. package com.example.myapplication;
  2. import android.os.Bundle;
  3. import androidx.fragment.app.Fragment;
  4. import androidx.recyclerview.widget.LinearLayoutManager;
  5. import androidx.recyclerview.widget.RecyclerView;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. public class BlankFragment1 extends Fragment {
  12. private View view;
  13. private Myadapter myadapter;
  14. private RecyclerView recyclerView;
  15. private List<String> list1;
  16. @Override
  17. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  18. {
  19. view = inflater.inflate(R.layout.activity_recycleview, container, false);
  20. recyclerView = view.findViewById(R.id.recyclerview);
  21. list1 = new ArrayList<>();
  22. for (int i= 0; i < 9; i++)
  23. {
  24. list1.add("这是第" + i + "行数据");
  25. }
  26. myadapter = new Myadapter(view.getContext(), list1);
  27. LinearLayoutManager manager = new LinearLayoutManager(view.getContext());
  28. manager.setOrientation(LinearLayoutManager.VERTICAL);
  29. recyclerView.setLayoutManager(manager);
  30. recyclerView.setAdapter(myadapter);
  31. return view;
  32. }
  33. }

BlankFragment2

  1. package com.example.myapplication;
  2. import android.content.Context;
  3. import android.os.Bundle;
  4. import androidx.fragment.app.Fragment;
  5. import androidx.recyclerview.widget.RecyclerView;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import java.util.List;
  10. public class BlankFragment2 extends Fragment {
  11. private RecyclerView recyclerView;
  12. private List<String> list;
  13. private Context context;
  14. private Myadapter myadapter;
  15. @Override
  16. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  17. Bundle savedInstanceState) {
  18. View view=inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
  19. context=view.getContext();
  20. // Inflate the layout for this fragment
  21. return inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
  22. }
  23. }

BlankFragment3

  1. package com.example.myapplication;
  2. import android.os.Bundle;
  3. import androidx.fragment.app.Fragment;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. public class BlankFragment3 extends Fragment {
  8. @Override
  9. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  10. Bundle savedInstanceState) {
  11. // Inflate the layout for this fragment
  12. return inflater.inflate(R.layout.fragment_blank_fragment3, container, false);
  13. }
  14. }

BlankFragment4

  1. package com.example.myapplication;
  2. import android.os.Bundle;
  3. import androidx.fragment.app.Fragment;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. public class BlankFragment4 extends Fragment {
  8. @Override
  9. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  10. Bundle savedInstanceState) {
  11. // Inflate the layout for this fragment
  12. return inflater.inflate(R.layout.fragment_blank_fragment4, container, false);
  13. }
  14. }

7、MainActivity

主函数主要实现功能是监听onlick、初始化initial、隐藏四个tab界面fragmentHide、展示一个tab界面showfragment。

#onlick函数

  1. public void onClick(View view) {
  2. fragmentHide();
  3. int id = view.getId();
  4. if (id == R.id.talk) {
  5. showfragment(fragment1);
  6. } else if (id == R.id.book) {
  7. showfragment(fragment2);
  8. } else if (id == R.id.look) {
  9. showfragment(fragment3);
  10. } else {
  11. showfragment(fragment4);
  12. }
  13. }

#initial函数

  1. public void inital(){
  2. transaction=manager.beginTransaction()
  3. .add(R.id.content,fragment1)
  4. .add(R.id.content,fragment2)
  5. .add(R.id.content,fragment3)
  6. .add(R.id.content,fragment4)
  7. .commit();
  8. }

#fragmentHide函数

  1. public void fragmentHide(){
  2. transaction=manager.beginTransaction()
  3. .hide(fragment1)
  4. .hide(fragment2)
  5. .hide(fragment3)
  6. .hide(fragment4)
  7. .commit();
  8. }

#showfragment函数

  1. private void showfragment(Fragment fragment) {
  2. transaction=manager.beginTransaction()
  3. .show(fragment)
  4. .commit();
  5. }

#MainActivity.java完整代码

  1. package com.example.myapplication;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import androidx.fragment.app.Fragment;
  4. import androidx.fragment.app.FragmentManager;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.LinearLayout;
  8. public class MainActivity extends AppCompatActivity implements View.OnClickListener
  9. {
  10. LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4;
  11. Fragment fragment1,fragment2,fragment3,fragment4;
  12. FragmentManager manager;
  13. int transaction;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState)
  16. {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.mainlayout);
  19. linearLayout1=findViewById(R.id.talk);
  20. linearLayout2=findViewById(R.id.book);
  21. linearLayout3=findViewById(R.id.look);
  22. linearLayout4=findViewById(R.id.me);
  23. manager=getSupportFragmentManager();
  24. fragment1=new BlankFragment1();
  25. fragment2=new BlankFragment2();
  26. fragment3=new BlankFragment3();
  27. fragment4=new BlankFragment4();
  28. inital();
  29. fragmentHide();
  30. showfragment(fragment1);
  31. linearLayout1.setOnClickListener(this);
  32. linearLayout2.setOnClickListener(this);
  33. linearLayout3.setOnClickListener(this);
  34. linearLayout4.setOnClickListener(this);
  35. }
  36. public void onClick(View view) {
  37. fragmentHide();
  38. int id = view.getId();
  39. if (id == R.id.talk) {
  40. showfragment(fragment1);
  41. } else if (id == R.id.book) {
  42. showfragment(fragment2);
  43. } else if (id == R.id.look) {
  44. showfragment(fragment3);
  45. } else {
  46. showfragment(fragment4);
  47. }
  48. }
  49. private void showfragment(Fragment fragment) {
  50. transaction=manager.beginTransaction()
  51. .show(fragment)
  52. .commit();
  53. }
  54. public void inital(){
  55. transaction=manager.beginTransaction()
  56. .add(R.id.content,fragment1)
  57. .add(R.id.content,fragment2)
  58. .add(R.id.content,fragment3)
  59. .add(R.id.content,fragment4)
  60. .commit();
  61. }
  62. public void fragmentHide(){
  63. transaction=manager.beginTransaction()
  64. .hide(fragment1)
  65. .hide(fragment2)
  66. .hide(fragment3)
  67. .hide(fragment4)
  68. .commit();
  69. }
  70. }

8、recycleview的实现

在layout下新建一个item.xml,新建一个textview

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content">
  5. <TextView
  6. android:id="@+id/textView1"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:layout_weight="1"
  10. android:text="TextView" />
  11. </LinearLayout>

在BlankFragment1初始化定义变量 recyclerView,list,context, myadapter,然后onCreateView()函数底下写入适配器需要的一些参数和数据。

代码具体为BlankFragment1中的代码。

#创建Myadapter类

初始化变量,创造构造函数。

  1. package com.example.myapplication;
  2. import android.content.Context;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.TextView;
  7. import androidx.annotation.NonNull;
  8. import androidx.recyclerview.widget.RecyclerView;
  9. import java.util.List;
  10. public class Myadapter extends RecyclerView.Adapter <Myadapter.Myholder> {
  11. Context context1;
  12. List<String> list1;
  13. public Myadapter(Context context,List list) {
  14. context1=context;
  15. list1=list;
  16. }
  17. @NonNull
  18. @Override
  19. public Myholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  20. View view=LayoutInflater.from(context1).inflate(R.layout.item,parent,false);
  21. Myholder myholder=new Myholder(view);
  22. return myholder;
  23. }
  24. @Override
  25. public void onBindViewHolder(@NonNull Myholder holder, int position) {
  26. holder.textView.setText(list1.get(position));
  27. }
  28. @Override
  29. public int getItemCount() {
  30. return list1.size();
  31. }
  32. public class Myholder extends RecyclerView.ViewHolder {
  33. TextView textView;
  34. public Myholder (@NonNull View itemView) {
  35. super(itemView);
  36. textView=itemView.findViewById(R.id.textView1);
  37. }
  38. }
  39. }

9、结果展示

 

10、总结

在本次实验中,我学会利用 AS 进行制作简易的微信界面,掌握 fragment,layout,xml 和相关控件的知识,实现页面的跳转功能,和 recycleview 实现列表功能,在实验中我在一直被switch-case无法运行困扰,在查询资料后我发现用if-else就可以解决,并且AS可以便捷的实现两者的相互转换;在老师的帮助下解决了版本不兼容的问题,成功实现代码的运行;由于只能在recycleview里显示一行数据,最后发现在item里要将match_parent改为wrap_content。

11、源代码来源

fuzengyu/fzy (github.com)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/676368
推荐阅读
相关标签
  

闽ICP备14008679号