diff --git a/demo/src/main/java/com/chaychan/bottombarlayout/MainActivity.java b/demo/src/main/java/com/chaychan/bottombarlayout/MainActivity.java index f416d64..105e4c6 100644 --- a/demo/src/main/java/com/chaychan/bottombarlayout/MainActivity.java +++ b/demo/src/main/java/com/chaychan/bottombarlayout/MainActivity.java @@ -113,6 +113,11 @@ public void run() { cancelTabLoading(bottomItem);//停止旋转动画 } }); + + mBottomBarLayout.setUnread(0,20);//设置第一个页签的未读数为20 + mBottomBarLayout.setUnread(1,101);//设置第二个页签的未读书 + mBottomBarLayout.showNotify(2);//设置第三个页签显示提示的小红点 + mBottomBarLayout.setMsg(3,"NEW");//设置第四个页签显示NEW提示文字 } /**停止首页页签的旋转动画*/ diff --git a/library/build.gradle b/library/build.gradle index 841548e..90c8b69 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -22,7 +22,7 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) + compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) diff --git a/library/src/main/java/com/chaychan/library/BottomBarItem.java b/library/src/main/java/com/chaychan/library/BottomBarItem.java index 5e92a1b..a11edd9 100644 --- a/library/src/main/java/com/chaychan/library/BottomBarItem.java +++ b/library/src/main/java/com/chaychan/library/BottomBarItem.java @@ -5,6 +5,7 @@ import android.graphics.drawable.Drawable; import android.support.annotation.Nullable; import android.util.AttributeSet; +import android.util.TypedValue; import android.view.Gravity; import android.view.View; import android.widget.ImageView; @@ -12,7 +13,6 @@ import android.widget.TextView; - /** * @author ChayChan * @description: 底部tab条目 @@ -35,9 +35,15 @@ public class BottomBarItem extends LinearLayout { private int mIconHeight;//图标的高度 private int mItemPadding;//BottomBarItem的padding - private TextView mTextView; + private ImageView mImageView; + private TextView mTvUnread; + private TextView mTvNotify; + private TextView mTvMsg; + private TextView mTextView; + private int mUnreadTextSize = 10; //未读数默认字体大小10sp + private int mMsgTextSize = 6; //消息默认字体大小6sp public BottomBarItem(Context context) { @@ -73,6 +79,9 @@ public BottomBarItem(Context context, @Nullable AttributeSet attrs, int defStyle mIconHeight = ta.getDimensionPixelSize(R.styleable.BottomBarItem_iconHeight, 0); mItemPadding = ta.getDimensionPixelSize(R.styleable.BottomBarItem_itemPadding, 0); + mUnreadTextSize = ta.getDimensionPixelSize(R.styleable.BottomBarItem_unreadTextSize, UIUtils.sp2px(mContext,mUnreadTextSize)); + mMsgTextSize = ta.getDimensionPixelSize(R.styleable.BottomBarItem_msgTextSize, UIUtils.sp2px(mContext,mMsgTextSize)); + ta.recycle(); checkValues(); @@ -107,9 +116,11 @@ private void init() { view.setPadding(mItemPadding,mItemPadding,mItemPadding,mItemPadding); } mImageView = (ImageView) view.findViewById(R.id.iv_icon); + mTvUnread = (TextView) view.findViewById(R.id.tv_unred_num); + mTvMsg = (TextView) view.findViewById(R.id.tv_msg); + mTvNotify = (TextView) view.findViewById(R.id.tv_point); mTextView = (TextView) view.findViewById(R.id.tv_text); - mImageView.setImageResource(mIconNormalResourceId); if (mIconWidth != 0 && mIconHeight != 0){ @@ -120,9 +131,12 @@ private void init() { mImageView.setLayoutParams(imageLayoutParams); } - mTextView.getPaint().setTextSize(mTextSize); - mTextView.setText(mText); - mTextView.setTextColor(mTextColorNormal); + mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,mTextSize);//设置底部文字字体大小 + mTvUnread.setTextSize(TypedValue.COMPLEX_UNIT_PX,mUnreadTextSize);//设置未读数的字体大小 + mTvMsg.setTextSize(TypedValue.COMPLEX_UNIT_PX,mMsgTextSize);//设置提示文字的字体大小 + + mTextView.setTextColor(mTextColorNormal);//设置底部文字字体颜色 + mTextView.setText(mText);//设置标签文字 LayoutParams textLayoutParams = (LayoutParams) mTextView.getLayoutParams(); textLayoutParams.topMargin = mMarginTop; @@ -156,4 +170,44 @@ public void setStatus(boolean isSelected){ mImageView.setImageResource(isSelected?mIconSelectedResourceId:mIconNormalResourceId); mTextView.setTextColor(isSelected?mTextColorSelected:mTextColorNormal); } + + private void setTvVisiable(TextView tv){ + //都设置为不可见 + mTvUnread.setVisibility(GONE); + mTvMsg.setVisibility(GONE); + mTvNotify.setVisibility(GONE); + + tv.setVisibility(VISIBLE);//设置为可见 + } + + /** + * 设置未读数 + * @param unreadNum 小于等于0则隐藏,大于0小于99则显示对应数字,超过99显示99+ + */ + public void setUnreadNum(int unreadNum){ + setTvVisiable(mTvUnread); + if (unreadNum <= 0){ + mTvUnread.setVisibility(GONE); + }else if (unreadNum <= 99){ + mTvUnread.setText(String.valueOf(unreadNum)); + }else{ + mTvUnread.setText("99+"); + } + } + public void setMsg(String msg){ + setTvVisiable(mTvMsg); + mTvMsg.setText(msg); + } + + public void hideMsg(){ + mTvMsg.setVisibility(GONE); + } + + public void showNotify(){ + setTvVisiable(mTvNotify); + } + + public void hideNotify(){ + mTvNotify.setVisibility(GONE); + } } diff --git a/library/src/main/java/com/chaychan/library/BottomBarLayout.java b/library/src/main/java/com/chaychan/library/BottomBarLayout.java index 337d8a6..7858c37 100644 --- a/library/src/main/java/com/chaychan/library/BottomBarLayout.java +++ b/library/src/main/java/com/chaychan/library/BottomBarLayout.java @@ -136,6 +136,48 @@ public void setCurrentItem(int currentItem) { mViewPager.setCurrentItem(mCurrentItem,mSmoothScroll); } + /** + * 设置未读数 + * @param position 底部标签的下标 + * @param unreadNum 未读数 + */ + public void setUnread(int position,int unreadNum){ + mItemViews.get(position).setUnreadNum(unreadNum); + } + + /** + * 设置提示消息 + * @param position 底部标签的下标 + * @param msg 未读数 + */ + public void setMsg(int position,String msg){ + mItemViews.get(position).setMsg(msg); + } + + /** + * 隐藏提示消息 + * @param position 底部标签的下标 + */ + public void hideMsg(int position){ + mItemViews.get(position).hideMsg(); + } + + /** + * 显示提示的小红点 + * @param position 底部标签的下标 + */ + public void showNotify(int position){ + mItemViews.get(position).showNotify(); + } + + /** + * 隐藏提示的小红点 + * @param position 底部标签的下标 + */ + public void hideNotify(int position){ + mItemViews.get(position).hideNotify(); + } + public int getCurrentItem() { return mCurrentItem; } diff --git a/library/src/main/res/drawable/shape_notify_point.xml b/library/src/main/res/drawable/shape_notify_point.xml new file mode 100644 index 0000000..9507100 --- /dev/null +++ b/library/src/main/res/drawable/shape_notify_point.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/library/src/main/res/drawable/shape_unread.xml b/library/src/main/res/drawable/shape_unread.xml new file mode 100644 index 0000000..8290ce8 --- /dev/null +++ b/library/src/main/res/drawable/shape_unread.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/library/src/main/res/layout/item_bottom_bar.xml b/library/src/main/res/layout/item_bottom_bar.xml index 78f25ba..e7ca1a5 100644 --- a/library/src/main/res/layout/item_bottom_bar.xml +++ b/library/src/main/res/layout/item_bottom_bar.xml @@ -1,18 +1,64 @@ - + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/library/src/main/res/values/colors.xml b/library/src/main/res/values/colors.xml index 73bb0c1..caa2a19 100644 --- a/library/src/main/res/values/colors.xml +++ b/library/src/main/res/values/colors.xml @@ -1,5 +1,6 @@ #ffffff + #ff0000 #DDDDDD \ No newline at end of file