欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android 关于封装tabbar,任意修改样式

程序员文章站 2022-06-01 22:22:35
...

android 关于封装tabbar,任意修改样式


主要代码:

package com.sun.framework.CustomizeVC;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sun.framework.Utils.ScreenUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public class TabBar extends LinearLayout {

    public int mSelectTextColor;//default Color.WHITE 选择字体颜色
    public int mUnSelectTextColor;//default Color.parseColor("#808080") 灰色 没有选择字体颜色
    public int mUnSelectTabItemBgColor;//default Color.argb(0,0,0,0) 没有选中的item背景颜色
    public int mSelectTabVItemBgColor;//default Color.argb(0,0,0,0) 选中的item背景颜色
    public int mDefaultSelectIndex;//default 0

    public Boolean mIsFullScreen;//default false 是否全屏
    public Boolean mIsFragRemove;//default false 使用Remove frag的逻辑  true 使用hide frag的逻辑
    public Boolean mIsShowTabBarView;//default true 是否显示TabBarView

    public int[] mSelectImageVIconArray;
    public int[] mUnSelectImageVIconArray;

    public int[] mSelectTextColorArray;
    public int[] mUnSelectTextColorArray;

    public Class<?>[] mTabFragmentClassArray;
    public String[] mTextVTitleArray;

    public float mTabBarHeight; //default 49
    public float mTabBar_ImageV_Height; //default 25
    public float mTabBar_ImageV_Width;//default 25
    public float mTabBar_TextV_Height;//default WRAP_CONTENT
    public float mTabBar_TextV_Width;//default WRAP_CONTENT
    public int mTabBarViewBackgroundColor;//default Color.rgb(0,0,0) tabbar背景颜色

    private List<Fragment> fragLst;
    private Map<Object, Fragment> tabFragS;
    private FrameLayout tabFragContainer;
    public LinearLayout tabBarView;
    private List<View> tabLst;
    private int preSelectIndex = -1;
    private Context context;
    private ScreenUtils screenUtils;
    private OnTabSelectedChangingListener onTabSelectedChangingListener;

    public TabBar(Context context) {
        super(context);
        this.context = context;
        commonInit();
    }

    public TabBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        commonInit();
    }

    public TabBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

    private void commonInit() {
        this.setOrientation(VERTICAL);
        screenUtils = new ScreenUtils(context);
        mIsFullScreen = false;
        mIsFragRemove = false;
        mIsShowTabBarView = true;
        mTabBarHeight = 49;//ViewGroup.LayoutParams.WRAP_CONTENT;
        mTabBar_ImageV_Height = 25;
        mTabBar_ImageV_Width = 25;
        mTabBar_TextV_Height = ViewGroup.LayoutParams.WRAP_CONTENT;
        mTabBar_TextV_Width = ViewGroup.LayoutParams.WRAP_CONTENT;
        mTabBarViewBackgroundColor = Color.rgb(0, 0, 0);
        mUnSelectTabItemBgColor = Color.argb(0, 0, 0, 0);
        mSelectTabVItemBgColor = Color.argb(0, 0, 0, 0);
        mUnSelectTextColor = Color.parseColor("#808080");
        mSelectTextColor = Color.WHITE;
        mDefaultSelectIndex = 0;
    }

    public void refreshTabBar() {

        if (this.getChildCount() > 0) {
            this.removeAllViews();
        }

        tabLst = new ArrayList<View>();
        fragLst = new ArrayList<Fragment>();
        tabFragS = new HashMap<Object, Fragment>();

        tabFragContainer = new FrameLayout(context);

        tabFragContainer.setBackgroundColor(Color.WHITE);

        if (mIsShowTabBarView) {
            tabBarView = new LinearLayout(context);
            tabBarView.setBackgroundColor(mTabBarViewBackgroundColor);
        }
        if (!mIsFullScreen) {
            this.addView(tabFragContainer);
            if (mIsShowTabBarView) {
                this.addView(tabBarView);
            }
        } else {
            RelativeLayout relativeLayout = new RelativeLayout(context);
            LinearLayout linearLayout = new LinearLayout(context);
            linearLayout.addView(tabFragContainer);
            relativeLayout.addView(linearLayout);

            RelativeLayout.LayoutParams relativeLayoutLayoutParams = (RelativeLayout.LayoutParams) linearLayout.getLayoutParams();
            relativeLayoutLayoutParams.height = LayoutParams.MATCH_PARENT;
            relativeLayoutLayoutParams.width = LayoutParams.MATCH_PARENT;

            if (mIsShowTabBarView) {
                LinearLayout linearLayout2 = new LinearLayout(context);
                linearLayout2.addView(tabBarView);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
                linearLayout2.setLayoutParams(params);
                relativeLayout.addView(linearLayout2);
            }
            this.addView(relativeLayout);

        }
        if (mIsShowTabBarView) {
            LayoutParams tabContLayoutParams = (LayoutParams) tabBarView.getLayoutParams();
            tabContLayoutParams.height = screenUtils.dp2px(mTabBarHeight);
            tabContLayoutParams.width = LayoutParams.MATCH_PARENT;
            //tabContLayoutParams.weight=0;
        }

        LayoutParams tabPageContLayoutParams = (LayoutParams) tabFragContainer.getLayoutParams();
        tabPageContLayoutParams.height = LayoutParams.MATCH_PARENT;
        tabPageContLayoutParams.width = LayoutParams.MATCH_PARENT;
        tabPageContLayoutParams.weight = 1;

        if (mIsShowTabBarView) {

            for (int i = 0; i < mSelectImageVIconArray.length; i++)
                AddTab(i);

            if (mDefaultSelectIndex != -1)
                On_TablClick_Listner.onClick(tabLst.get(mDefaultSelectIndex));

        } else {

            AddTabFrag(mTabFragmentClassArray[mDefaultSelectIndex]);
        }

    }


    public void setOnTabSelectedChangingListener(
            OnTabSelectedChangingListener onTabSelectedChangingListener) {
        this.onTabSelectedChangingListener = onTabSelectedChangingListener;
    }

    public void SwitchTabBar(int tabBarIndex) {

        if (mIsShowTabBarView) {
            if (tabBarIndex < 0 || tabBarIndex > tabLst.size())
                return;
            View sender = tabLst.get(tabBarIndex);
            this.On_TablClick_Listner.onClick(sender);
        } else {
            AddTabFrag(mTabFragmentClassArray[tabBarIndex]);

        }
    }

    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        this.setOrientation(LinearLayout.VERTICAL);

    }

    Fragment currentFrag;

    private void AddTabFrag(Class<?> fragClass) {

        FragmentManager fm = ((FragmentActivity) this.getContext()).getSupportFragmentManager();
        FragmentTransaction tran = fm.beginTransaction();

        if (mIsFragRemove) {
            if (currentFrag != null) {
                tran.remove(currentFrag);
            }

            FrameLayout layout = new FrameLayout(this.getContext());
            layout.setId(ViewIdGenerator.generateViewId());
            tabFragContainer.addView(layout);
            layout.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
            layout.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;

            Fragment frag = null;
            try {
                frag = (Fragment) fragClass.newInstance();
                tran.add(layout.getId(), frag);
                tran.show(frag);
                currentFrag = frag;

            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } else {

            Collection<Fragment> aCollection = tabFragS.values();
            for (Fragment fragment : aCollection) {
                // if (fragment.isVisible()) {
                tran.hide(fragment);
                // }
            }
            try {
                Fragment frag = tabFragS.get(fragClass.getName());

                if (frag == null) {
                    FrameLayout layout = new FrameLayout(this.getContext());
                    layout.setId(ViewIdGenerator.generateViewId());
                    tabFragContainer.addView(layout);
                    layout.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
                    layout.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
                    frag = (Fragment) fragClass.newInstance();
                    tran.add(layout.getId(), frag);
                    fragLst.add(frag);
                    tabFragS.put(fragClass.getName(), frag);
                }
                tran.show(frag);

            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        //tran.addToBackStack(null);
        tran.commit();
    }

    private void AddTab(int index) {
        LinearLayout tab = new LinearLayout(this.getContext());
        tab.setOrientation(VERTICAL);
        tab.setBackgroundColor(mUnSelectTabItemBgColor);
        tabBarView.addView(tab);

        LayoutParams params = (LayoutParams) tab.getLayoutParams();
        params.weight = 1;
        params.height = screenUtils.dp2px(mTabBarHeight);
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        tab.setClickable(true);

        ImageView img = new ImageView(getContext());
        tab.addView(img);
        params = (LayoutParams) img.getLayoutParams();
        params.width = screenUtils.dp2px(mTabBar_ImageV_Width);
        params.height = screenUtils.dp2px(mTabBar_ImageV_Height);
        params.gravity = Gravity.CENTER;
        params.topMargin = screenUtils.dp2px(5);
        img.setImageResource(mUnSelectImageVIconArray[index]);

        TextView tv = new TextView(getContext());
        tab.addView(tv);
        params = (LayoutParams) tv.getLayoutParams();
        params.width = screenUtils.dp2px(mTabBar_TextV_Width);
        params.height = screenUtils.dp2px(mTabBar_TextV_Height);
        params.gravity = Gravity.CENTER;
        params.bottomMargin = screenUtils.dp2px(5);
        if (mUnSelectTextColorArray != null) {
            tv.setTextColor(mUnSelectTextColorArray[index]);
        } else {
            tv.setTextColor(mUnSelectTextColor);
        }
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        tv.setText(mTextVTitleArray[index]);

        tab.setOnClickListener(On_TablClick_Listner);
        tabLst.add(tab);
    }

    OnClickListener On_TablClick_Listner = new OnClickListener() {

        @Override
        public void onClick(View v) {
            int index = tabLst.indexOf(v);
            if (preSelectIndex == index) return;
            if (onTabSelectedChangingListener != null && !onTabSelectedChangingListener.OnTabSelectedChanging(v, index))
                return;


            if (preSelectIndex != -1) {
                ViewGroup tab = (ViewGroup) tabLst.get(preSelectIndex);
                tab.setBackgroundColor(mUnSelectTabItemBgColor);
                ((ImageView) tab.getChildAt(0)).setImageResource(mUnSelectImageVIconArray[preSelectIndex]);
                int color;
                if (mUnSelectTextColorArray != null) {
                    color = mUnSelectTextColorArray[preSelectIndex];
                } else {
                    color = mUnSelectTextColor;
                }
                ((TextView) tab.getChildAt(1)).setTextColor(color);
            }

            preSelectIndex = index;
            ViewGroup currTab = (ViewGroup) v;
            currTab.setBackgroundColor(mSelectTabVItemBgColor);
            ((ImageView) currTab.getChildAt(0)).setImageResource(mSelectImageVIconArray[preSelectIndex]);
            int color;
            if (mSelectTextColorArray != null) {
                color = mSelectTextColorArray[preSelectIndex];
            } else {
                color = mSelectTextColor;
            }
            ((TextView) currTab.getChildAt(1)).setTextColor(color);
            AddTabFrag(mTabFragmentClassArray[index]);
        }
    };

    public interface OnTabSelectedChangingListener {
        boolean OnTabSelectedChanging(View sender, int index);
    }


    public static class ViewIdGenerator {
        private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);

        public static int generateViewId() {

            if (Build.VERSION.SDK_INT < 17) {
                for (; ; ) {
                    final int result = sNextGeneratedId.get();
                    // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
                    int newValue = result + 1;
                    if (newValue > 0x00FFFFFF)
                        newValue = 1; // Roll over to 1, not 0.
                    if (sNextGeneratedId.compareAndSet(result, newValue)) {
                        return result;
                    }
                }
            } else {
                return View.generateViewId();
            }

        }
    }
}



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data class="MainBinding">

    </data>

    <LinearLayout
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.sun.framework.CustomizeVC.TabBar
            android:id="@+id/tabBar"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </com.sun.framework.CustomizeVC.TabBar>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

        </RelativeLayout>


    </LinearLayout>



使用:

package com.example.apple.test4;

import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import com.example.apple.test4.databinding.MainBinding;
import com.sun.framework.CustomizeVC.SubFragmentActivity;
import com.sun.framework.CustomizeVC.TabBar;

public class MainActivity extends SubFragmentActivity {
    MainBinding binding;
    public TabBar tabBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        tabBar = binding.tabBar;
        tabBar.mSelectTextColorArray = new int[]{Color.RED,Color.RED,Color.GREEN,Color.YELLOW,Color.WHITE};
        //tabBar.isShowTabBarView = false;
        //tabBar.mDefaultSelectIndex = 4;
        tabBar.mTextVTitleArray = new String[]{"预报","列表","首页","地图","设置"};
        tabBar.mSelectImageVIconArray = new int[]{R.drawable.ditu_hov,R.drawable.ditu_hov,R.drawable.ditu_hov,R.drawable.ditu_hov,R.drawable.ditu_hov};
        tabBar.mUnSelectImageVIconArray = new int[]{R.drawable.ditu_hov,R.drawable.ditu_hov,R.drawable.ditu_hov,R.drawable.ditu_hov,R.drawable.ditu_hov};
        tabBar.mTabFragmentClassArray = new Class<?>[]{ForecastFragment.class,HomeFragment.class,ForecastFragment.class,ForecastFragment.class,ForecastFragment.class};
        tabBar.refreshTabBar();
    }

}

我的业余技术微信公众号:YKJGZH,欢迎大家进入

相关标签: tabbar android