栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

用Android Studio实现仿微信APP门户界面设计

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用Android Studio实现仿微信APP门户界面设计

实验环境:

windows操作系统、Android Studio

界面展示:

 

 

代码实现: 顶部top.xml文件:



    

注意layout_height不能设置为match_parent,否则bottom无法显示

底端bottom.xml文件



    

        

        


    


这里只给出了第一个垂直LinearLayout-聊天菜单,其余三个与之类似,注意修改id。

中间内容fragment.xml文件



    
    

这里也只给出了第一个分段

整体布局activity_main.xml文件




    

    

    



weixin_liaotian.java文件
package com.example.project1;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

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


public class weixin_liaotian extends Fragment {



    public weixin_liaotian() {
        // 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_liaotian, container, false);
    }
}

这里也只给出聊天分段的文件

主函数MainActivity.java文件
package com.example.project1;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.Color;
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{


    //Fragment
    private Fragment fragment_first=new weixin_liaotian();
    private Fragment fragment_second=new weixin_lianxiren();
    private Fragment fragment_third=new weixin_pengyouquan();
    private Fragment fragment_fourth=new weixin_shezhi();

    //底端菜单栏LinearLayout
    private LinearLayout linear_first;
    private LinearLayout linear_second;
    private LinearLayout linear_third;
    private LinearLayout linear_fourth;

    //底端菜单栏中的Imageview
    private ImageView imageView_first;
    private ImageView imageView_second;
    private ImageView imageView_third;
    private ImageView imageView_fourth;

    //底端菜单栏中的TextView
    private TextView textView_first;
    private TextView textView_second;
    private TextView textView_third;
    private TextView textView_fourth;


    //FragmentManager
    private FragmentManager fragmentManager;

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

        initView();
        initFragment();
        initEvent();
        selectFragment(0);
        //将第一个图标设为选中状态
        imageView_first.setImageResource(R.drawable.chat);
        textView_first.setTextColor(getResources().getColor(R.color.select));
    }

    @Override
    public void onClick(View view) {
        //每次点击之后,将所有的ImageView和TextView设置为未选中
        restartButton();

        switch(view.getId())
        {
            case R.id.chat:
                //选择所点击的菜单对应的图层片段
                selectFragment(0);
                //将该菜单的点击状态置为点击态
                imageView_first.setImageResource(R.drawable.chat_green);
                textView_first.setTextColor(getResources().getColor(R.color.select));
                break;
            case R.id.contacts:
                selectFragment(1);
                imageView_second.setImageResource(R.drawable.contacts_green);
                textView_second.setTextColor(getResources().getColor(R.color.select));
                break;
            case R.id.circle_friend:
                selectFragment(2);
                imageView_third.setImageResource(R.drawable.circle_people_green);
                textView_third.setTextColor(getResources().getColor(R.color.select));
                break;
            case R.id.settings:
                selectFragment(3);
                imageView_fourth.setImageResource(R.drawable.settings_green);
                textView_fourth.setTextColor(getResources().getColor(R.color.select));
                break;
            default:
                break;
        }
    }

    //重置菜单的点击状态,设为未点击
    private void restartButton() {
        //设置为未点击状态
        //第一片段
        imageView_first.setImageResource(R.drawable.chat);
        textView_first.setTextColor(getResources().getColor(R.color.white));
        //第二片段
        imageView_second.setImageResource(R.drawable.contacts);
        textView_second.setTextColor(getResources().getColor(R.color.white));
        //第三片段
        imageView_third.setImageResource(R.drawable.circle_people);
        textView_third.setTextColor(getResources().getColor(R.color.white));
        //第四片段
        imageView_fourth.setImageResource(R.drawable.settings);
        textView_fourth.setTextColor(getResources().getColor(R.color.white));
    }

    //初始化中间的部分的图层片段
    private void initFragment(){
        fragmentManager=getSupportFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        transaction.add(R.id.frame_content,fragment_first);
        transaction.add(R.id.frame_content,fragment_second);
        transaction.add(R.id.frame_content,fragment_third);
        transaction.add(R.id.frame_content,fragment_fourth);
        //提交事务
        transaction.commit();
    }

    //初始化各底端的LinearLayout、ImageView和TextView组件
    private  void initView(){
        linear_first=findViewById(R.id.chat);
        linear_second=findViewById(R.id.contacts);
        linear_third=findViewById(R.id.circle_friend);
        linear_fourth=findViewById(R.id.settings);

        imageView_first=findViewById(R.id.imageView1);
        imageView_second=findViewById(R.id.imageView2);
        imageView_third=findViewById(R.id.imageView3);
        imageView_fourth=findViewById(R.id.imageView4);

        textView_first=findViewById(R.id.textView1);
        textView_second=findViewById(R.id.textView2);
        textView_third=findViewById(R.id.textView3);
        textView_fourth=findViewById(R.id.textView4);

    }

    //初始化点击监听事件
    private void initEvent(){
        linear_first.setonClickListener(this);
        linear_second.setonClickListener(this);
        linear_third.setonClickListener(this);
        linear_fourth.setonClickListener(this);
    }

    //隐藏所有图层分段
    private void hideView(FragmentTransaction transaction){
        transaction.hide(fragment_first);
        transaction.hide(fragment_second);
        transaction.hide(fragment_third);
        transaction.hide(fragment_fourth);
    }

    //选择相应的图层分段
    private void selectFragment(int i){
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        //调用隐藏所有图层函数
        hideView(transaction);
        switch (i){
            case 0:
                transaction.show(fragment_first);

                break;
            case 1:
                transaction.show(fragment_second);

                break;
            case 2:
                transaction.show(fragment_third);

                break;
            case 3:
                transaction.show(fragment_fourth);

                break;
            default:
                break;
        }
        //提交转换事务
        transaction.commit();

    }

}

这个文件实现了Fragment的切换、底部的click事件以及图标、文字的颜色切换

除此之外,还有drawable文件夹中的图标文件和value文件夹中的colors文件,具体代码可以在代码仓库查到。

小结:

在本次实验中,通过编辑bottom.xml和top.xml文件完成了底部和顶部的实现,通过编辑Fragment文件实现了中间界面的展示和隐藏,在MainActivity.java中实现了大部分功能,在阿里矢量库找到了需要的图标。实验中产生了许多问题,通过查阅其他人的博客解决了问题。

代码已上传至gitee

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/305390.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号