AS制作简易微信

AS 制作简易微信

作业目标

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

技术说明

  • Android Studio

  • Pixel 3a API 34

  • activity

  • xml

  • fragment

关键代码

基本框架实现

准备工作:导入所需图片到drawable目录下:

图片.png

基本框架xml分为四部分:top,bottom,四个tab页面和一个main_activity

top栏设计

如图:

图片.png

代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/black"
    android:gravity="center"
    android:orientation="vertical">
​
​
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/black"
        android:gravity="center_horizontal"
        android:text="微信"
        android:textColor="@color/white"
        android:textSize="40sp"></TextView>
</LinearLayout>
bottom栏

如图:

图片.png

代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
​
    <LinearLayout
        android:id="@+id/chat"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:contentDescription="@string/app_name"
        android:onClick="onClick"
        android:orientation="vertical">
​
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@color/black"
            app:srcCompat="@drawable/a" />
​
        <TextView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:background="@color/black"
            android:gravity="center"
            android:text="微信"
            android:textColor="@color/white"
            android:textSize="20dp"/>
​
    </LinearLayout>
​
    <LinearLayout
        android:id="@+id/friend"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:contentDescription="@string/app_name"
        android:onClick="onClick"
        android:orientation="vertical">
​
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="51dp"
            android:background="@color/black"
            app:srcCompat="@drawable/b" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:background="@color/black"
            android:gravity="center"
            android:text="联系人"
            android:textColor="@color/white"
            android:textSize="20dp"/>
    </LinearLayout>
​
    <LinearLayout
        android:id="@+id/finding"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:contentDescription="@string/app_name"
        android:onClick="onClick"
        android:orientation="vertical">
​
        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/black"
            app:srcCompat="@drawable/c" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:background="@color/black"
            android:gravity="center"
            android:text="发现"
            android:textColor="@color/white"
            android:textSize="20dp"/>
    </LinearLayout>
​
    <LinearLayout
        android:id="@+id/setting"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:contentDescription="@string/app_name"
        android:onClick="onClick"
        android:orientation="vertical">
​
        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="110dp"
            android:layout_height="wrap_content"
            android:background="@color/black"
            app:srcCompat="@drawable/d" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_weight="1"
            android:background="@color/black"
            android:gravity="center"
            android:text="设置"
            android:textColor="@color/white"
            android:textSize="20dp"/>
    </LinearLayout>
</LinearLayout>
tab页面

在用户点击页面时会显示对应的页面,这里只展示一个,其余三个类似

如图:

图片.png

代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragment1">
​
    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/tab01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="这是微信"
        android:textSize="35sp" />
​
</FrameLayout>
main-activity

通过main_activity文件将top,bottom两个文件添加到一起

如图:

图片.png

代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
​
​
    <include layout="@layout/top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
​
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
​
    </FrameLayout>
​
    <include layout="@layout/buttom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

切换效果的实现

要想实现点击切换的效果,需要fragmentmanager来对四个不同的tab进行控制,让他们叠加在一起的同时,实现在不同的点击动作发生时进行一个tab的显示及其余三个的隐藏。

fragment类

创建四个fragment,对应四个事件,这里只展示一个,其余三个类似

如图:

图片.png

代码:

package com.example.mychat;
​
import android.os.Bundle;
​
import androidx.fragment.app.Fragment;
​
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
​
​
public class Fragment1 extends Fragment {
​
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.tab01, container, false);
    }
}
MainActivity

主函数主要的功能是实现

  • 1.监听部分onclick

  • 2.初始化initial将四个frgment压入content

  • 3.隐藏四个tab界面fragmenthide

  • 4.展示一个tab界面fragmentshow

onclick函数
  • [注]在写onclick函数前要对tab四个页面监听,根据结果返回fragment界面

    linearLayout1.setOnClickListener(this);
            linearLayout2.setOnClickListener(this);
            linearLayout3.setOnClickListener(this);
            linearLayout4.setOnClickListener(this);
    • 设置监听后,对主函数也要进行修改

    public class MainActivity extends AppCompatActivity implements View.OnClickListener{}

代码:

@Override
    public void onClick(View view) {
        fragmenthide();
        if (view.getId()==R.id.chat){
            fragmentshow(fragment1);
        }else if (view.getId()==R.id.friend){
            fragmentshow(fragment2);
        }else if (view.getId()==R.id.finding){
            fragmentshow(fragment3);
        }else if(view.getId()==R.id.setting){
            fragmentshow(fragment4);
        }
    }
initial函数

代码:

public void initial() {
        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();

    }
fragmenthide函数

代码:

private void fragmenthide() {
        FragmentTransaction ft = fm.beginTransaction()
                .hide(fragment1)
                .hide(fragment2)
                .hide(fragment3)
                .hide(fragment4);
        ft.commit();
    }
fragmentshow函数

代码:

public void fragmentshow(Fragment fragment) {

        FragmentTransaction transaction = fm.beginTransaction()
                .show(fragment);
        transaction.commit();
    }
MainActivity.java所有代码
package com.example.mychat;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    Fragment fragment1,fragment2,fragment3,fragment4;
    FragmentManager fm;
    LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
        fragment4 = new Fragment4();
        fm = getSupportFragmentManager();

        linearLayout1 = findViewById(R.id.chat);
        linearLayout2 = findViewById(R.id.friend);
        linearLayout3 = findViewById(R.id.finding);
        linearLayout4 = findViewById(R.id.setting);

        initial();
        fragmenthide();
        fragmentshow(fragment1);
        linearLayout1.setOnClickListener(this);
        linearLayout2.setOnClickListener(this);
        linearLayout3.setOnClickListener(this);
        linearLayout4.setOnClickListener(this);
    }

    private void fragmenthide() {
        FragmentTransaction ft = fm.beginTransaction()
                .hide(fragment1)
                .hide(fragment2)
                .hide(fragment3)
                .hide(fragment4);
        ft.commit();
    }

    public void initial() {
        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();

    }

    @Override
    public void onClick(View view) {
        fragmenthide();
        if (view.getId()==R.id.chat){
            fragmentshow(fragment1);
        }else if (view.getId()==R.id.friend){
            fragmentshow(fragment2);
        }else if (view.getId()==R.id.finding){
            fragmentshow(fragment3);
        }else if(view.getId()==R.id.setting){
            fragmentshow(fragment4);
        }
    }

    public void fragmentshow(Fragment fragment) {

        FragmentTransaction transaction = fm.beginTransaction()
                .show(fragment);
        transaction.commit();
    }
}

recycleview的实现

在完成了以上功能后,现在我们需要在任意tab页面中实现列表效果(这里我用的是tab02)

1️⃣在tab02.xml页面添加一个recycleview

图片.png

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

图片.png

3️⃣重写Fragment2.java文件

我们先初始化定义一些变量recyclerView,list,context, myadapter(一个适配器)

private RecyclerView recyclerView;
private List<String> list;
private Context context;
private Myadapter myadapter;

然后我们开始在onCreateView()函数底下写入适配器需要的一些参数和数据:

public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
    View view=inflater.inflate(R.layout.tab02,container,false);
    context=view.getContext();
    recyclerView=view.findViewById(R.id.recycleview);
    list=new ArrayList();

    initial();
    myadapter = new Myadapter(context,list);
    recyclerView.setAdapter(myadapter);
    LinearLayoutManager manager=new LinearLayoutManager(context);
    manager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(manager);

    return view;

}

定义一个list用来存放我们需要输出的文字,initData()是初始化数据的函数。然后初始化myadapter适配器,并在底下填入参数。 接下来我们开始写initData()来定义需要输出的文字:

private void initial() {
    list.add("蔡徐坤");
    list.add("陈立农");
    list.add("范丞丞");
    list.add("黄明昊");
    list.add("林彦俊");
    list.add("朱正廷");
    list.add("王子异");
    list.add("王琳凯");
    list.add("尤长靖");
    list.add("nine percent(^-^)V");
}

4️⃣创建一个Myadapter类

初始化变量和创建构造函数:

Context context1;
List<String> list1;
public Myadapter(Context context,List list) {
    context1=context;
    list1=list;
}

实现的方法:

@NonNull
@Override
public Myholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context1).inflate(R.layout.item,parent,false);

    Myholder holder = new Myholder(view);

    return holder;
}

@Override
public void onBindViewHolder(@NonNull Myholder holder, int position) {
    holder.textView.setText(list1.get(position));
}

@Override
public int getItemCount() {
    return list1.size();
}

public class Myholder extends RecyclerView.ViewHolder{
    TextView textView;
    public Myholder(@NonNull View itemView) {
        super(itemView);
        textView=itemView.findViewById(R.id.textView1);
    }
}

结果展示

图片.png

图片.png

eaf05344cd676740a412fb33fa42cd1.png

1a012628e7c150e522ba4a29e4d6404.png

总结

本次实验,学会利用AS进行制作简易的微信界面,掌握fragment,layout,xml和相关控件的知识,实现页面的跳转功能,和recycleview实现列表功能,在实验中我遇到一个头疼的问题,我写完了代码没报错但是允许就是闪退,看了logcat中的报错才知道fragment的类要声明为public,也算是吃一堑长一智啦(^-^)!

源码地址

https://github.com/Drizzle042425/AS

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值