AS怎么用Fragment做一个微信界面

先上效果图
在这里插入图片描述
我的目录文件
在这里插入图片描述
导入图片到drawable:
链接:https://pan.baidu.com/s/1vZRBVALF6hIlIDgMC2Pd3A
提取码:3m4k
下载好后把文件夹里的图片全部导入到res-drawable目录下

MainActivity.java代码:

package com.example.demo06;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Blank1Fragment f1;
    private Blank2Fragment f2;
    private Blank3Fragment f3;
    private Blank4Fragment f4;

    private LinearLayout foot1;
    private LinearLayout foot2;
    private LinearLayout foot3;
    private LinearLayout foot4;

    private ImageButton mImgWeixin;
    private ImageButton mImgFrd;
    private ImageButton mImgAddress;
    private ImageButton mImgSettings;

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.mylayout);

        foot1 = (LinearLayout) findViewById(R.id.weixin);
        foot2 = (LinearLayout) findViewById(R.id.frd);
        foot3 = (LinearLayout) findViewById(R.id.contacts);
        foot4 = (LinearLayout) findViewById(R.id.settings);
        mImgWeixin = (ImageButton)findViewById(R.id.imageButton_weixin);
        mImgFrd = (ImageButton)findViewById(R.id.imageButton_frd);
        mImgAddress = (ImageButton)findViewById(R.id.imageButton_contacts);
        mImgSettings = (ImageButton)findViewById(R.id.imageButton_settings);

        foot1.setOnClickListener(this);
        foot2.setOnClickListener(this);
        foot3.setOnClickListener(this);
        foot4.setOnClickListener(this);

        initFragment1();
    }

    private void initFragment1(){
        //开启事务,fragment的控制是由事务来实现的
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        //方式(add),初始化fragment并添加到事务中,如果为null就new一个
        if(f1 == null){
           f1 = new Blank1Fragment();
            transaction.add(R.id.main_frame_layout,f1);
        }
        mImgWeixin.setImageResource(R.drawable.tab_weixin_pressed);
        //隐藏所有fragment
        hideFragment(transaction);
        //显示需要显示的fragment
        transaction.show(f1);
        //提交事务
        transaction.commit();
    }
    private void initFragment2(){
        //开启事务,fragment的控制是由事务来实现的
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        //第一种方式(add),初始化fragment并添加到事务中,如果为null就new一个
        if(f2 == null){
            f2 = new Blank2Fragment();
            transaction.add(R.id.main_frame_layout, f2);
        }
        mImgFrd.setImageResource(R.drawable.tab_find_frd_pressed);
        //隐藏所有fragment
        hideFragment(transaction);
        //显示需要显示的fragment
        transaction.show(f2);

        transaction.commit();
    }
    private void initFragment3(){
        //开启事务,fragment的控制是由事务来实现的
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        //第一种方式(add),初始化fragment并添加到事务中,如果为null就new一个
        if(f3 == null){
            f3 = new Blank3Fragment();
            transaction.add(R.id.main_frame_layout, f3);
        }
        mImgAddress.setImageResource(R.drawable.tab_address_pressed);
        //隐藏所有fragment
        hideFragment(transaction);
        //显示需要显示的fragment
        transaction.show(f3);
        //提交事务
        transaction.commit();
    }
    private void initFragment4(){
        //开启事务,fragment的控制是由事务来实现的
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        //第一种方式(add),初始化fragment并添加到事务中,如果为null就new一个
        if(f4 == null){
            f4 = new Blank4Fragment();
            transaction.add(R.id.main_frame_layout, f4);
        }
        mImgSettings.setImageResource(R.drawable.tab_settings_pressed);
        //隐藏所有fragment
        hideFragment(transaction);
        //显示需要显示的fragment
        transaction.show(f4);
        //提交事务
        transaction.commit();
    }
    //隐藏所有的fragment
    private void hideFragment(FragmentTransaction transaction){
        if(f1 != null){
            transaction.hide(f1);
        }
        if(f2 != null){
            transaction.hide(f2);
        }
        if(f3 != null){
            transaction.hide(f3);
        }
        if(f4 != null){
            transaction.hide(f4);
        }
    }

    @Override
    public void onClick(View v) {
        resetimg();
        if(v == foot1){
            initFragment1();
        }else if(v == foot2){
            initFragment2();
        }else if(v == foot3){
            initFragment3();
        }else if(v == foot4){
            initFragment4();
        }
    }

    public void resetimg(){
        mImgWeixin.setImageResource(R.drawable.tab_weixin_normal);
        mImgFrd.setImageResource(R.drawable.tab_find_frd_normal);
        mImgAddress.setImageResource(R.drawable.tab_address_normal);
        mImgSettings.setImageResource(R.drawable.tab_settings_normal);
    }
}

mylayout.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"

    android:orientation="vertical">

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

    <FrameLayout
        android:id="@+id/main_frame_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ffffff"
        tools:ignore="InvalidId">

    </FrameLayout>

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

</LinearLayout>

top.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/top"
    android:layout_width="match_parent"
    android:layout_height="65dp"
    android:layout_gravity="center_horizontal"
    android:background="#dddddd"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"

        android:background="#dddddd"
        android:text="WeChat"
        android:textSize="30sp" />
</LinearLayout>

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:id="@+id/bottom"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="@drawable/bottom_bar"
    android:baselineAligned="false"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/weixin"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/imageButton_weixin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="#000000"
            android:clickable="false"
            app:srcCompat="@drawable/tab_weixin_pressed"
            android:contentDescription="TODO" />

        <TextView
            android:id="@+id/textView_1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:text="微信"
            android:background="#000000"
            android:textColor="#ffffff"
            android:clickable="false"
            android:textSize="15sp"
            android:visibility="visible"
            tools:visibility="visible" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/frd"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/imageButton_frd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#000000"
            android:clickable="false"
            android:contentDescription="@string/todo"
            app:srcCompat="@drawable/tab_find_frd_normal" />

        <TextView
            android:id="@+id/textView_2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:clickable="false"
            android:background="#000000"
            android:text="朋友"
            android:textColor="#ffffff"
            android:textSize="15sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/contacts"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/imageButton_contacts"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#000000"
            android:clickable="false"
            android:contentDescription="TODO"
            app:srcCompat="@drawable/tab_address_normal" />

        <TextView
            android:id="@+id/textView_3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:clickable="false"
            android:text="通讯录"
            android:background="#000000"
            android:textColor="#ffffff"
            android:textSize="15sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/settings"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageButton
            android:id="@+id/imageButton_settings"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#000000"
            android:clickable="false"
            android:contentDescription="TODO"
            app:srcCompat="@drawable/tab_settings_normal" />

        <TextView
            android:id="@+id/textView_4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:clickable="false"
            android:text="设置"
            android:background="#000000"
            android:textColor="#ffffff"
            android:textSize="15sp" />
    </LinearLayout>

</LinearLayout>

Blank1Fragment.java

package com.example.demo06;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import static com.example.demo06.R.id.textView1;

public class Blank1Fragment extends Fragment {

    public Blank1Fragment(){

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_blank1, container, false);
    }
}

fragment_blank1.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:id="@+id/frag1"
    android:layout_width="match_parent"

    android:layout_height="match_parent"
    android:background="#ffffff"
    tools:context=".Blank1Fragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="这是我的微信"
        android:textSize="100px" />

</FrameLayout>

Blank2Fragment.java,Blank3Fragment.java,Blank4Fragment.java代码跟Blank2Fragment.java一样
fragment_blank2.xml,fragment_blank3.xml,fragment_blank4.xml代码跟fragment_blank2.xml一样

ok,完成了!

附上整个项目代码地址:https://gitee.com/zhangjianhui1010/as_wechat_interface

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值