2021-10-09

移动开发技术第一次作业-----微信界面

一、预览效果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、核心思路:使用布局(layouts)和分段(fragment),以及对控件进行点击监听。
Fragment:,它可作为Activity界面的组成部分,可在Activity运行中实现动态地加入、移除和交换。一个Activity中可同时出现多个Fragment,一个Fragment也可在多个Activity中使用。
1、创建四个xml文件,分别对应 消息界面 联系人界面 朋友圈界面 设置界面 。
消息界面:在这里插入图片描述

朋友圈界面:

在这里插入图片描述
联系人界面:
在这里插入图片描述
设置界面:
在这里插入图片描述
2、创建四个Fragment,分别对应“聊天”、“联系人”、“朋友圈”、“设置”这四个xml文件,其中,设置所对应的fragment函数如下

在这里插入图片描述
联系所对应的fragment如下
在这里插入图片描述
朋友圈所对应的fragment如下
在这里插入图片描述
信息对应的fragment如下:
在这里插入图片描述

以下是mainactivity函数,最重要的部分:

在这里插入代package com.example.mywechat;

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

import android.graphics.Color;
import android.os.Bundle;
import android.view.SurfaceControl;
import android.view.View;

import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements OnClickListener {
    private FragmentManager fragmentManager;
    private FragmentTransaction beginTransaction;

    private FrameLayout frameLayout;
    private LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4;
    private ImageView imageView1,imageView2,imageView3,imageView4,ivCurrent;
    private TextView textView1,textView2,textView3,textView4,tvCurrent;


    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        linearLayout1 = findViewById(R.id.linearLayout1);
        linearLayout2 = findViewById(R.id.linearLayout2);
        linearLayout3 = findViewById(R.id.linearLayout3);
        linearLayout4 = findViewById(R.id.linearLayout4);

        imageView1 = findViewById(R.id.imageView);
        imageView2 = findViewById(R.id.imageView2);
        imageView3 = findViewById(R.id.imageView3);
        imageView4 = findViewById(R.id.imageView4);

        textView1 = findViewById(R.id.message);
        textView2 = findViewById(R.id.contact);
        textView3 = findViewById(R.id.find);
        textView4 = findViewById(R.id.config);

        linearLayout1.setOnClickListener(this);
        linearLayout2.setOnClickListener(this);
        linearLayout3.setOnClickListener(this);
        linearLayout4.setOnClickListener(this);

        textView1.setTextColor(Color.GREEN);
        linearLayout1.setBackgroundColor(Color.rgb(155,155,155));
        fragmentManager = getSupportFragmentManager();
        beginTransaction = fragmentManager.beginTransaction();
        beginTransaction.replace(R.id.id_contianer, new MessageFragment());
        beginTransaction.commit();
    }


    @Override
    public void onClick(View v) {
        setAllColor();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction beginTransaction = fragmentManager
                .beginTransaction();
//        hideAllFragment();
        switch (v.getId()){
            case R.id.linearLayout1:
                beginTransaction.replace(R.id.id_contianer, new MessageFragment());
                textView1.setTextColor(Color.GREEN);
                linearLayout1.setBackgroundColor(Color.rgb(155,155,155));
                break;
            case R.id.linearLayout2:
                beginTransaction.replace(R.id.id_contianer, new ContactFragment());
                textView2.setTextColor(Color.GREEN);
                linearLayout2.setBackgroundColor(Color.rgb(155,155,155));
                break;
            case R.id.linearLayout3:
                beginTransaction.replace(R.id.id_contianer, new FindFragment());
                textView3.setTextColor(Color.GREEN);
                linearLayout3.setBackgroundColor(Color.rgb(155,155,155));
                break;
            case R.id.linearLayout4:
                beginTransaction.replace(R.id.id_contianer, new ConfigFragment());
                textView4.setTextColor(Color.GREEN);
                linearLayout4.setBackgroundColor(Color.rgb(155,155,155));
                break;
        }
        beginTransaction.commit();
    }
    private void setAllColor() {
        textView1.setTextColor(Color.BLACK);
        textView2.setTextColor(Color.BLACK);
        textView3.setTextColor(Color.BLACK);
        textView4.setTextColor(Color.BLACK);
        linearLayout1.setBackgroundColor(Color.WHITE);
        linearLayout2.setBackgroundColor(Color.WHITE);
        linearLayout3.setBackgroundColor(Color.WHITE);
        linearLayout4.setBackgroundColor(Color.WHITE);
    }
//    private void hideAllFragment() {
//        beginTransaction.hide(messageFragment);
//        beginTransaction.hide(configFragment);
//        beginTransaction.hide(contactFragment);
//        beginTransaction.hide(findFragment);
//    }
}码片











三、项目总结反思:
1、这次实验遇到的问题。这次的实验算是一个比较入门的项目,但是其中的困难点也有很多,比如监听事件绑定的时候绑定的是bottom里面的四个linearLayout,而不是fragment里面的linearLayout,里面需要理解的就是java主函数中的绑定监听以及监听实现的逻辑,其中有点击事件,点击后就要显示那个fragment,其余的就要隐藏,这些都是要写方法来实现的。
2、还有就是如果AS虚拟机运行结果是hello world或者是上一次的结果,就说明代码有问题。
3、运行图片不能显示的问题可以把代码中的图像来源改为android:src="@drawable/message"。
4、遇到问题多百度多想,而不是抱怨,抱怨是解决不了任何问题的,有时候上课跟着老师觉得没有任何问题,下来自己弄的时候就全是问题了。
四、童志成的gitee地址:gitee地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值