android 底部标签栏CommonTabLayout搭建项目底部菜单(带消息提醒)

本文介绍如何使用自定义组件CommonTabLayout实现Android项目的底部菜单,并带有消息提醒功能。该组件提供了丰富的自定义选项,包括指示器样式、颜色、宽度、高度等,以及文字和图标的相关设置。
摘要由CSDN通过智能技术生成

import android.util.AttributeSet;

import android.util.SparseArray;

import android.util.TypedValue;

import android.view.Gravity;

import android.view.View;

import android.view.ViewGroup;

import android.view.animation.OvershootInterpolator;

import android.widget.FrameLayout;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.TextView;

import java.util.ArrayList;

/** 没有继承HorizontalScrollView不能滑动,对于ViewPager无依赖 */

@SuppressLint(“NewApi”)

public class CommonTabLayout extends FrameLayout implements ValueAnimator.AnimatorUpdateListener {

private Context mContext;

private ArrayList mTabEntitys = new ArrayList<>();

private LinearLayout mTabsContainer;

private int mCurrentTab;

private int mLastTab;

private int mTabCount;

/** 用于绘制显示器 */

private Rect mIndicatorRect = new Rect();

private GradientDrawable mIndicatorDrawable = new GradientDrawable();

private Paint mRectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

private Paint mDividerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

private Paint mTrianglePaint = new Paint(Paint.ANTI_ALIAS_FLAG);

private Path mTrianglePath = new Path();

private static final int STYLE_NORMAL = 0;

private static final int STYLE_TRIANGLE = 1;

private static final int STYLE_BLOCK = 2;

private int mIndicatorStyle = STYLE_NORMAL;

private float mTabPadding;

private boolean mTabSpaceEqual;

private float mTabWidth;

/** indicator */

private int mIndicatorColor;

private float mIndicatorHeight;

private float mIndicatorWidth;

private float mIndicatorCornerRadius;

private float mIndicatorMarginLeft;

private float mIndicatorMarginTop;

private float mIndicatorMarginRight;

private float mIndicatorMarginBottom;

private long mIndicatorAnimDuration;

private boolean mIndicatorAnimEnable;

private boolean mIndicatorBounceEnable;

private int mIndicatorGravity;

/** underline */

private int mUnderlineColor;

private float mUnderlineHeight;

private int mUnderlineGravity;

/** divider */

private int mDividerColor;

private float mDividerWidth;

private float mDividerPadding;

/** title */

private static final int TEXT_BOLD_NONE = 0;

private static final int TEXT_BOLD_WHEN_SELECT = 1;

private static final int TEXT_BOLD_BOTH = 2;

private float mTextsize;

private int mTextSelectColor;

private int mTextUnselectColor;

private int mTextBold;

private boolean mTextAllCaps;

/** icon */

private boolean mIconVisible;

private int mIconGravity;

private float mIconWidth;

private float mIconHeight;

private float mIconMargin;

private int mHeight;

/** anim */

private ValueAnimator mValueAnimator;

private OvershootInterpolator mInterpolator = new OvershootInterpolator(1.5f);

private FragmentChangeManager mFragmentChangeManager;

public CommonTabLayout(Context context) {

this(context, null, 0);

}

public CommonTabLayout(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

@SuppressLint(“NewApi”)

public CommonTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

setWillNotDraw(false);//重写onDraw方法,需要调用这个方法来清除flag

setClipChildren(false);

setClipToPadding(false);

this.mContext = context;

mTabsContainer = new LinearLayout(context);

addView(mTabsContainer);

obtainAttributes(context, attrs);

//get layout_height

String height = attrs.getAttributeValue(“http://schemas.android.com/apk/res/android”, “layout_height”);

//create ViewPager

if (height.equals(ViewGroup.LayoutParams.MATCH_PARENT + “”)) {

} else if (height.equals(ViewGroup.LayoutParams.WRAP_CONTENT + “”)) {

} else {

int[] systemAttrs = {android.R.attr.layout_height};

TypedArray a = context.obtainStyledAttributes(attrs, systemAttrs);

mHeight = a.getDimensionPixelSize(0, ViewGroup.LayoutParams.WRAP_CONTENT);

a.recycle();

}

mValueAnimator = ValueAnimator.ofObject(new PointEvaluator(), mLastP, mCurrentP);

mValueAnimator.addUpdateListener(this);

}

private void obtainAttributes(Context context, AttributeSet attrs) {

TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CommonTabLayout);

mIndicatorStyle = ta.getInt(R.styleable.CommonTabLayout_tl_indicator_style, 0);

mIndicatorColor = ta.getColor(R.styleable.CommonTabLayout_tl_indicator_color, Color.parseColor(mIndicatorStyle == STYLE_BLOCK ? “#4B6A87” : “#ffffff”));

mIndicatorHeight = ta.getDimension(R.styleable.CommonTabLayout_tl_indicator_height,

dp2px(mIndicatorStyle == STYLE_TRIANGLE ? 4 : (mIndicatorStyle == STYLE_BLOCK ? -1 : 2)));

mIndicatorWidth = ta.getDimension(R.styleable.CommonTabLayout_tl_indicator_width, dp2px(mIndicatorStyle == STYLE_TRIANGLE ? 10 : -1));

mIndicatorCornerRadius = ta.getDimensi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值