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,完成了!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章