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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值