微信的门户页面框架设计

一、项目整体说明

设计目标

此项目需要完成基本的微信页面布局设计,包括顶部显示栏、底部导航栏和4个table页面,点击相应的导航键,能简单实现table页面之间的切换,相应的图标也发生变化。

使用技术

布局(layout)和分段(fragment),对控件进行点击监听,同时用activity完善门户页面的框架设计

运行环境

计算机型号:小新 15IIL 2020
处理器:Intel® Core™ i7-1065G7 CPU @ 1.30GHz 1.50 GHz
机带RAM:8.00 GB (7.78 GB 可用)
操作系统:Windows 10 家庭中文版
编译器:Android Studio 2021.2.0.0

二、功能说明及代码解析

1.各页面设计(layout)

顶部导航栏(top.xml)

在layout文件中新建一个.xml文件,命名为top.xml,布局(RootTag)设置为LinearLayout,创建完成后拖入一个TextView空间至LinearLayout下
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在code界面设置字体大小、颜色及背景颜色等
在这里插入图片描述

<?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="0dp"
        android:layout_height="60dp"
        android:layout_weight="1"
        android:background="@color/teal_200"
        android:gravity="center"
        android:text="微信"
        android:textColor="#546E7A"
        android:textSize="30sp" />
</LinearLayout>

代码解析

android:id="@+id/textView",为组件设置一个资源id,在java文件中可以通过findViewById(id)找到该组件。
android:layout_width=“match_parent”,布局的宽度,match_parent(填满父容器)
android:layout_height=“wrap_content”,布局的高度,wrap_content(组件实际大小)
android:layout_weight=“1”,等比例划分区域。
android:background="@color/teal_200",为组件设置一个背景图片,或者直接用颜色覆盖。
android:gravity=“center”,表示textView中的文字相对于TextView的对齐方式。
android:text=“微信”,要显示的文字。
android:textColor="#546E7A",设置文字的颜色。
android:textSize=“30sp” />,设置文字的大小。

设置完成后效果图如下
在这里插入图片描述

底部导航栏(bottom.xml)

将需要的八个微信图标(微信,通讯录,发现,我的及其点击后的图标)拖入mipmap-hdpi中做准备
在这里插入图片描述
在layout文件中新建另一个.xml文件,命名为bottom.xml,布局(RootTag)设置为LinearLayout,创建完成后,在LinearLayout之下拖入四个纵向排列的LinearLayout,每个LinearLayout中分别加入选择好的ImageView和TextView
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
imageView选择如下
在这里插入图片描述
在code页面设置字体和相对布局等

<?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:backgroundTint="@android:color/darker_gray">

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

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:src="@mipmap/chat"
            app:srcCompat="@mipmap/chat" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="微信"
            android:textSize="18sp" />

    </LinearLayout>

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

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:src="@mipmap/friend"
            app:srcCompat="@mipmap/friend" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="通讯录"
            android:textSize="18sp" />
    </LinearLayout>

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

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:src="@mipmap/find"
            app:srcCompat="@mipmap/find" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="发现"
            android:textSize="18sp" />
    </LinearLayout>

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

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:src="@mipmap/my"
            app:srcCompat="@mipmap/my" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="我的"
            android:textSize="18sp" />

    </LinearLayout>
</LinearLayout>

代码解析

LinearLayout:android:orientation="horizontal",布局中组件的排列方式,有horizontal(水平)和vertical(垂直)两种方式
ImageView:app:srcCompat="@mipmap/my",用于运行中图片的显示

设置完成后效果图如下
在这里插入图片描述

主题界面(activity_main.xml)

主题界面布局仍为LinearLayout,但布局组件排列方式为vertical(垂直)
在这里插入图片描述
在控件搜索栏中搜索include,拖两个进入LinearLayout中,并分别选择top,bottom界面引入,之后向LinearLayout中拖入FragmentContainerView用于页面内容显示
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在code页面代码设置如下

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <include layout="@layout/top"></include>

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

    <include layout="@layout/bottom"></include>

</LinearLayout>

代码解析

include layout="@layout/top"在布局中导入另一个布局

4个Fragment页面设计

在layout中新建四个.xml文件命名分别为fragment_first到fragment_forth,布局Root Tag均为LinearLayout
在这里插入图片描述

fragment_first.xml(微信聊天)页面设计

在linearlayout中拖入一个TextView作为展示内容
在这里插入图片描述
代码配置

<?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="match_parent"
    tools:context=".first">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="微信聊天页面"
        android:textSize="50sp" />

</LinearLayout>

代码解析

tools:context=".first",这句相当于被注释了,编译后不起任何作用

完成后效果图展示
在这里插入图片描述

fragment_second.xml(通讯录)页面设计

在linearlayout中拖入一个TextView作为展示内容
在这里插入图片描述
代码配置

<?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="match_parent"
    tools:context=".second">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="通讯录"
        android:textSize="50sp" />

</LinearLayout>

代码解析

重复内容同上

完成后效果图展示
在这里插入图片描述

fragment_third.xml(发现/朋友圈)页面设计

在linearlayout中拖入一个TextView作为展示内容
在这里插入图片描述
代码配置

<?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="match_parent"
    tools:context=".third">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="朋友圈"
        android:textSize="50sp" />

</LinearLayout>

代码解析

重复内容同上

完成后效果图展示
在这里插入图片描述

fragment_forth.xml(我的)页面设计

在linearlayout中拖入一个TextView作为展示内容
在这里插入图片描述
代码配置

<?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="match_parent"
    tools:context=".fourth">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="我的详情界面"
        android:textSize="50sp" />

</LinearLayout>

代码解析

重复内容同上

完成后效果图展示
在这里插入图片描述

2.相关Java文件配置

点击按钮绑定点击切换的方法

跳转页面的实现需要将布局按钮与事件相结合,当点击按钮时触发监听事件,实现跳转,此时就每个linearLayout都需要与java类绑定,在java文件中写方法实现跳转,所以先新建四个java文件分别与bottom里的每个linearLayout绑定
在这里插入图片描述

微信聊天java代码
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 first extends Fragment {

    public first() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false);
    }
}
通讯录Java代码
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 second extends Fragment {

    public second() {
        // Required empty public constructor
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_second, container, false);
    }
}
发现/朋友圈Java代码
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 third extends Fragment {

    public third() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_third, container, false);
    }
}
我的Java代码
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 fourth extends Fragment {

    public fourth() {
        // Required empty public constructor
    }
   
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fourth, container, false);
    }
}

主函数MainActivity及代码解析

新建四个Fragment对象,并使用FragmentManager来管理

    private Fragment first = new first();
    private Fragment second = new second();
    private Fragment third = new third();
    private Fragment fourth = new fourth();

    private FragmentManager fragmentManager;

创建四个linearLayout对象,并绑定点击的监听事件

   private LinearLayout linearLayout1;
    private LinearLayout linearLayout2;
    private LinearLayout linearLayout3;
    private LinearLayout linearLayout4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        linearLayout1 = findViewById(R.id.first);
        linearLayout2 = findViewById(R.id.second);
        linearLayout3 = findViewById(R.id.third);
        linearLayout4 = findViewById(R.id.forth);

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

        initFragment();
    }

初始化fragment

   protected void initFragment() {
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.content,first);
        transaction.add(R.id.content,second);
        transaction.add(R.id.content,third);
        transaction.add(R.id.content,fourth);
        hideFragment(transaction);
        transaction.show(second);
        transaction.commit();
    }

在点击另一个导航键的时候需要隐藏其余的几个页面,因此要编写隐藏fragment的代码

    protected void hideFragment(FragmentTransaction transaction){
        transaction.hide(first);
        transaction.hide(second);
        transaction.hide(third);
        transaction.hide(fourth);
    }

采用switch编写点击触发的事件(页面跳转)

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.first:
                showFragment(1);
                break;
            case R.id.second:
                showFragment(2);
                break;
            case R.id.third:
                showFragment(3);
                break;
            case R.id.forth:
                showFragment(4);
                break;
            default:
                break;
        }
    }

用switch编写展示当前fragment的方法

    private void showFragment(int i) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 1:
                transaction.show(first);
                break;
            case 2:
                transaction.show(second);
                break;
            case 3:
                transaction.show(third);
                break;
            case 4:
                transaction.show(fourth);
                break;
            default:
                break;
        }
        transaction.commit();
    }
}

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{


    private Fragment first = new first();
    private Fragment second = new second();
    private Fragment third = new third();
    private Fragment fourth = new fourth();

    private FragmentManager fragmentManager;

    private LinearLayout linearLayout1;
    private LinearLayout linearLayout2;
    private LinearLayout linearLayout3;
    private LinearLayout linearLayout4;


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

        linearLayout1 = findViewById(R.id.first);
        linearLayout2 = findViewById(R.id.second);
        linearLayout3 = findViewById(R.id.third);
        linearLayout4 = findViewById(R.id.forth);

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

        initFragment();
    }

    protected void initFragment() {
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.content,first);
        transaction.add(R.id.content,second);
        transaction.add(R.id.content,third);
        transaction.add(R.id.content,fourth);
        hideFragment(transaction);
        transaction.show(second);
        transaction.commit();
    }

    protected void hideFragment(FragmentTransaction transaction){
        transaction.hide(first);
        transaction.hide(second);
        transaction.hide(third);
        transaction.hide(fourth);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.first:
                showFragment(1);
                break;
            case R.id.second:
                showFragment(2);
                break;
            case R.id.third:
                showFragment(3);
                break;
            case R.id.forth:
                showFragment(4);
                break;
            default:
                break;
        }
    }

    private void showFragment(int i) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 1:
                transaction.show(first);
                break;
            case 2:
                transaction.show(second);
                break;
            case 3:
                transaction.show(third);
                break;
            case 4:
                transaction.show(fourth);
                break;
            default:
                break;
        }
        transaction.commit();
    }
}

三、遇到的问题及解决方法

1.图片插入mipmap-anydpi-v26之后预览页面显示图片,运行后不显示图片
在这里插入图片描述
解决方法:把图片从mipmap-anydpi-v26里面挪到mipmap-hdpi里面

四、运行结果展示

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

五、clone地址

gitee地址无效请直接搜索https://gitee.com/waiaiy/android.git

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 微信小程序是一种基于微信的应用开发平台,可以让开发者在微信中快速开发、部署和分发小程序。小程序框架是一种轻量级的开发框架,提供了一系列的基础组件和API,使得开发者可以快速构建高性能的小程序。微信小程序的开发语言是基于 JavaScript 和 WXML/WXSS,支持使用 IDE 和代码检查工具来开发和调试。 ### 回答2: 微信小程序框架是一种用于开发微信小程序的工具,它提供了一系列的开发规范、API接口以及组件库,帮助开发者快速搭建和开发小程序。微信小程序框架基于Vue.js和React.js实现,通过提供类似于Web开发的组件化开发方式,使得开发小程序更加简单和高效。 微信小程序框架的特点如下: 1. 组件化开发:微信小程序框架采用组件化的开发方式,将界面拆分为各个独立的组件,开发者可以通过组件的组合和复用实现快速开发。 2. 轻量化:微信小程序框架采用轻量级的设计页面渲染速度快,性能稳定,并且具备良好的用户体验。 3. 开发门槛低:开发者可以使用HTML、CSS和JavaScript进行开发,并且可以快速上手,有助于降低学习成本。 4. 全面的API支持:微信小程序框架提供了丰富的API接口,包括常用的网络请求、数据存储、设备信息、地理位置等功能,方便开发者快速实现各种业务需求。 5. 实时预览:微信小程序框架提供实时预览功能,开发者可以在开发工具中实时查看小程序的效果,加快开发调试的速度。 6. 跨平台支持:微信小程序框架支持在微信客户端以及其他平台上运行,具有较好的跨平台兼容性。 综上所述,微信小程序框架是一个高效、灵活且易于上手的工具,为开发者提供了丰富的功能和良好的开发体验,同时也为用户提供了更多丰富多样的小程序应用。 ### 回答3: 微信小程序框架是一种用于开发微信小程序的软件开发框架。它提供了一套完整的开发工具和组件库,使开发者可以使用 HTML、CSS 和 JavaScript 来构建小程序。 首先,微信小程序框架具有跨平台的特点。开发者只需要通过微信开发者工具即可编写小程序代码,而不需要针对不同的操作系统进行开发。这大大简化了开发流程和成本,并提高了开发效率。 其次,小程序框架具有丰富的组件库。开发者可以通过简单的代码调用各种组件,如按钮、列表、输入框等,来快速构建小程序界面。这些组件经过优化,能够在微信客户端中实现良好的用户体验。 再次,小程序框架提供了强大的接口能力。开发者可以使用框架提供的接口,访问微信提供的各种功能和服务,如获取用户信息、定位、支付等。这样,开发者可以充分发挥自己的创意和想象力,将丰富的功能集成到小程序中。 此外,微信小程序框架还提供了丰富的开发文档和示例,方便开发者学习和参考。开发者可以根据自己的需求,灵活运用框架提供的功能和工具,进行个性化的开发。 综上所述,微信小程序框架是一种高效、便捷、灵活的开发工具,为开发者提供了一种快速开发小程序的方式,并且能够实现丰富的功能和良好的用户体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值