移动开发-实验1-微信界面简单实现

目标:

1:需要实现3-4个tab切换效果;本功能要求需要的技术为:activity、xml、fragment

2:在任一tab页中实现列表效果;本功能的实现需要使用recycleview;

设计思路

1:实现微信界面:将微信分为上,中,下三个布局,分别为top.xml,tab.xml,bottom.xml文件,中间内容为tab1.xml,tab2.xml,tab3.xml,tab4.xml。

2列表实现

将任意一个tab替换为列表

设计过程

top.xml:
android:layout_width="wrap_content":设置布局宽度
android:layout_height="wrap_content"设置布局高度
android:background="#07E810":设置背景颜色
android:gravity="center":设置文本在TextView中的对齐方式为居中对齐。
android:text="微信":设置TextView显示的文本内容为"微信"。
android:textColor="@color/white":设置文本颜色为白色
anroid:textSize="40sp":设置字体大小为40sp 

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#07E810"
        android:gravity="center"
        android:text="微信"
        android:textColor="@color/white"
        android:textSize="40sp" />
</LinearLayout>

top.xml展示:

bottom.xml

android:orientation="horizontal": 这个属性用于设置 LinearLayout 中子视图的排列方向。在这里,它被设置为 "horizontal",表示子视图将水平排列。

android:layout_weight="1": 这是内部 LinearLayout 的属性之一,通常用于在布局中进行权重分配。在这里,它被设置为 "1",表示内部 LinearLayout 在与其他视图共享空间时具有相等的权重。

android:orientation="vertical": 这个属性用于设置内部 LinearLayout 中子视图的排列方向。在这里,它被设置为 "vertical",表示子视图将垂直排列。

ImageView: 这是 ImageView 元素的开始标签,表示一个图像视图。

android:id="@+id/imageView11": 这是 ImageView 的属性之一,用于指定其唯一标识符,可以在代码中引用它。

android:layout_weight="1": 这是 ImageView 的属性之一,通常用于在布局中进行权重分配,被设置为 "1",表示 ImageView 在与其他视图共享空间时具有相等的权重。

app:srcCompat="@android:drawable/ic_menu_always_landscape_portrait": 这个属性用于设置 ImageView 的图像资源。引用了一个Android内置的图像资源

<?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:background="#07E810"
    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="115dp"
            android:layout_height="109dp"

            android:layout_weight="1"
            app:srcCompat="@android:drawable/ic_menu_compass" />

        <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="30sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView22"
            android:layout_width="115dp"
            android:layout_height="109dp"

            android:layout_weight="1"
            app:srcCompat="@android:drawable/ic_btn_speak_now" />

        <TextView
            android:id="@+id/textView22"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="联系人"
            android:textColor="@color/white"
            android:textSize="30sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView33"
            android:layout_width="match_parent"
            android:layout_height="109dp"

            android:layout_weight="1"
            app:srcCompat="@android:drawable/ic_menu_add" />

        <TextView
            android:id="@+id/textView33"
            android:layout_width="115dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="位置"
            android:textColor="@color/white"
            android:textSize="30sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout4"
        android:layout_width="109dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView44"
            android:layout_width="match_parent"
            android:layout_height="109dp"

            android:layout_weight="1"
            app:srcCompat="@android:drawable/ic_menu_manage" />

        <TextView
            android:id="@+id/textView44"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="设置"
            android:textColor="@color/white"
            android:textSize="30sp" />
    </LinearLayout>
</LinearLayout>

bottom.xml展示

 tab.xml

android:layout_width="wrap_content": 这是 TextView 的属性之一,用于定义其宽度。在这里,它被设置为 "wrap_content",表示 TextView 的宽度将根据其文本内容自动调整。

android:layout_height="wrap_content": 这是 TextView 的属性之一,用于定义其高度。它被设置为 "wrap_content",表示 TextView 的高度将根据其文本内容自动调整。

android:layout_gravity="center": 这是 TextView 的属性之一,用于定义 TextView 在其父容器中的对齐方式,设置为 "center",表示文本将在父容器中水平和垂直居中对齐。

android:layout_weight="1": 这是 TextView 的属性之一,通常用于在布局中进行权重分配。在这里,它被设置为 "1",表示 TextView 在与其他视图共享空间时具有相等的权重。

android:gravity="center": 这是 TextView 的属性之一,用于定义文本在 TextView 内的对齐方式。在这里,它被设置为 "center",表示文本将水平和垂直居中对齐。

android:text="这是聊天界面": 这是 TextView 的属性之一,用于设置 TextView 中显示的文本内容

android:textSize="50sp": 这是 TextView 的属性之一,用于定义文本的大小。在这里,它被设置为 "50sp",表示文本将以50sp的大小显示。
 

<?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/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:text="这是聊天界面"
        android:textSize="50sp" />
</LinearLayout>

top.xml展示:

 最后main.xml中完成整合,完成基础布局
<?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/content1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

    </FrameLayout>

    <include
        android:id="@+id/content"
        layout="@layout/bottom" />

</LinearLayout>

main.xml展示

 

创建java类(fragment类) 

创建四个fragment文件

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) : 这是 onCreateView 方法的开始。它是一个生命周期方法,用于创建和返回与Fragment相关联的用户界面视图。

return inflater.inflate(R.layout.tab1, container, false): 这是 onCreateView 方法的主体。它通过 inflater(一个用于将XML布局文件转换为视图对象的工具)加载了一个名为 tab1 的布局文件,并将其与 container关联。

package com.example.myapplication;

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.tab1, container, false);
    }
}
创建MainMainActivity文件 ,实现界面切换和fragment布局

linearLayout1=findViewById(R.id.LinearLayout1);, linearLayout2=findViewById(R.id.LinearLayout2);, linearLayout3=findViewById(R.id.LinearLayout3);, linearLayout4=findViewById(R.id.LinearLayout4);: 这些行查找并获取XML布局文件中具有对应标识符的LinearLayout视图,然后将它们分别分配给 linearLayout1、linearLayout2、linearLayout3 和 linearLayout4 变量。

fm=getSupportFragmentManager();: 这一行获取 FragmentManager 的实例,用于管理Fragment的交互和显示。

initial();: 这一行调用 initial() 方法,它在布局中添加了四个Fragment(Fragment1、Fragment2、Fragment3 和 Fragment4)。

fragmenthide();: 这一行调用 fragmenthide() 方法,它隐藏了所有的Fragment。

fragmentshow(fragment1);: 这一行调用 fragmentshow() 方法,并显示 Fragment1,作为初始显示的Fragment。

linearLayout1.setOnClickListener(this);, linearLayout2.setOnClickListener(this);, linearLayout3.setOnClickListener(this);, linearLayout4.setOnClickListener(this);: 这些行为 linearLayout1、linearLayout2、linearLayout3 和 linearLayout4 设置了点击监听器,当这些布局元素被点击时,onClick 方法会被调用。

private void fragmenthide() {: 这是 fragmenthide() 方法的开始,它用于隐藏所有的Fragment。

FragmentTransaction ft=fm.beginTransaction()...: 这一行开始了一个 FragmentTransaction 事务,它用于执行界面操作。在这里,使用 hide() 方法隐藏了四个不同的Fragment:fragment1、fragment2、fragment3 和 fragment4。

ft.commit();: 这一行提交了事务,使界面的隐藏操作生效。

private void initial() {: 这是 initial() 方法的开始,它用于初始化并添加四个不同的Fragment到活动中。

FragmentTransaction ft=fm.beginTransaction()...: 这一行开始了一个新的 FragmentTransaction 事务,用于执行Fragment操作。在这里,使用 add() 方法将四个Fragment添加到活动的布局中。

ft.commit();: 这一行提交了事务,将Fragment添加到活动的布局中。

public void onClick(View view): 这是 onClick 方法的开始,它是一个点击事件处理方法。它会在用户点击布局元素时被调用,根据用户的点击来切换显示不同的Fragment。

fragmenthide();: 这一行再次调用 fragmenthide() 方法,以确保在显示新的Fragment之前隐藏所有其他Fragment。

FragmentTransaction transaction=fm.beginTransaction()...: 这一行开始了一个新的 FragmentTransaction 事务,用于执行Fragment操作。

transaction.commit();: 这一行提交了事务,以显示特定的Fragment。

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 androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

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

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    Fragment1 fragment;
    Fragment fragment1,fragment2,fragment3,fragment4;

    FragmentManager fm;
    FragmentTransaction ft;
    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() {
        FragmentTransaction ft=fm.beginTransaction()
                .hide(fragment1)
                .hide(fragment2)
                .hide(fragment3)
                .hide(fragment4);
        ft.commit();

    }

    private void initial() {
        FragmentTransaction ft=fm.beginTransaction()
                .add(R.id.content1,fragment1)
                .add(R.id.content1,fragment2)
                .add(R.id.content1,fragment3)
                .add(R.id.content1,fragment4);
        ft.commit();
    }

    @Override
    public void onClick(View view){
        fragmenthide();

        /*switch (view.getId()){
            case R.id.LinearLayout1: fragmentshow(fragment1);break;
            case R.id.LinearLayout2: fragmentshow(fragment2);break;
            case R.id.LinearLayout3: fragmentshow(fragment3);break;
            case R.id.LinearLayout4: fragmentshow(fragment4);break;
            default:  break;*/
        if(view.getId()==R.id.LinearLayout1){
            fragmenthide();
            fragmentshow(fragment1);
        }
        if(view.getId()==R.id.LinearLayout2){
            fragmenthide();
            fragmentshow(fragment2);
        }
        if(view.getId()==R.id.LinearLayout3){
            fragmenthide();
            fragmentshow(fragment3);
        }
        if(view.getId()==R.id.LinearLayout4){
            fragmenthide();
            fragmentshow(fragment4);
        }


    }

    private void fragmentshow(Fragment fragment) {
        FragmentTransaction transaction=fm.beginTransaction()
                .show(fragment);
        transaction.commit();
    }
}

 结果展示:

实现列表 

将tab2.xml进行修改,加入recycleview

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

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycleview"
        android:layout_width="388dp"
        android:layout_height="701dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
   />
</LinearLayout>
新建item.xml设计布局

 

<?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/Textview1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:layout_weight="0"
        android:gravity="center"
        android:text="TextView"
        android:textColor="@color/black"
        android:textSize="35sp"
    />

</LinearLayout>
创建Myadapter类

  `onCreateViewHolder` 方法的参数 `parent` 是 `RecyclerView` 的父视图,`viewType` 是视图类型,通常在多视图类型的情况下使用。在这里,`viewType` 没有被使用。
   `View view = LayoutInflater.from(context1).inflate(R.layout.item, parent, false);`:这一行通过从上下文 `context1` 中获取布局填充器,然后将 `item` 布局文件充气为视图对象,该视图将用作新的视图持有者的布局。
   `Myholder myholder = new Myholder(view);`:接着,新的视图持有者对象 `myholder` 被创建,它将关联到视图对象 `view`。最后,`myholder` 对象被返回,表示新的视图持有者已创建。

  `Myholder` 类包含一个 `TextView` 类型的成员变量 `textView`,用于引用 `item` 布局中的文本视图。
   构造函数 `Myholder(@NonNull View itemView)` 接受一个视图 `itemView`,并调用父类的构造函数 `super(itemView)` 来设置视图持有者的根视图。
   接着,通过 `itemView.findViewById(R.id.textView6)` 查找 `item` 布局中具有标识符 `R.id.textView6` 的文本视图,然后将它赋给 `textView` 成员变量,以便在 `onBindViewHolder` 方法中绑定数据到这个文本视图。

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;
import java.util.zip.Inflater;

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 myholder=new Myholder(view);
        return myholder;
    }

    @Override
    public void onBindViewHolder(@NonNull Myholder holder, int position) {

        holder.textView.setText(list1.get(position));


    }

    @Override
    public int getItemCount() {
        return list1.size();
    }
    protected class Myholder extends RecyclerView.ViewHolder {
        TextView textView;
        public Myholder(@NonNull View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.Textview1);
        }
    }
}

修改fragment2 

recyclerView = view.findViewById(R.id.recycleView);: 这一行通过视图 view 找到具有标识符 R.id.recycleView 的 RecyclerView 控件,并将其赋给 recyclerView 变量,以便在后续操作中使用。

list = new ArrayList();: 这一行创建一个名为 list 的新数组列表(ArrayList)以存储数据。这个列表将用于填充 RecyclerView。

myadapter = new Myadapter(context, list);: 这一行创建了一个名为 myadapter 的自定义适配器(Myadapter),并将上下 context 和数据列表 list 传递给适配器。适配器用于将数据显示在 RecyclerView 中。

recyclerView.setAdapter(myadapter);: 这一行将适配器 myadapter 设置为 RecyclerView 的适配器,这将使 RecyclerView 显示来自适配器的数据。

LinearLayoutManager manager = new LinearLayoutManager(context);: 这一行创建了一个 LinearLayoutManager 对象,用于配置 RecyclerView 的布局管理器。这里的布局管理器是垂直线性布局管理器。

manager.setOrientation(LinearLayoutManager.VERTICAL);: 这一行设置线性布局管理器的方向为垂直方向,以便 RecyclerView 中的项目在垂直方向上排列。

recyclerView.setLayoutManager(manager);: 这一行将线性布局管理器 manager 分配给 RecyclerView,这将决定项目的排列方式。

return view;: 这一行返回了包含 RecyclerView 的视图,它将成为Fragment2的用户界面
 

package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.*;

public class Fragment2 extends Fragment {
    Context context;
    List list;
    RecyclerView recyclerView;
    Myadapter myadapter;

    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.tab2, container, false);
        context = view.getContext();
        recyclerView = view.findViewById(R.id.recycleview);
        list = new ArrayList();
        for(int i=0;i<9;i++){
            list.add("好友"+i);}

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

        return view;


    }
}

结果展示

总结

1运用as可以自己制作简易安卓app

2在使用as的过程中注意as的版本问题,不然会发生大量报错

3as给我们提供了很多强大的功能,方便我的使用和学习

gitee地址

https://gitee.com/tourists-zhang/as-projecticon-default.png?t=N7T8https://gitee.com/tourists-zhang/as-project

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值