refactoring the code for tab icon

This commit is contained in:
cgspine
2019-02-15 14:35:30 +08:00
parent a57edc98e4
commit e2cbef937e
5 changed files with 329 additions and 178 deletions
@@ -0,0 +1,123 @@
/*
* Tencent is pleased to support the open source community by making QMUI_Android available.
*
* Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the MIT License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.qmuiteam.qmui.widget;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import com.qmuiteam.qmui.util.QMUIColorHelper;
import com.qmuiteam.qmui.util.QMUIDrawableHelper;
public class QMUITabIcon extends Drawable {
private final @NonNull Drawable mNormalIconDrawable;
private final @Nullable Drawable mSelectedIconDrawable;
private final boolean mDynamicChangeIconColor;
private final int mNormalIconColor;
private final int mSelectedIconColor;
public QMUITabIcon(@NonNull Drawable normalIconDrawable, @NonNull Drawable selectedIconDrawable) {
mNormalIconDrawable = normalIconDrawable;
mSelectedIconDrawable = selectedIconDrawable;
mDynamicChangeIconColor = false;
mNormalIconColor = 0;
mSelectedIconColor = 0;
mNormalIconDrawable.setAlpha(255);
mSelectedIconDrawable.setAlpha(0);
int nw = mNormalIconDrawable.getIntrinsicWidth();
int nh = mNormalIconDrawable.getIntrinsicHeight();
mNormalIconDrawable.setBounds(0, 0, nw, nh);
mSelectedIconDrawable.setBounds(0, 0, nw, nh);
}
public QMUITabIcon(@NonNull Drawable normalIconDrawable, int normalIconColor, int selectedIconColor) {
mNormalIconDrawable = normalIconDrawable;
mSelectedIconDrawable = null;
mDynamicChangeIconColor = true;
mNormalIconColor = normalIconColor;
mSelectedIconColor = selectedIconColor;
mNormalIconDrawable.setAlpha(255);
QMUIDrawableHelper.setDrawableTintColor(mNormalIconDrawable, normalIconColor);
}
@Override
public int getIntrinsicWidth() {
return mNormalIconDrawable.getIntrinsicWidth();
}
@Override
public int getIntrinsicHeight() {
return mNormalIconDrawable.getIntrinsicHeight();
}
@Override
public void setAlpha(int alpha) {
// not used
}
@Override
public void setColorFilter(@Nullable ColorFilter colorFilter) {
// not used
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
/**
* set the select percent for QMUITabIcon, value must be in [0, 1]
*
* @param percent muse be in [0, 1]
*/
public void setCurrentSelectPercent(float percent) {
percent = Math.min(1f, Math.max(0f, percent));
if (mDynamicChangeIconColor) {
int targetColor = QMUIColorHelper.computeColor(mNormalIconColor, mSelectedIconColor, percent);
QMUIDrawableHelper.setDrawableTintColor(mNormalIconDrawable, targetColor);
} else {
int normalAlpha = (int) (255 * (1 - percent));
mNormalIconDrawable.setAlpha(normalAlpha);
if (mSelectedIconDrawable != null) {
mSelectedIconDrawable.setAlpha(255 - normalAlpha);
}
}
invalidateSelf();
}
@Override
public void draw(@NonNull Canvas canvas) {
mNormalIconDrawable.draw(canvas);
if (!mDynamicChangeIconColor && mSelectedIconDrawable != null) {
mSelectedIconDrawable.draw(canvas);
}
}
@Override
protected void onBoundsChange(Rect bounds) {
mNormalIconDrawable.setBounds(bounds);
if (mSelectedIconDrawable != null) {
mSelectedIconDrawable.setBounds(bounds);
}
}
}
@@ -52,7 +52,6 @@ import com.qmuiteam.qmui.QMUIInterpolatorStaticHolder;
import com.qmuiteam.qmui.R;
import com.qmuiteam.qmui.util.QMUIColorHelper;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.util.QMUIDrawableHelper;
import com.qmuiteam.qmui.util.QMUILangHelper;
import com.qmuiteam.qmui.util.QMUIResHelper;
@@ -210,7 +209,7 @@ public class QMUITabSegment extends HorizontalScrollView {
int index = (int) v.getTag();
Tab model = getAdapter().getItem(index);
if (model != null) {
selectTab(index, !mHasIndicator && !model.isDynamicChangeIconColor(), true);
selectTab(index, false, true);
}
if (mOnTabClickListener != null) {
mOnTabClickListener.onTabClick(index);
@@ -343,7 +342,7 @@ public class QMUITabSegment extends HorizontalScrollView {
public void reset() {
mContentLayout.getTabAdapter().clear();
mCurrentSelectedIndex = NO_POSITION;
if(mSelectAnimator != null){
if (mSelectAnimator != null) {
mSelectAnimator.cancel();
mSelectAnimator = null;
}
@@ -383,7 +382,7 @@ public class QMUITabSegment extends HorizontalScrollView {
* 设置 indicator的宽度是否随内容宽度变化
*/
public void setIndicatorWidthAdjustContent(boolean indicatorWidthFollowContent) {
if(mIsIndicatorWidthFollowContent != indicatorWidthFollowContent){
if (mIsIndicatorWidthFollowContent != indicatorWidthFollowContent) {
mIsIndicatorWidthFollowContent = indicatorWidthFollowContent;
mContentLayout.requestLayout();
}
@@ -395,7 +394,7 @@ public class QMUITabSegment extends HorizontalScrollView {
* @param isIndicatorTop true 时表示 indicator 位置在 Tab 的上方, false 时表示在下方
*/
public void setIndicatorPosition(boolean isIndicatorTop) {
if(mIndicatorTop != isIndicatorTop){
if (mIndicatorTop != isIndicatorTop) {
mIndicatorTop = isIndicatorTop;
mContentLayout.invalidate();
}
@@ -646,9 +645,11 @@ public class QMUITabSegment extends HorizontalScrollView {
public void onAnimationUpdate(ValueAnimator animation) {
float animValue = (float) animation.getAnimatedValue();
int preColor = QMUIColorHelper.computeColor(getTabSelectedColor(prevModel), getTabNormalColor(prevModel), animValue);
prevView.mTextView.setTextColor(preColor);
prevModel.setCurrentSelectPercent(1 - animValue);
int nowColor = QMUIColorHelper.computeColor(getTabNormalColor(nowModel), getTabSelectedColor(nowModel), animValue);
prevView.setColorInTransition(prevModel, preColor);
nowView.setColorInTransition(nowModel, nowColor);
nowView.mTextView.setTextColor(nowColor);
nowModel.setCurrentSelectPercent(animValue);
layoutIndicatorInTransition(prevModel, nowModel, animValue);
}
});
@@ -694,7 +695,7 @@ public class QMUITabSegment extends HorizontalScrollView {
}
private void layoutIndicator(Tab model, boolean invalidate) {
if(model == null){
if (model == null) {
return;
}
if (mIndicatorRect == null) {
@@ -710,7 +711,7 @@ public class QMUITabSegment extends HorizontalScrollView {
mIndicatorPaint.setStyle(Paint.Style.FILL);
}
mIndicatorPaint.setColor(getTabSelectedColor(model));
if(invalidate){
if (invalidate) {
mContentLayout.invalidate();
}
}
@@ -770,8 +771,10 @@ public class QMUITabSegment extends HorizontalScrollView {
TabItemView targetView = listViews.get(targetIndex);
int preColor = QMUIColorHelper.computeColor(getTabSelectedColor(preModel), getTabNormalColor(preModel), offsetPercent);
int targetColor = QMUIColorHelper.computeColor(getTabNormalColor(targetModel), getTabSelectedColor(targetModel), offsetPercent);
preView.setColorInTransition(preModel, preColor);
targetView.setColorInTransition(targetModel, targetColor);
preView.mTextView.setTextColor(preColor);
preModel.setCurrentSelectPercent(1 - offsetPercent);
targetView.mTextView.setTextColor(targetColor);
targetModel.setCurrentSelectPercent(offsetPercent);
layoutIndicatorInTransition(preModel, targetModel, offsetPercent);
}
@@ -1007,7 +1010,8 @@ public class QMUITabSegment extends HorizontalScrollView {
boolean isSelectedTabBold();
@Nullable Typeface getTypeface();
@Nullable
Typeface getTypeface();
}
public static class TabLayoutOnPageChangeListener implements ViewPager.OnPageChangeListener {
@@ -1075,60 +1079,59 @@ public class QMUITabSegment extends HorizontalScrollView {
}
}
public static class Tab {
public static final int USE_TAB_SEGMENT = Integer.MIN_VALUE;
private int textSize = USE_TAB_SEGMENT;
private int normalColor = USE_TAB_SEGMENT;
private int selectedColor = USE_TAB_SEGMENT;
private Drawable normalIcon = null;
private Drawable selectedIcon = null;
private int contentWidth = 0;
private int contentLeft = 0;
private int iconPosition = USE_TAB_SEGMENT;
public static class TabBuilder {
private Drawable normalDrawable;
private Drawable selectedDrawable;
private boolean dynamicChangeIconColor = true;
private int textSize = Tab.USE_TAB_SEGMENT;
private int normalColor = Tab.USE_TAB_SEGMENT;
private int selectedColor = Tab.USE_TAB_SEGMENT;
private int iconPosition = Tab.USE_TAB_SEGMENT;
private int gravity = Gravity.CENTER;
private CharSequence text;
private List<View> mCustomViews;
private int mSignCountDigits = 2;
private TextView mSignCountTextView;
private int mSignCountMarginLeft = 0;
private int mSignCountMarginTop = 0;
/**
* 是否动态更改icon颜色,如果为true, selectedIcon将失效
*/
private boolean dynamicChangeIconColor = true;
private int signCountDigits = 2;
public Tab(CharSequence text) {
public TabBuilder(CharSequence text) {
this.text = text;
}
public Tab(Drawable normalIcon, Drawable selectedIcon, CharSequence text, boolean dynamicChangeIconColor) {
this(normalIcon, selectedIcon, text, dynamicChangeIconColor, true);
public TabBuilder setNormalDrawable(Drawable normalDrawable) {
this.normalDrawable = normalDrawable;
return this;
}
/**
* 如果你的 icon 显示大小和实际大小不吻合:
* 1. 设置icon 的 bounds
* 2. 使用此构造器
* 3. 最后一个参数(setIntrinsicSize)设置为false
*
* @param normalIcon 未选中态 icon
* @param selectedIcon 选中态 icon
* @param text 文字
* @param dynamicChangeIconColor 是否动态改变 icon 颜色
* @param setIntrinsicSize 是否设置 icon 的大小为 intrinsic width 和 intrinsic height。
*/
public Tab(Drawable normalIcon, Drawable selectedIcon, CharSequence text, boolean dynamicChangeIconColor, boolean setIntrinsicSize) {
this.normalIcon = normalIcon;
if (this.normalIcon != null && setIntrinsicSize) {
this.normalIcon.setBounds(0, 0, normalIcon.getIntrinsicWidth(), normalIcon.getIntrinsicHeight());
public TabBuilder setSelectedDrawable(Drawable selectedDrawable) {
this.selectedDrawable = selectedDrawable;
if(selectedDrawable != null){
this.dynamicChangeIconColor = false;
}
this.selectedIcon = selectedIcon;
if (this.selectedIcon != null && setIntrinsicSize) {
this.selectedIcon.setBounds(0, 0, selectedIcon.getIntrinsicWidth(), selectedIcon.getIntrinsicHeight());
}
this.text = text;
return this;
}
public TabBuilder setTextSize(int textSize) {
this.textSize = textSize;
return this;
}
public TabBuilder setColor(int normalColor, int selectedColor) {
this.normalColor = normalColor;
this.selectedColor = selectedColor;
return this;
}
public TabBuilder setDynamicChangeIconColor(boolean dynamicChangeIconColor) {
this.dynamicChangeIconColor = dynamicChangeIconColor;
return this;
}
public TabBuilder setGravity(int gravity) {
this.gravity = gravity;
return this;
}
public TabBuilder setIconPosition(int iconPosition) {
this.iconPosition = iconPosition;
return this;
}
/**
@@ -1136,8 +1139,55 @@ public class QMUITabSegment extends HorizontalScrollView {
*
* @param digit 数字显示的最大位数
*/
public void setmSignCountDigits(int digit) {
mSignCountDigits = digit;
public TabBuilder setSignCountDigits(int digit) {
this.signCountDigits = digit;
return this;
}
public Tab build() {
Tab tab = new Tab(this.text);
if (normalDrawable != null) {
if (dynamicChangeIconColor || selectedDrawable == null) {
tab.tabIcon = new QMUITabIcon(normalDrawable, normalColor, selectedColor);
} else {
tab.tabIcon = new QMUITabIcon(normalDrawable, selectedDrawable);
}
tab.tabIcon.setBounds(0, 0, tab.tabIcon.getIntrinsicWidth(), tab.tabIcon.getIntrinsicHeight());
}
tab.gravity = this.gravity;
tab.iconPosition = this.iconPosition;
tab.textSize = this.textSize;
tab.normalColor = this.normalColor;
tab.selectedColor = this.selectedColor;
tab.signCountDigits = this.signCountDigits;
return tab;
}
}
public static class Tab {
public static final int USE_TAB_SEGMENT = Integer.MIN_VALUE;
private int textSize = USE_TAB_SEGMENT;
private int normalColor = USE_TAB_SEGMENT;
private int selectedColor = USE_TAB_SEGMENT;
private QMUITabIcon tabIcon = null;
private int contentWidth = 0;
private int contentLeft = 0;
private int iconPosition = USE_TAB_SEGMENT;
private int gravity = Gravity.CENTER;
private CharSequence text;
private int signCountDigits = 2;
private TextView mSignCountTextView;
private List<View> mCustomViews;
private int mSignCountMarginLeft = 0;
private int mSignCountMarginTop = 0;
public Tab(CharSequence text) {
this.text = text;
}
public void setSignCountDigits(int digit) {
this.signCountDigits = digit;
}
public void setTextColor(@ColorInt int normalColor, @ColorInt int selectedColor) {
@@ -1185,6 +1235,25 @@ public class QMUITabSegment extends HorizontalScrollView {
this.iconPosition = iconPosition;
}
public void setCurrentSelectPercent(float percent) {
if (tabIcon != null) {
tabIcon.setCurrentSelectPercent(percent);
}
}
public void setTabIconBounds(Rect bounds) {
if (tabIcon != null) {
tabIcon.setBounds(bounds);
}
}
public void setTabIconBounds(int left, int top, int right, int bottom) {
if (tabIcon != null) {
tabIcon.setBounds(left, top, right, bottom);
}
}
public int getGravity() {
return gravity;
}
@@ -1197,22 +1266,14 @@ public class QMUITabSegment extends HorizontalScrollView {
return normalColor;
}
public Drawable getNormalIcon() {
return normalIcon;
public QMUITabIcon getTabIcon() {
return tabIcon;
}
public int getSelectedColor() {
return selectedColor;
}
public Drawable getSelectedIcon() {
return selectedIcon;
}
public boolean isDynamicChangeIconColor() {
return dynamicChangeIconColor;
}
public void addCustomView(@NonNull View view) {
if (mCustomViews == null) {
mCustomViews = new ArrayList<>();
@@ -1310,9 +1371,9 @@ public class QMUITabSegment extends HorizontalScrollView {
}
private String getNumberDigitsFormattingValue(int number) {
if (QMUILangHelper.getNumberDigits(number) > mSignCountDigits) {
if (QMUILangHelper.getNumberDigits(number) > signCountDigits) {
StringBuilder result = new StringBuilder();
for (int digit = 1; digit <= mSignCountDigits; digit++) {
for (int digit = 1; digit <= signCountDigits; digit++) {
result.append("9");
}
result.append("+");
@@ -1429,33 +1490,13 @@ public class QMUITabSegment extends HorizontalScrollView {
return mGestureDetector.onTouchEvent(event) || super.onTouchEvent(event);
}
public void setColorInTransition(Tab tab, int color) {
mTextView.setTextColor(color);
if (tab.isDynamicChangeIconColor()) {
Drawable icon = mTextView.getCompoundDrawables()[getTabIconPosition(tab)];
if (icon != null) {
QMUIDrawableHelper.setDrawableTintColor(icon, color);
setDrawable(mTextView, icon, getTabIconPosition(tab));
}
}
}
public void updateDecoration(Tab tab, boolean isSelected) {
int color = isSelected ? getTabSelectedColor(tab) : getTabNormalColor(tab);
mTextView.setTextColor(color);
tab.setCurrentSelectPercent(isSelected ? 1f : 0f);
Drawable icon = tab.getNormalIcon();
if (isSelected) {
if (!tab.isDynamicChangeIconColor()) {
icon = tab.getSelectedIcon() != null ? tab.getSelectedIcon() : icon;
} else if (icon != null) {
icon = icon.mutate();
QMUIDrawableHelper.setDrawableTintColor(icon, color);
}
}
Drawable icon = tab.getTabIcon();
if (icon == null) {
mTextView.setCompoundDrawablePadding(0);
mTextView.setCompoundDrawables(null, null, null, null);
@@ -1611,8 +1652,8 @@ public class QMUITabSegment extends HorizontalScrollView {
usedLeft = usedLeft + childMeasureWidth + (mMode == MODE_SCROLLABLE ? mItemSpaceInScrollMode : 0);
}
if(mCurrentSelectedIndex != NO_POSITION && mSelectAnimator == null
&& mViewPagerScrollState == ViewPager.SCROLL_STATE_IDLE){
if (mCurrentSelectedIndex != NO_POSITION && mSelectAnimator == null
&& mViewPagerScrollState == ViewPager.SCROLL_STATE_IDLE) {
layoutIndicator(mTabAdapter.getItem(mCurrentSelectedIndex), false);
}
}
@@ -168,16 +168,14 @@ public class QDTabSegmentFixModeFragment extends BaseFragment {
case 4:
mTabSegment.reset();
mTabSegment.setHasIndicator(false);
QMUITabSegment.Tab component = new QMUITabSegment.Tab(
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component),
null,
"Components", true
);
QMUITabSegment.Tab util = new QMUITabSegment.Tab(
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util),
null,
"Helper", true
);
QMUITabSegment.Tab component = new QMUITabSegment.TabBuilder("Components")
.setNormalDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component))
.setDynamicChangeIconColor(true)
.build();
QMUITabSegment.Tab util = new QMUITabSegment.TabBuilder("Helper")
.setNormalDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util))
.setDynamicChangeIconColor(true)
.build();
mTabSegment.addTab(component);
mTabSegment.addTab(util);
break;
@@ -189,16 +187,16 @@ public class QDTabSegmentFixModeFragment extends BaseFragment {
case 6:
mTabSegment.reset();
mTabSegment.setHasIndicator(false);
QMUITabSegment.Tab component2 = new QMUITabSegment.Tab(
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component),
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component_selected),
"Components", false
);
QMUITabSegment.Tab util2 = new QMUITabSegment.Tab(
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util),
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util_selected),
"Helper", false
);
QMUITabSegment.Tab component2 = new QMUITabSegment.TabBuilder("Components")
.setNormalDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component))
.setSelectedDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component_selected))
.build();
QMUITabSegment.Tab util2 = new QMUITabSegment.TabBuilder("Helper")
.setNormalDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util))
.setSelectedDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util_selected))
.build();
mTabSegment.addTab(component2);
mTabSegment.addTab(util2);
break;
@@ -207,20 +205,18 @@ public class QDTabSegmentFixModeFragment extends BaseFragment {
mTabSegment.setHasIndicator(true);
mTabSegment.setIndicatorWidthAdjustContent(true);
mTabSegment.setIndicatorPosition(false);
QMUITabSegment.Tab component3 = new QMUITabSegment.Tab(
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component),
null,
"Components", true
);
component3.setTextColor(QMUIResHelper.getAttrColor(getContext(), R.attr.qmui_config_color_blue),
QMUIResHelper.getAttrColor(getContext(), R.attr.qmui_config_color_red));
QMUITabSegment.Tab util3 = new QMUITabSegment.Tab(
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util),
null,
"Helper", true
);
util3.setTextColor(QMUIResHelper.getAttrColor(getContext(), R.attr.qmui_config_color_gray_1),
QMUIResHelper.getAttrColor(getContext(), R.attr.qmui_config_color_red));
QMUITabSegment.Tab component3 = new QMUITabSegment.TabBuilder("Components")
.setNormalDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component))
.setDynamicChangeIconColor(true)
.setColor(QMUIResHelper.getAttrColor(getContext(), R.attr.qmui_config_color_blue),
QMUIResHelper.getAttrColor(getContext(), R.attr.qmui_config_color_red))
.build();
QMUITabSegment.Tab util3 = new QMUITabSegment.TabBuilder("Helper")
.setNormalDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util))
.setDynamicChangeIconColor(true)
.setColor(QMUIResHelper.getAttrColor(getContext(), R.attr.qmui_config_color_gray_1),
QMUIResHelper.getAttrColor(getContext(), R.attr.qmui_config_color_red))
.build();
mTabSegment.addTab(component3);
mTabSegment.addTab(util3);
break;
@@ -228,11 +224,10 @@ public class QDTabSegmentFixModeFragment extends BaseFragment {
mTabSegment.updateTabText(0, "动态更新文案");
break;
case 9:
QMUITabSegment.Tab component4 = new QMUITabSegment.Tab(
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component),
null,
"动态更新", true
);
QMUITabSegment.Tab component4 = new QMUITabSegment.TabBuilder("动态更新")
.setNormalDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component))
.setDynamicChangeIconColor(true)
.build();
mTabSegment.replaceTab(0, component4);
break;
@@ -104,39 +104,30 @@ public class HomeFragment extends BaseFragment {
mTabSegment.setDefaultSelectedColor(selectColor);
// mTabSegment.setDefaultTabIconPosition(QMUITabSegment.ICON_POSITION_BOTTOM);
// // 如果你的 icon 显示大小和实际大小不吻合:
// // 1. 设置icon 的 bounds
// // 2. Tab 使用拥有5个参数的构造器
// // 3. 最后一个参数(setIntrinsicSize)设置为false
QMUITabSegment.Tab component = new QMUITabSegment.TabBuilder("Components")
.setNormalDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component))
.setSelectedDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component_selected))
.setDynamicChangeIconColor(false)
.build();
// 如果你的 icon 显示大小和实际大小不吻合:
// 通过 tab.setTabIconBounds 重新设置 bounds
// int iconShowSize = QMUIDisplayHelper.dp2px(getContext(), 20);
// Drawable normalDrawable = ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component);
// normalDrawable.setBounds(0, 0, iconShowSize, iconShowSize);
// Drawable selectDrawable = ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component_selected);
//
// selectDrawable.setBounds(0, 0, iconShowSize, iconShowSize);
//
// QMUITabSegment.Tab component = new QMUITabSegment.Tab(
// normalDrawable,
// normalDrawable,
// "Components", false, false
// );
// component.setTabIconBounds(0, 0, iconShowSize, iconShowSize);
QMUITabSegment.Tab component = new QMUITabSegment.Tab(
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component),
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component_selected),
"Components", false
);
QMUITabSegment.Tab util = new QMUITabSegment.TabBuilder("Helper")
.setNormalDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util))
.setSelectedDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util_selected))
.setDynamicChangeIconColor(false)
.build();
QMUITabSegment.Tab lab = new QMUITabSegment.TabBuilder("Lab")
.setNormalDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_lab))
.setSelectedDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_lab_selected))
.setDynamicChangeIconColor(false)
.build();
QMUITabSegment.Tab util = new QMUITabSegment.Tab(
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util),
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util_selected),
"Helper", false
);
QMUITabSegment.Tab lab = new QMUITabSegment.Tab(
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_lab),
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_lab_selected),
"Lab", false
);
mTabSegment.addTab(component)
.addTab(util)
.addTab(lab);
@@ -107,22 +107,23 @@ public class QDNotchHelperFragment extends BaseFragment {
}
private void initTabs() {
QMUITabSegment.Tab component = new QMUITabSegment.Tab(
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component),
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component_selected),
"Components", false
);
QMUITabSegment.Tab component = new QMUITabSegment.TabBuilder("Components")
.setNormalDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component))
.setSelectedDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_component_selected))
.setDynamicChangeIconColor(false)
.build();
QMUITabSegment.Tab util = new QMUITabSegment.Tab(
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util),
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util_selected),
"Helper", false
);
QMUITabSegment.Tab lab = new QMUITabSegment.Tab(
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_lab),
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_lab_selected),
"Lab", false
);
QMUITabSegment.Tab util = new QMUITabSegment.TabBuilder("Helper")
.setNormalDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util))
.setSelectedDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util_selected))
.setDynamicChangeIconColor(false)
.build();
QMUITabSegment.Tab lab = new QMUITabSegment.TabBuilder("Lab")
.setNormalDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_lab))
.setSelectedDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_lab_selected))
.setDynamicChangeIconColor(false)
.build();
mTabSegment.addTab(component)
.addTab(util)
.addTab(lab);