移动开发 App(微信)门户页面设计与开发

一、作业目标

1.设计一个app的门户框架,需要实现3-4个tab切换效果

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

二、设计思路

1.实现类微信界面:

将界面分为上中下三个部分,分别为top.xml,tab.xml,bottom.xml,中间的tab.xml分为tab1.xml,tab2.xml,tab3.xml,tab4.xml四个部分,最后用main.xml将其整合在一起

2.实现列表效果:

利用recycleview在tab.xml实现列表效果

三、设计过程

首先设计头部文件top.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/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/black"
        android:gravity="center"
        android:text="微信"
        android:textColor="@color/white"
        android:textSize="30sp" />
</LinearLayout>

1.android:layout_width="wrap_content":视图的宽度将会根据其内容自动进行调整,以适应文本内容的宽度。

2.android:layout_height="wrap_content":视图的高度将会根据其内容自动进行调整,以适应文本内容的宽度。

3.android:layout_weight="1":表示这个视图在与其他视图共享空间时具有相等的权重。

4.android:background="@color/black":表示将视图的背景颜色设置为黑色。

5.android:gravity="center":这会将视图或内容在水平和垂直方向上都居中对齐,使其位于可用空间的中心。

6.android:text="微信":将文本内容设置为微信

7.android:textColor="@color/white":将文本内容设置为白色。

8.android:textSize="30sp":将文本内容的大小设置为30sp。

top.xml界面显示如下:

然后设计中间部分tab.xml:

<?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/textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="这是聊天界面"
        android:textSize="35sp" />

</FrameLayout>

1.android:layout_width="match_content":这会使视图的宽度填满其父容器的宽度。如果视图嵌套在一个父容器中,它将充满该父容器的宽度,无论父容器的宽度是多少。

2.android:layout_height="match_content":这会使视图的宽度填满其父容器的宽度。如果视图嵌套在一个父容器中,它将充满该父容器的宽度,无论父容器的宽度是多少。

3.android:gravity="center":这会将视图或内容在水平和垂直方向上都居中对齐,使其位于可用空间的中心

4.android:text=“这是聊天界面":将文本内容设置为这是聊天界面

5.android:textSize="30sp":将文本内容的大小设置为30sp。

由于tab1与其他三个tab.xml相似,在此只展示tab1的代码和界面:

在设计底部部分bottom.xml:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        >


        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"

            app:srcCompat="@android:drawable/sym_action_chat" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="聊天"
            android:textSize="25dp"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        >

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"

            app:srcCompat="@android:drawable/sym_action_call" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="联系人"
            android:textSize="25dp"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout3"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        >

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"

            app:srcCompat="@android:drawable/ic_menu_myplaces" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="位置"
            android:textSize="25dp"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout4"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        >

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"

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

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="设置"
            android:textSize="25dp"/>
    </LinearLayout>

</LinearLayout>

1.android:orientation="vertical":线性布局会将其子视图垂直排列,从上到下的方式排列子视图

2..android:layout_weight="1":表示这个视图在与其他视图共享空间时具有相等的权重。

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

4.app:srcCompat="@android:drawable/sym_action_call":用于设置图像视图的图像资源

bottom的界面显示如下:

最后整合在mian.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"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
    </FrameLayout>
    <include
        layout="@layout/bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"/>

</LinearLayout>

mian.xml的界面显示如下:

四、创建java文件

首先创建四个Fragment文件:

 四个Fragment代码类似,故只展示Fragment1的代码:

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

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

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

创建MainActivity文件,实现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 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();


        inital();
        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 inital(){
            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) {
                fragmenthide();
                fragmentshow(fragment1);
            } else if (id == R.id.LinearLayout2) {
                fragmenthide();
                fragmentshow(fragment2);
            } else if (id == R.id.LinearLayout3) {
                fragmenthide();
                fragmentshow(fragment3);
            } else if (id == R.id.LinearLayout4) {
                fragmenthide();
                fragmentshow(fragment4);
            }
        }




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

        }
    }

1.fragmenthide():用来隐藏视图

2.inital():用来初始化Fragment

3.onClick(View view):用于处理底部导航栏中选项的点击事件

4.fragmentshow(Fragment fragment):用来显示视图

运行结果如下:

五、实现列表效果:

首先修改tab2的代码,加入recyclerview:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp" />
</FrameLayout>

android:layout_marginStart="8dp":视图与其父容器的起始边之间的距离为8dp。

tab2修改后的界面:

新建item.xml,设计列表布局格式:

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

    <TextView
        android:id="@+id/textView10"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="35sp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="TextView"
        android:textColor="@color/black" />
</androidx.constraintlayout.widget.ConstraintLayout>

item界面:

创建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;

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.textView10);
        }
    }
}

修改Fragment2里的代码:

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 Fragment2 extends Fragment {


    Context context;
    List list;
    RecyclerView recyclerView;
    Myadapter myadapter;


    @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;

    }
}

最后的结果:

六、总结

学会了怎么使用as,了解到了怎么编辑视图,学会了如何切换界面,类和函数的撰写更加熟练,使用过程中出现了很多错误,但是在老师的提醒下没有着急,仔细阅读错误,知道了是什么原因,最终解决了。

七、gitte仓库链接

https://gitee.com/pjf_1/code-warehouse

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值