Android 底部导航栏

implementation 'com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar'

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.flyco.tablayout.CommonTabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_alignParentBottom="true"
        android:background="#fff"
        app:tl_iconHeight="20dp"
        app:tl_iconVisible="true"
        app:tl_iconWidth="20dp"
        app:tl_indicator_anim_duration="2"
        app:tl_indicator_anim_enable="true"
        app:tl_indicator_bounce_enable="true"
        app:tl_textSelectColor="#FF3333"
        app:tl_textUnselectColor="#666666"
        app:tl_textsize="12sp" />

</RelativeLayout>

MainActivity.java :

package com.example.myapplication6;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements OnTabSelectListener {
    private FragmentManager mFragmentManager;
    private Fragment fragment_shouye,fragment_fenlei,fragment_zixun,fragment_wode;
    private FragmentTransaction fragmentTransaction;
    ArrayList<CustomTabEntity> tabs = new ArrayList<>();
    String [] titles={"首页","消息","进货单","我的"};
    int []  icons_f={R.drawable.ic_icon__shouye,R.drawable.ic_icon__shouye,R.drawable.ic_icon__shouye,R.drawable.ic_icon__shouye};
    int []  icons={R.drawable.ic_icon_shouye_f,R.drawable.ic_icon_shouye_f,R.drawable.ic_icon_shouye_f,R.drawable.ic_icon_shouye_f};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mFragmentManager=getSupportFragmentManager();
        fragmentTransaction=mFragmentManager.beginTransaction();
        hideAllFragment(fragmentTransaction);
        //默认为首页
        if (fragment_shouye==null){
            fragment_shouye=new OneFragment();
            fragmentTransaction.add(R.id.frame_layout,fragment_shouye);
        }else {
            fragmentTransaction.show(fragment_shouye);
        }
        fragmentTransaction.commit();
        //init();

        for (int i=0;i<4;i++){
            MainTabEntity mainTabEntity = new MainTabEntity(titles[i],icons[i],icons_f[i]);
            tabs.add(mainTabEntity);
        }

        CommonTabLayout commonTabLayout = findViewById(R.id.tab_layout);
        commonTabLayout.setTabData(tabs);
        commonTabLayout.setOnTabSelectListener(this);


        int backStackCount = getFragmentManager().getBackStackEntryCount();
        for(int i = 0; i < backStackCount; i++) {
            getFragmentManager().popBackStack();
        }
    }
    @Override
    public void onTabSelect(int position) {
        FragmentTransaction fTransaction1 = mFragmentManager.beginTransaction();
        hideAllFragment(fTransaction1);
        switch (position){
            case 0:
                if (fragment_shouye==null){
                    fragment_shouye=new OneFragment();
                    fTransaction1.add(R.id.frame_layout,fragment_shouye);
                }else {
                    fTransaction1.show(fragment_shouye);
                }
                break;
            case 1:
                if (fragment_fenlei==null){
                    fragment_fenlei=new TwoFragment();
                    fTransaction1.add(R.id.frame_layout,fragment_fenlei);
                }else {
                    fTransaction1.show(fragment_fenlei);
                }
                break;
            case 2:
                if (fragment_zixun==null){
                    fragment_zixun=new ThreeFragment();
                    fTransaction1.add(R.id.frame_layout,fragment_zixun);
                }else {
                    fTransaction1.show(fragment_zixun);
                }
                break;
            case 3:
                if (fragment_wode==null){
                    fragment_wode=new FourFragment();
                    fTransaction1.add(R.id.frame_layout,fragment_wode);
                }else {
                    fTransaction1.show(fragment_wode);
                }
                break;

        }
        fTransaction1.commit();
    }

    @Override
    public void onTabReselect(int position) {

    }

    //隐藏所有Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction){
        if(fragment_shouye != null)fragmentTransaction.hide(fragment_shouye);
        if(fragment_fenlei != null)fragmentTransaction.hide(fragment_fenlei);
        if(fragment_zixun != null)fragmentTransaction.hide(fragment_zixun);
        if(fragment_wode != null)fragmentTransaction.hide(fragment_wode);
    }


}
MainTabEntity.java :
package com.example.myapplication6;


import com.flyco.tablayout.listener.CustomTabEntity;

/**
 * Created by 5499 on 2018/10/17.
 */

public class MainTabEntity implements CustomTabEntity {
    String title;
    int selectedIcon;
    int unSelectIcon;


    public MainTabEntity(String title, int selectedIcon, int unSelectIcon) {
        this.title = title;
        this.selectedIcon = selectedIcon;
        this.unSelectIcon = unSelectIcon;
    }

    @Override
    public String getTabTitle() {
        return title;
    }

    @Override
    public int getTabSelectedIcon() {
        return selectedIcon;
    }

    @Override
    public int getTabUnselectedIcon() {
        return unSelectIcon;
    }
}

以前用过的一种方式。

猜你喜欢

转载自blog.csdn.net/qq_41334474/article/details/88308748