移动开发技术第一次作业-----微信界面
一、预览效果
二、核心思路:使用布局(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地址