IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> 微信的门户页面框架设计(AS) -> 正文阅读

[移动开发]微信的门户页面框架设计(AS)

一.设计目标:

1.完成基本的微信页面布局设计,包括顶部显示栏,和底部导航栏(四个tab页包括微信、朋友、发现、我)。

2.利用fragment(分段)、activity完善门户页面的框架设计,实现四个页面的切换,点击相应的导航键,便可以切换到该页面,相应的图标也发生改变,并对控件进行监听。

二.设计流程:

1.制作底部导航栏bottom.xml

?点击 res-layout 新建xml文件

8cb1bba325804840984ddcadcb4b6b5d.png

bottom平均分成四块,每块由一个图片和一个文字构成。可以用水平的LinearLayout中再包裹一个竖直的LinearLayout,被包裹的每个LinearLayout里包含一个ImageButton和TextView控件。

准备两组八张图标存放在res-drawable。

5cde488662a74b27826649112df96ff3.png

?添加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="100dp"
    android:background="@color/purple_200"
    android:clickable="false"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/tab_weixin"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:clickable="true"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="57dp"
            android:src="@drawable/a_original"
            tools:srcCompat="@drawable/a_original" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="微信"
            android:textColor="@color/black"
            android:textSize="23sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/tab_pengyou"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:clickable="true"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="103dp"
            android:layout_height="60dp"
            android:src="@drawable/b_original"
            tools:srcCompat="@drawable/b_original" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="朋友"
            android:textColor="@color/black"
            android:textSize="23sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/tab_faxian"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:clickable="true"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="103dp"
            android:layout_height="63dp"
            android:src="@drawable/c_original"
            tools:srcCompat="@drawable/c_original" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="发现"
            android:textColor="@color/black"
            android:textSize="23sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/tab_shezhi"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:clickable="true"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="match_parent"
            android:layout_height="62dp"
            android:src="@drawable/d_original"
            tools:srcCompat="@drawable/d_original" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="我"
            android:textColor="@color/black"
            android:textSize="23sp" />
    </LinearLayout>

</LinearLayout>

代码解析:

外部LinearLayout:android:orientation=“horizontal”,布局中组件的排列方式,有horizontal(水平)和vertical(垂直)两种方式。

内部ImageView:tools:srcCompat="@drawable/tab_weixin_normal",vector绘制图片的地址。
?

功能说明(布局展示):

39b738e66dcc4c7bab5df44206a5129f.png

2.制作顶部的top.xml,在res-layout新建一个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="wrap_content"
    android:background="@color/purple_200">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="微信"
        android:textColor="@color/black"
        android:textSize="30sp" />
</LinearLayout>

?代码解析:

android:id="@+id/textView2",为组件设置一个资源id,在java文件中可以通过findViewById(id)找到该组件。

android:layout_width=“wrap_content”,布局的宽度,通常不直接写数字,用wrap_content(组件实际大小),fill_parent或者match_parent填满父容器。

android:layout_height=“wrap_content”,布局的高度,参数同上。

android:layout_weight=“1”,用来等比例地划分区域。

android:background="@color/purple_200",为组件设置一个背景图片,或者直接用颜色覆盖。

android:gravity=“center”,表示textView中的文字相对于TextView的对齐方式。

android:text=“微信”,要显示的文字。

android:textColor="@color/black",设置文字的颜色。

android:textSize=“30sp” />,设置文字的大小。

功能说明(布局展示):

5cec6316ba45426aa0ccebe4ea444227.png

3.activity_main的配置

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

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

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


    </FrameLayout>

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

?代码解析:

相关的属性作用和bottom.xml以及top.xml类似。

功能说明(布局展示):

对主页面显示的内容布局。整个主页面使用竖直的LinerLayout,将top.xml和bottom.xml导入,以及中间内容content(对应Fragment)。

ce8e4384217448438585df9581e691eb.png

4.创建Fragment

在java目录下创建四个fragment类,WeChatBlankFragment(微信)、FriBlankFragment(朋友)、FoundBlankFragment(发现)、MeBlankFragment(我),系统会自动在res-layout目录下生成四个对应的xml文件(可自主命名),对应bottom的四个按钮。

d7f8c3c08b644f1fb64f0643ebf37c83.png

以FriBlankFragment(朋友)为例,配置相关代码

package com.example.wechat;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

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


public class FriBlankFragment extends Fragment {



    public FriBlankFragment() {
        // 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.tab02, container, false);
    }
}

配置FriBlankFragment对应的xml文件代码

<?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=".FriBlankFragment">

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

</LinearLayout>

5.配置MainActivity

5.1声明所需要的变量

    private Fragment hTab01 = new WeChatBlankFragment();
    private Fragment hTab02 = new FriBlankFragment();
    private Fragment hTab03 = new FoundBlankFragment();
    private Fragment hTab04 = new MeBlankFragment();

    private LinearLayout hTabWeChat;
    private LinearLayout hTabFrd;
    private LinearLayout hTabContact;
    private LinearLayout hTabMe;

    private ImageView imageWeixin,imagepengyou,imagefaxian, imagewo;

    private FragmentManager fm;
    private TextView textView;

代码解析:

Fragment变量:对应创建的Fragment? ,FragmentManager变量:管理Fragment的类
ImageView变量:对应之前创建的ImageView,LinearLayout变量:对应之前创建的LinearLayout

5.2 利用FragmentManager对Fragment进行管理,实现页面切换

将所有的Fragment添加进去:

    private void initFragment() {
        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.content, hTab01);
        transaction.add(R.id.content, hTab02);
        transaction.add(R.id.content, hTab03);
        transaction.add(R.id.content, hTab04);
        transaction.commit();
    }

页面的切换(功能):

    private void showfragment(int i){
        FragmentTransaction transaction=fm.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                textView.setText("微信");
                transaction.show(hTab01);
                imageWeixin.setImageResource(R.drawable.a_nomal);
                break;

            case 1:
                textView.setText("朋友");
                transaction.show(hTab02);
                imagepengyou.setImageResource(R.drawable.b_nomal);
                break;

            case 2:
                textView.setText("发现");
                transaction.show(hTab03);
                imagefaxian.setImageResource(R.drawable.c_nomal);
                break;

            case 3:
                textView.setText("我");
                transaction.show(hTab04);
                imagewo.setImageResource(R.drawable.d_nomal);
                break;

            default:
                break;
        }
        transaction.commit();
    }

为了使所有Fragment不会发生重叠,将所有Fragment都隐藏(功能):

    private void hideFragment(FragmentTransaction transaction) {
        transaction.hide(hTab01);
        transaction.hide(hTab02);
        transaction.hide(hTab03);
        transaction.hide(hTab04);

    }

5.3 完成事件的触发

事件触发代码:

   private void initEvent() {
        hTabWeChat.setOnClickListener(this);
        hTabFrd.setOnClickListener(this);
        hTabContact.setOnClickListener(this);
        hTabMe.setOnClickListener(this);
    }

点击相应按钮的响应函数

    public void onClick(View v) {
        resetImage();
        switch (v.getId()){
            case R.id.tab_weixin:
                showfragment(0);
                break;
            case R.id.tab_pengyou:
                showfragment(1);
                break;
            case R.id.tab_faxian:
                showfragment(2);
                break;
            case R.id.tab_shezhi:
                showfragment(3);
                break;
            default:
                break;
        }

5.4 实现点击后的按钮切换

    public void resetImage(){
        imageWeixin.setImageResource(R.drawable.a_original);
        imagepengyou.setImageResource(R.drawable.b_original);
        imagefaxian.setImageResource(R.drawable.c_original);
        imagewo.setImageResource(R.drawable.d_original);
    }

5.5 主函数调用

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        textView=findViewById(R.id.textView2);

        hTabWeChat =findViewById(R.id.tab_weixin);
        hTabFrd =findViewById(R.id.tab_pengyou);
        hTabContact =findViewById(R.id.tab_faxian);
        hTabMe =findViewById(R.id.tab_shezhi);

        imageWeixin=findViewById(R.id.imageView);
        imagepengyou=findViewById(R.id.imageView1);
        imagefaxian=findViewById(R.id.imageView2);
        imagewo =findViewById(R.id.imageView3);

        initEvent();
        initFragment();
        showfragment(0);
    }

5.6 完整的MainActivity代码

package com.example.wechat;

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.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Fragment hTab01 = new WeChatBlankFragment();
    private Fragment hTab02 = new FriBlankFragment();
    private Fragment hTab03 = new FoundBlankFragment();
    private Fragment hTab04 = new MeBlankFragment();

    private LinearLayout hTabWeChat;
    private LinearLayout hTabFrd;
    private LinearLayout hTabContact;
    private LinearLayout hTabMe;

    private ImageView imageWeixin,imagepengyou,imagefaxian, imagewo;

    private FragmentManager fm;
    private TextView textView;

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

        textView=findViewById(R.id.textView2);

        hTabWeChat =findViewById(R.id.tab_weixin);
        hTabFrd =findViewById(R.id.tab_pengyou);
        hTabContact =findViewById(R.id.tab_faxian);
        hTabMe =findViewById(R.id.tab_shezhi);

        imageWeixin=findViewById(R.id.imageView);
        imagepengyou=findViewById(R.id.imageView1);
        imagefaxian=findViewById(R.id.imageView2);
        imagewo =findViewById(R.id.imageView3);

        initEvent();
        initFragment();
        showfragment(0);
    }


    private void initFragment() {
        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.add(R.id.content, hTab01);
        transaction.add(R.id.content, hTab02);
        transaction.add(R.id.content, hTab03);
        transaction.add(R.id.content, hTab04);
        transaction.commit();
    }

    private void initView() {
        hTabWeChat = (LinearLayout) findViewById(R.id.tab_weixin);
        hTabFrd = (LinearLayout) findViewById(R.id.tab_pengyou);
        hTabContact = (LinearLayout) findViewById(R.id.tab_faxian);
        hTabMe = (LinearLayout) findViewById(R.id.tab_shezhi);

    }


    private void showfragment(int i){
        FragmentTransaction transaction=fm.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                textView.setText("微信");
                transaction.show(hTab01);
                imageWeixin.setImageResource(R.drawable.a_nomal);
                break;

            case 1:
                textView.setText("朋友");
                transaction.show(hTab02);
                imagepengyou.setImageResource(R.drawable.b_nomal);
                break;

            case 2:
                textView.setText("发现");
                transaction.show(hTab03);
                imagefaxian.setImageResource(R.drawable.c_nomal);
                break;

            case 3:
                textView.setText("我");
                transaction.show(hTab04);
                imagewo.setImageResource(R.drawable.d_nomal);
                break;

            default:
                break;
        }
        transaction.commit();
    }


    private void hideFragment(FragmentTransaction transaction) {
        transaction.hide(hTab01);
        transaction.hide(hTab02);
        transaction.hide(hTab03);
        transaction.hide(hTab04);

    }

    private void initEvent() {
        hTabWeChat.setOnClickListener(this);
        hTabFrd.setOnClickListener(this);
        hTabContact.setOnClickListener(this);
        hTabMe.setOnClickListener(this);
    }

    public void onClick(View v) {
        resetImage();
        switch (v.getId()){
            case R.id.tab_weixin:
                showfragment(0);
                break;
            case R.id.tab_pengyou:
                showfragment(1);
                break;
            case R.id.tab_faxian:
                showfragment(2);
                break;
            case R.id.tab_shezhi:
                showfragment(3);
                break;
            default:
                break;
        }

    }
    public void resetImage(){
        imageWeixin.setImageResource(R.drawable.a_original);
        imagepengyou.setImageResource(R.drawable.b_original);
        imagefaxian.setImageResource(R.drawable.c_original);
        imagewo.setImageResource(R.drawable.d_original);
    }
}

三 运行结果

3.1运行环境:

计算机型号:联想拯救者 R7000P 2020H

处理器:AMD Ryzen 7 4800H with Radeon Graphics

内存:16.0 GB (15.9 GB 可用)

操作系统:Windows 11 家庭中文版

浏览器:Microsoft Edge 102.0.1245.30(64-bit)

编译器:Android Studio 2021.2.0.0

3.2 运行结果:

运行后点击微信、朋友、发现、我的效果,可以看到界面和图标都发生了相应的切换。

a48949a9acbb4c4894ef459e0a6b30f2.png0fdd551e4d2c4dd89da801ae9fd6a70b.png800051f0a51c4831bcac6d650a6c3c5f.png88cb05703a54423c890c9025f36aa605.png

?四 源码仓库

WeChat: 微信门户页面设计(第一次作业) (gitee.com)https://gitee.com/xiaohutongxue80/WeChat

?

?

?

?

?

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2022-10-08 20:53:18  更:2022-10-08 20:56:17 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 0:34:07-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码