仿微信界面作业一

 一、所需文件以及成果展示

“聊天”页面

“联系人”页面

剩余两个页面与此类似。

 二、整体思路

1.编写bottom.xml文件实现界面下方“聊天”“联系人”“位置”“设置”四个按钮
2.编写top.xml文件显示顶部“微信”二字
3.编写main.xml文件将bottom.xml和top.xml文件集成到main中
4.编写tab1~tab4文件作为4个显示页面
5.编写fragment1~4和MainActivity实现4个页面的切换
6.编写item,MainActivity2,Myadapter文件使列表显示在“聊天”页面中

三、简要过程

1.bottom.xml文件的编写:

创建bottom.xml文件拉取4个Layouts里面的LinearLayout(vertical)控件

各拉取一个imageView控件(在上)和textView控件(在下)

在imageView控件里选择合适的图片,更改textView中相应的文字。

部分代码如下(全部代码已上传文末代码仓库):

<?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="wrap_content"
    android:background="@color/black"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView11"
            android:layout_width="90dp"
            android:layout_height="93dp"
            android:src="@android:drawable/sym_action_chat" />

        <TextView
            android:id="@+id/textView11"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="聊天"
            android:textColor="@color/white"
            android:textSize="35sp" />

    </LinearLayout>
2.top文件的编写:

较简单直接粘代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/black"
        android:gravity="center"
        android:text="微信"
        android:textColor="@color/white"
        android:textSize="40sp"
        tools:ignore="HardcodedText,InefficientWeight" />
</LinearLayout>
3.main.xml文件:

将bottom.xml和top.xml文件include进去。

代码如下

<?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"/>

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    </FrameLayout>

    <include
        layout="@layout/bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
4.tab1~4:

tab1:拖一个RecyclerView进来设置参数如下

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
其余的tab模版都一样:
<?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">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="246dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:text="联系人"
        android:textSize="35sp" />
</LinearLayout>
5.fragment文件:

fragment1.java:

package com.example.myapplication;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;


public class Fragment1 extends Fragment {
Context context;
RecyclerView recyclerView;
List list;
Myadapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.tab1, container, false);
        context=view.getContext();
        recyclerView=view.findViewById(R.id.recyclerView);
        list=new ArrayList();
        for(int i=0;i<9;i++) {
            list.add("这是第"+i+"行数据");}

        adapter=new Myadapter(context,list);
        recyclerView.setAdapter(adapter);
        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(manager);
        return view;
    }
}

其余fragment:

package com.example.myapplication;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;


public class Fragment2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.tab2, container, false);
    }
}
6.MainActivity文件:
package com.example.myapplication;

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.main);
        fragment1=new Fragment1();
        fragment2=new Fragment2();
        fragment3=new Fragment3();
        fragment4=new Fragment4();
        linearLayout1=findViewById(R.id.LinearLayout1);
        linearLayout2=findViewById(R.id.LinearLayout2);
        linearLayout3=findViewById(R.id.LinearLayout3);
        linearLayout4=findViewById(R.id.LinearLayout4);

        fm=getSupportFragmentManager();
        initial();
        fragmenthide();
        fragmentshow(fragment1);
        linearLayout1.setOnClickListener(this);
        linearLayout2.setOnClickListener(this);
        linearLayout3.setOnClickListener(this);
        linearLayout4.setOnClickListener(this);

    }



    private void fragmenthide() //将4个页面都隐藏
   {
        FragmentTransaction ft=fm.beginTransaction()
                .hide(fragment1)
                .hide(fragment2)
                .hide(fragment3)
                .hide(fragment4);
        ft.commit();
    }

    private void initial() {//将4个页面初始化

        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();


        int id = view.getId();
        if (id == R.id.LinearLayout1) {
            fragmentshow(fragment1);
        } else if (id == R.id.LinearLayout2) {
            fragmentshow(fragment2);
        } else if (id == R.id.LinearLayout3) {
            fragmentshow(fragment3);
        } else if (id == R.id.LinearLayout4) {
            fragmentshow(fragment4);
        }


    }



    private void fragmentshow(Fragment fragment){//显示对应页面
        FragmentTransaction transaction=fm.beginTransaction()
                .show(fragment);
        transaction.commit();

    }
}

7.MainActivity2:

package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity2 extends AppCompatActivity {
    List<String> list;
    RecyclerView recyclerView;

    Myadapter adapter;
    Context context;


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

        recyclerView=findViewById(R.id.recyclerView);
        list=new ArrayList();
        for(int i=0;i<9;i++) {
            list.add("这是第"+i+"行数据");}
        context=this;
        adapter=new Myadapter(this,list);

        recyclerView.setAdapter(adapter);
        LinearLayoutManager manager=new LinearLayoutManager(this);
        manager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(manager);
    }
}
7.Myadapter:
package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class Myadapter extends RecyclerView.Adapter<Myadapter.Myholder> {
   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 myadapter=new Myholder(view);
        return myadapter;
    }

    @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.textView6);



        }
    }
}

四、总结

     写代码过程中总会遇到各种报错,见到了不要着急,要按报错语句的提示修改,as开发工具一般有给你提供的修改方法,大部分按照开发工具给你提供的方法修改都能改正确,但是改后可能会有逻辑错误,要谨慎使用。提词器是个好东西,可以极大缩短写代码的时间。

Gitee:

https://gitee.com/Shuo1123/android-development

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值