当前位置:   article > 正文

android studio——设计简单微信界面超详细教程_android studio做微信聊天界面

android studio做微信聊天界面

一、作业目标

本次作业开发的是微信APP的门户框架,UI布局为上中下结构。

功能一:用户可通过点击底部导航栏切换四个板块进行切换,它们分别是“聊天”、“联系人”、“功能”、“我的”,每切换一个界面会有对应的文本提示。

功能二:在每一tab页面实现列表效果。

(功能二的实现在功能一的前提下改进,详情可直接移步第四点的第3点)

二、设计流程

1、外观设计

微信页面整体为上-中-下结构

上部:显示栏背景为黑色,内有白色“微信”字样

中部:纯文本“这是××界面”和几行列表

下部:四个.png文件的图标以及对应板块的名字

2、内部联系

I.用一个main.xml文件将三个部分联系起来,使得三个部分可以在同一个页面进行展示

II.写一个Mainactivity.java文件来实现点击切换的功能:

(1)创建四个Fragment类及其对应的四个tab.xml文件,通过Fragmentxx类绑定板块对应的布局文件,将其联系起来;再用一个myadapter类将所有的单个文本Item.xml联系起来。

(2)在MainActivity类中创建Fragmentxx对象,通过Transaction将Fragmentxx对象添加到主布局中间的空板块,创建FragmentManager管理Fragment。

(3)初始化方法

(4)写监听方法

(5)设置控件被点击后的响应事件

(6)展示页面的方法

四、技术说明及关键代码(含详细步骤)

在这之前先了解一下各方面功能:

android:id="@+id/top",为组件设置资源id,top为命名,在java文件中通过findViewById(id)被找到。

android:layout_width=“wrap_content”,布局的宽度

android:layout_height=“wrap_content”,布局的高度

android:layout_weight=“1”,用来等比例地划分区域。

android:gravity=“center”,对齐方式。

android:text=“微信”,显示的文本。

android:textColor="@color/white",设置文字的颜色。

android:textSize=“28dp” />,设置文字的大小。

1.实现外观设计
  I.上部

        找到res下的layout文件夹,并在该文件夹下右键—>new—>XML—>Layout XML File创建一个weixin.xml文件;点击下图右上角的Design,将TextView拖一个到LinearLayout下面,再点击code查看代码,进行修改  

点击code显示的代码即为weixin.xml的代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <TextView
  6. android:id="@+id/textView3"
  7. android:layout_width="match_parent"
  8. android:layout_height="67dp"
  9. android:layout_weight="24"
  10. android:background="@color/black"
  11. android:gravity="center"
  12. android:shadowColor="@color/white"
  13. android:text="微信"
  14. android:textColor="@color/white"
  15. android:textSize="28dp" />
  16. </LinearLayout>
  II.中部

        在layout下继续右键创建一个tab1.xml文件,点击code查看代码,修改文本为“这是聊天界面”,并调整大小。

        这时候,由于共有四个界面,所以你可以选择:

       (1) 将tab1.xml直接复制,粘贴到layout下,改名为tab2.xml,tab3.xml,tab4.xml(记得把文本和code里面 android:id="@+id/textView8" 这一句TextView后的数字改成不同值);

        (2)重复三次创建tab1.xml的操作。

这里只展示tab1.xml的代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:alpha="12"
  6. android:gravity="center"
  7. android:orientation="horizontal">
  8. <TextView
  9. android:id="@+id/textView5"
  10. android:layout_width="wrap_content"
  11. android:layout_height="63dp"
  12. android:layout_weight="1"
  13. android:alpha="12"
  14. android:textSize="30sp"
  15. android:gravity="center"
  16. android:text="这是聊天界面" />
  17. </LinearLayout>
  III.下部

        首先去网上下载四个适合用来当底部导航的.png类型的图标

        然后,在layout下创建一个名为bottom.xml的文件

        点击Design—>layout—>目Linearlayout(纵向),拖一个放到横向的LinearLayout下面;

        点击common—>Imageview,拖一个放到纵向的LinearLayout下,再把TextView也放一个到纵向的LinearLayout下面,修改参数如下图一、二(上右)、三,完成一个图标的创建。

        把纵向的LinearLayout复制,在横向的LinearLayout下粘贴三次,并改名四个纵向的LinearLayout、ImageView、TextView分别编号为1,2,3,4,如图四(下右)。

bottom.xml的代码:

  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. android:orientation="horizontal">
  6. <LinearLayout
  7. android:id="@+id/LinearLayout1"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_weight="1"
  11. android:orientation="vertical">
  12. <ImageView
  13. android:id="@+id/imageView1"
  14. android:layout_width="match_parent"
  15. android:layout_height="67dp"
  16. android:src="@drawable/star1" />
  17. <TextView
  18. android:id="@+id/textView1"
  19. android:layout_width="104dp"
  20. android:layout_height="wrap_content"
  21. android:layout_weight="1"
  22. android:gravity="center"
  23. android:text="聊天"
  24. android:textSize="30sp" />
  25. </LinearLayout>
  26. <LinearLayout
  27. android:id="@+id/LinearLayout2"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_weight="1"
  31. android:orientation="vertical">
  32. <ImageView
  33. android:id="@+id/imageView2"
  34. android:layout_width="match_parent"
  35. android:layout_height="69dp"
  36. android:src="@drawable/light" />
  37. <TextView
  38. android:id="@+id/textView2"
  39. android:layout_width="104dp"
  40. android:layout_height="wrap_content"
  41. android:layout_weight="1"
  42. android:gravity="center"
  43. android:text="联系人"
  44. android:textSize="30sp" />
  45. </LinearLayout>
  46. <LinearLayout
  47. android:id="@+id/LinearLayout3"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:layout_weight="1"
  51. android:orientation="vertical">
  52. <ImageView
  53. android:id="@+id/imageView3"
  54. android:layout_width="match_parent"
  55. android:layout_height="67dp"
  56. android:src="@drawable/moon3" />
  57. <TextView
  58. android:id="@+id/textView3"
  59. android:layout_width="104dp"
  60. android:layout_height="wrap_content"
  61. android:layout_weight="1"
  62. android:gravity="center"
  63. android:text="功能"
  64. android:textSize="30sp" />
  65. </LinearLayout>
  66. <LinearLayout
  67. android:id="@+id/LinearLayout4"
  68. android:layout_width="wrap_content"
  69. android:layout_height="wrap_content"
  70. android:orientation="vertical">
  71. <ImageView
  72. android:id="@+id/imageView"
  73. android:layout_width="match_parent"
  74. android:layout_height="67dp"
  75. android:layout_weight="1"
  76. android:src="@drawable/sun" />
  77. <TextView
  78. android:id="@+id/textView4"
  79. android:layout_width="104dp"
  80. android:layout_height="wrap_content"
  81. android:layout_weight="1"
  82. android:gravity="center"
  83. android:text="我的 "
  84. android:textSize="30sp" />
  85. </LinearLayout>
  86. </LinearLayout>

        注意:选择的ImageView图标不能直接使用,所以要把之前下载的四个.png图标直接复制粘贴到res—>drawable文件下,code中的android:src="@drawable/moon3" />这一句是自己的.png路径,四个都要修改。

2.实现联系和切换功能
  I.整合页面

                先在layout下创建一个main.xml文件,点击code修改代码,把上-中-下三个部分包括在内,再点击design,调整三块的占比,使得顶部、图标和文本都能显露出来。

注意:如果不把大小调整合适,最后运行可能会导致显示不完整的。

main.xml代码:

可以看到layout="@layout/weixin",layout="@layout/content",layout="@layout/bottom"说明包含了这三个部分

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <include
  7. layout="@layout/weixin"
  8. android:layout_width="match_parent"
  9. android:layout_height="211dp" />
  10. <FrameLayout
  11. android:id="@+id/content"
  12. android:layout_width="match_parent"
  13. android:layout_height="335dp"
  14. android:layout_weight="1"></FrameLayout>
  15. <include
  16. layout="@layout/bottom"
  17. android:layout_width="match_parent"
  18. android:layout_height="146dp" />
  19. </LinearLayout>
  II.切换功能

        要实现切换功能,就需要

        (1)在java—>ui下创建四个Fragment类(.java),将其分别与之前创建的tab1,2,3,4四个.xml文件联系起来(可创建一个后复制粘贴,修改名称和tab)。

        (2)在MainActivity类(.java)中创建四个Fragment对象,四个LinearLayout对象,创建FragmentManager管理Fragment。

        (3)在MainActivity.Java中写对应的方法进行实现:

                initial();用来初始化; 

                fragmenthide();是为了点每点击一个界面,将其他三个界面隐藏起来; 

                fragmentshow(fragment1);默认展示第一个界面;

                onClick(View view);是将用户的点击进行传达,通过监听获取组件id,根据获取得到的id,展示对应的板块。

四个Fragment类只有tab的序号需要更改,这里展示Fragment1:

  1. package com.example.myapplication;
  2. import android.os.Bundle;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import androidx.fragment.app.Fragment;
  7. public class Fragment1 extends Fragment
  8. {
  9. @Override
  10. public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
  11. View view1 = new View(getContext());
  12. return inflater.inflate(R.layout.tab1, container, false);
  13. }
  14. }

MainActivity.java的代码:

  LinearLayout对象,对应的id是bottom.xml文件中的第二层LinearLayout的id

        linearLayout1=findViewById(R.id.LinearLayout1);
        linearLayout2=findViewById(R.id.LinearLayout2);
        linearLayout3=findViewById(R.id.LinearLayout3);
        linearLayout4=findViewById(R.id.LinearLayout4);
 隐藏其他项
            private void fragmenthide() {
                FragmentTransaction ft=fm.beginTransaction()
                        .hide(fragment1)
                        .hide(fragment2)
                        .hide(fragment3)
                        .hide(fragment4);
                ft.commit();
    }

 初始化,将所有fragment都添加到界面中部板块,对应的id是Activitymain.xml中的Fragment的id
            private void initial() {
                fm=getSupportFragmentManager();

用content是因为main.xml用的名字是content(android:id="@+id/content")
        FragmentTransaction ft=fm.beginTransaction()
                .add(R.id.content,fragment1) 
                .add(R.id.content,fragment2)
                .add(R.id.content,fragment3)
                .add(R.id.content,fragment4);
        ft.commit();
    }

 点击触发通过监听获取组件id,根据获取得到的id,展示对应的板块
            @Override
            public void onClick(View view){
                fragmenthide();
                int id=view.getId();
                if(id==R.id.LinearLayout1)
                    fragmentshow(fragment1);
                if(id==R.id.LinearLayout2)
                    fragmentshow(fragment2);
                if(id==R.id.LinearLayout3)
                    fragmentshow(fragment3);
                if(id==R.id.LinearLayout4)
                    fragmentshow(fragment4);
    }

完整代码:

  1. package com.example.myapplication;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.widget.LinearLayout;
  5. import com.google.android.material.bottomnavigation.BottomNavigationView;
  6. import androidx.appcompat.app.AppCompatActivity;
  7. import androidx.fragment.app.Fragment;
  8. import androidx.fragment.app.FragmentManager;
  9. import androidx.fragment.app.FragmentTransaction;
  10. import androidx.navigation.NavController;
  11. import androidx.navigation.Navigation;
  12. import androidx.navigation.ui.AppBarConfiguration;
  13. import androidx.navigation.ui.NavigationUI;
  14. import com.example.myapplication.databinding.ActivityMainBinding;
  15. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  16. Fragment fragment1,fragment2,fragment3,fragment4;
  17. FragmentManager fm;
  18. LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.main);
  23. fragment1=new Fragment1();
  24. fragment2=new Fragment2();
  25. fragment3=new Fragment3();
  26. fragment4=new Fragment4();
  27. // LinearLayout对象,对应的id是bottom.xml文件中的第二层LinearLayout的id
  28. linearLayout1=findViewById(R.id.LinearLayout1);
  29. linearLayout2=findViewById(R.id.LinearLayout2);
  30. linearLayout3=findViewById(R.id.LinearLayout3);
  31. linearLayout4=findViewById(R.id.LinearLayout4);
  32. fm=getSupportFragmentManager();
  33. initial();
  34. fragmenthide();
  35. fragmentshow(fragment1);
  36. linearLayout1.setOnClickListener(this);
  37. linearLayout2.setOnClickListener(this);
  38. linearLayout3.setOnClickListener(this);
  39. linearLayout4.setOnClickListener(this);
  40. }
  41. //隐藏其他项
  42. private void fragmenthide() {
  43. FragmentTransaction ft=fm.beginTransaction()
  44. .hide(fragment1)
  45. .hide(fragment2)
  46. .hide(fragment3)
  47. .hide(fragment4);
  48. ft.commit();
  49. }
  50. //初始化,将所有fragment都添加到界面中部板块
  51. //对应的id是Activitymain.xml中的Fragment的id
  52. private void initial() {
  53. fm=getSupportFragmentManager();
  54. //用content是因为main.xml用的名字是content(android:id="@+id/content")
  55. FragmentTransaction ft=fm.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. ft.commit();
  61. }
  62. //点击触发通过监听获取组件id,根据获取得到的id,展示对应的板块
  63. @Override
  64. public void onClick(View view){
  65. fragmenthide();
  66. int id=view.getId();
  67. if(id==R.id.LinearLayout1)
  68. fragmentshow(fragment1);
  69. if(id==R.id.LinearLayout2)
  70. fragmentshow(fragment2);
  71. if(id==R.id.LinearLayout3)
  72. fragmentshow(fragment3);
  73. if(id==R.id.LinearLayout4)
  74. fragmentshow(fragment4);
  75. }
  76. //展示
  77. private void fragmentshow(Fragment fragment){
  78. FragmentTransaction transaction=fm.beginTransaction()
  79. .show(fragment);
  80. transaction.commit();
  81. }
  82. }

        注意:onClick()方法下需要用if()语句,switch()不行。

    3.增加列表效果

  由于多了列表,首先想到要更改的就是tab1.xml,以及类Fragment1。故:

       (1)、再创建一个新的item.xml文件存放单个文本;

  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. android:gravity="center">
  6. <TextView
  7. android:id="@+id/item"
  8. android:layout_width="400dp"
  9. android:layout_height="match_parent"
  10. android:layout_gravity="center"
  11. android:layout_weight="1"
  12. android:gravity="center"
  13. android:text="TextView"
  14. android:textColor="@android:color/holo_orange_dark"
  15. android:textSize="30sp" />
  16. </LinearLayout>

       (2)、 类Fragnent1展示tab1的内容,用到recyclerView对象,修改文本内容,同时使用myadapter类和队列,让它们呈现时排列起来。

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

       (3)、对tab1进行修改,由于tab1.xml的android:id="@+id/recyclerView",被Fragment类调用后进行了修改和整合,故展示的是整体文本效果;

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:id="@+id/linearLayout"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent">
  9. <androidx.recyclerview.widget.RecyclerView
  10. android:id="@+id/recyclerView"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. app:layout_constraintBottom_toBottomOf="parent"
  14. app:layout_constraintEnd_toEndOf="parent"
  15. app:layout_constraintHorizontal_bias="0.498"
  16. app:layout_constraintStart_toStartOf="parent"
  17. app:layout_constraintTop_toTopOf="parent" />
  18. </androidx.constraintlayout.widget.ConstraintLayout>

       (4)、 Myadapter类是适配器,作用就是看到的tab1.xml的界面与数据之间的桥梁,当列表里的每一项显示到页面时,都会调用myadapter的方法返回一个View,而这里是获取view的个数,将整体展示。

  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<String> 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 holder=new Myholder(view);
  22. return holder;
  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.item);
  37. }
  38. }
  39. }

        

四、结果展示

五、其他报错原因

1.当你写了多个mainactivity时,APP—>mainifests—>Androidmainifests的code会有两个activity,这时候你需要把另一个的android:exported改为"false"。

注意:增加列表效果后,也需要修改这个文件下半部分,只留下一个activity,上半部分不变:

  1. <activity
  2. android:name=".MainActivity"
  3. android:exported="true">
  4. <intent-filter>
  5. <action android:name="android.intent.action.MAIN" />
  6. <category android:name="android.intent.category.LAUNCHER" />
  7. </intent-filter>
  8. </activity>
  9. </application>
  10. </manifest>

2、作为新手,第一次编写并运行该项目,好像需要修改文件,我运行的时候朋友帮我改了一个,我自己改了一个。在自己的这个项目的文件夹—>APP—>build.gradle.kts可以找到这个文件,可以看看是不是是不是少了这一句。

还有这个文件的最下面那两句应该是加上去的

3.system-image文件要直接能被file->settings->Android SDK直接找到,也就是说那个路径要在system-image文件的上一层

六、线上仓库地址(gitee)

luo17_world: 学习移动开发技术课程时创建 (gitee.com)

下载与安装:git下载与安装教程_git-scm.com download-CSDN博客下载与安装:

hint: Updates were rejected because the remote contains work that you do hint: not have locally.-CSDN博客

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

闽ICP备14008679号