Skip to content

Commit

Permalink
Merge pull request #34 from liyujiang-gzu/master
Browse files Browse the repository at this point in the history
增加TextBold属性,避免ViewPager的Adapter可能的NPE
  • Loading branch information
chaychan authored Nov 21, 2020
2 parents 883ed4d + 7216c88 commit cd35792
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions demo/src/main/res/layout/activity_fragment_manager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down
15 changes: 14 additions & 1 deletion library/src/main/java/com/chaychan/library/BottomBarItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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; //述文本的默认选中显示颜色
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);//设置未读数背景
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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; //述文本的默认选中显示颜色
Expand All @@ -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);
Expand Down Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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条目数量一致");
}
}
Expand Down
4 changes: 3 additions & 1 deletion library/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<attr name="itemText" format="string"/>
<!--文字大小-->
<attr name="itemTextSize" format="dimension"/>
<!--文字加粗-->
<attr name="itemTextBold" format="boolean"/>
<!--默认状态下的文字颜色-->
<attr name="textColorNormal" format="color"/>
<!--选中状态下的文字颜色-->
Expand Down Expand Up @@ -56,4 +58,4 @@
<attr name="smoothScroll" format="boolean"/>
</declare-styleable>

</resources>
</resources>

0 comments on commit cd35792

Please sign in to comment.