From 7216c88069a86191abe7e79c9562240b1f5d2a22 Mon Sep 17 00:00:00 2001 From: liyujiang Date: Mon, 11 Mar 2019 13:56:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0TextBold=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8DViewPager=E7=9A=84Adapter=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E7=9A=84NPE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bottombarlayout/DynamicAddItemActivity.java | 1 + .../main/res/layout/activity_fragment_manager.xml | 4 ++++ .../java/com/chaychan/library/BottomBarItem.java | 15 ++++++++++++++- .../com/chaychan/library/BottomBarLayout.java | 4 +++- library/src/main/res/values/attr.xml | 4 +++- 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/demo/src/main/java/com/chaychan/bottombarlayout/DynamicAddItemActivity.java b/demo/src/main/java/com/chaychan/bottombarlayout/DynamicAddItemActivity.java index 16c40af..d291cd9 100644 --- a/demo/src/main/java/com/chaychan/bottombarlayout/DynamicAddItemActivity.java +++ b/demo/src/main/java/com/chaychan/bottombarlayout/DynamicAddItemActivity.java @@ -83,6 +83,7 @@ private TabFragment createFragment(int titleId) { private BottomBarItem createBottomBarItem(int i) { BottomBarItem item = new BottomBarItem.Builder(this) + .titleTextBold(true) .titleTextSize(8) .titleNormalColor(R.color.tab_normal_color) .titleSelectedColor(R.color.tab_selected_color) diff --git a/demo/src/main/res/layout/activity_fragment_manager.xml b/demo/src/main/res/layout/activity_fragment_manager.xml index da6dcb2..691e2e2 100644 --- a/demo/src/main/res/layout/activity_fragment_manager.xml +++ b/demo/src/main/res/layout/activity_fragment_manager.xml @@ -31,6 +31,7 @@ app:iconNormal="@mipmap/tab_home_normal" app:iconSelected="@mipmap/tab_home_selected" app:itemText="@string/tab_home" + app:itemTextBold="true" app:textColorNormal="@color/tab_normal_color" app:textColorSelected="@color/tab_selected_color" app:itemTextSize="8sp" @@ -46,6 +47,7 @@ app:iconNormal="@mipmap/tab_video_normal" app:iconSelected="@mipmap/tab_video_selected" app:itemText="@string/tab_video" + app:itemTextBold="true" app:textColorNormal="@color/tab_normal_color" app:textColorSelected="@color/tab_selected_color" app:itemTextSize="8sp" @@ -63,6 +65,7 @@ app:iconNormal="@mipmap/tab_micro_normal" app:iconSelected="@mipmap/tab_micro_selected" app:itemText="@string/tab_micro" + app:itemTextBold="true" app:textColorNormal="@color/tab_normal_color" app:textColorSelected="@color/tab_selected_color" app:itemTextSize="8sp" @@ -78,6 +81,7 @@ app:iconNormal="@mipmap/tab_me_normal" app:iconSelected="@mipmap/tab_me_selected" app:itemText="@string/tab_me" + app:itemTextBold="true" app:textColorNormal="@color/tab_normal_color" app:textColorSelected="@color/tab_selected_color" app:itemTextSize="8sp" diff --git a/library/src/main/java/com/chaychan/library/BottomBarItem.java b/library/src/main/java/com/chaychan/library/BottomBarItem.java index 5120066..d96ed89 100644 --- a/library/src/main/java/com/chaychan/library/BottomBarItem.java +++ b/library/src/main/java/com/chaychan/library/BottomBarItem.java @@ -29,6 +29,7 @@ public class BottomBarItem extends LinearLayout { private Drawable normalIcon;//普通状态图标的资源id private Drawable selectedIcon;//选中状态图标的资源id private String title;//文本 + private boolean titleTextBold = false;//文字加粗 private int titleTextSize = 12;//文字大小 默认为12sp private int titleNormalColor; //描述文本的默认显示颜色 private int titleSelectedColor; //述文本的默认选中显示颜色 @@ -83,6 +84,7 @@ private void initAttrs(TypedArray ta) { selectedIcon = ta.getDrawable(R.styleable.BottomBarItem_iconSelected); title = ta.getString(R.styleable.BottomBarItem_itemText); + titleTextBold = ta.getBoolean(R.styleable.BottomBarItem_itemTextBold, titleTextBold); titleTextSize = ta.getDimensionPixelSize(R.styleable.BottomBarItem_itemTextSize, UIUtils.sp2px(context, titleTextSize)); titleNormalColor = ta.getColor(R.styleable.BottomBarItem_textColorNormal, UIUtils.getColor(context,R.color.bbl_999999)); @@ -157,7 +159,7 @@ private void init() { } mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextSize);//设置底部文字字体大小 - + mTextView.getPaint().setFakeBoldText(titleTextBold); mTvUnread.setTextSize(TypedValue.COMPLEX_UNIT_PX, unreadTextSize);//设置未读数的字体大小 mTvUnread.setTextColor(unreadTextColor);//设置未读数字体颜色 mTvUnread.setBackground(unreadTextBg);//设置未读数背景 @@ -284,6 +286,7 @@ public BottomBarItem create(Builder builder) { this.normalIcon = builder.normalIcon; this.selectedIcon = builder.selectedIcon; this.title = builder.title; + this.titleTextBold = builder.titleTextBold; this.titleTextSize = builder.titleTextSize; this.titleNormalColor = builder.titleNormalColor; this.titleSelectedColor = builder.titleSelectedColor; @@ -312,6 +315,7 @@ public static final class Builder { private Drawable normalIcon;//普通状态图标的资源id private Drawable selectedIcon;//选中状态图标的资源id private String title;//标题 + private boolean titleTextBold;//文字加粗 private int titleTextSize;//字体大小 private int titleNormalColor; //描述文本的默认显示颜色 private int titleSelectedColor; //述文本的默认选中显示颜色 @@ -332,6 +336,7 @@ public static final class Builder { public Builder(Context context) { this.context = context; + titleTextBold = false; titleTextSize = UIUtils.sp2px(context,12); titleNormalColor = getColor(R.color.bbl_999999); titleSelectedColor = getColor(R.color.bbl_ff0000); @@ -374,6 +379,14 @@ public Builder title(String title) { return this; } + /** + * Sets the title's text bold + */ + public Builder titleTextBold(boolean titleTextBold) { + this.titleTextBold = titleTextBold; + return this; + } + /** * Sets the title's text size */ diff --git a/library/src/main/java/com/chaychan/library/BottomBarLayout.java b/library/src/main/java/com/chaychan/library/BottomBarLayout.java index 2c7037d..184ee2f 100644 --- a/library/src/main/java/com/chaychan/library/BottomBarLayout.java +++ b/library/src/main/java/com/chaychan/library/BottomBarLayout.java @@ -4,6 +4,7 @@ import android.content.res.TypedArray; import android.os.Bundle; import android.os.Parcelable; +import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.View; @@ -67,7 +68,8 @@ private void init() { } if (mViewPager != null) { - if (mViewPager.getAdapter().getCount() != mChildCount) { + PagerAdapter adapter = mViewPager.getAdapter(); + if (adapter != null && adapter.getCount() != mChildCount) { throw new IllegalArgumentException("LinearLayout的子View数量必须和ViewPager条目数量一致"); } } diff --git a/library/src/main/res/values/attr.xml b/library/src/main/res/values/attr.xml index dc09691..b2b4662 100644 --- a/library/src/main/res/values/attr.xml +++ b/library/src/main/res/values/attr.xml @@ -10,6 +10,8 @@ + + @@ -56,4 +58,4 @@ - \ No newline at end of file +