Skip to content

Commit

Permalink
feature: with a new control parameter, different icon bounds can work…
Browse files Browse the repository at this point in the history
… in QMUITabSegment #117
  • Loading branch information
cgspine committed Dec 20, 2017
1 parent 9465894 commit f729e39
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
22 changes: 19 additions & 3 deletions qmui/src/main/java/com/qmuiteam/qmui/widget/QMUITabSegment.java
Original file line number Diff line number Diff line change
Expand Up @@ -1016,13 +1016,29 @@ public Tab(CharSequence text) {
this.text = text;
}

public Tab(Drawable normalIcon, Drawable selectedIcon, CharSequence text, boolean dynamicChangeIconColor) {

public Tab(Drawable normalIcon, Drawable selectedIcon, CharSequence text, boolean dynamicChangeIconColor){
this(normalIcon, selectedIcon, text, dynamicChangeIconColor, true);
}
/**
* 如果你的 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) {
if (this.normalIcon != null && setIntrinsicSize) {
this.normalIcon.setBounds(0, 0, normalIcon.getIntrinsicWidth(), normalIcon.getIntrinsicHeight());
}
this.selectedIcon = selectedIcon;
if (this.selectedIcon != null) {
if (this.selectedIcon != null && setIntrinsicSize) {
this.selectedIcon.setBounds(0, 0, selectedIcon.getIntrinsicWidth(), selectedIcon.getIntrinsicHeight());
}
this.text = text;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.qmuiteam.qmuidemo.fragment.home;

import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
Expand All @@ -8,6 +9,7 @@
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.util.QMUIResHelper;
import com.qmuiteam.qmui.widget.QMUITabSegment;
import com.qmuiteam.qmuidemo.base.BaseFragment;
Expand Down Expand Up @@ -87,11 +89,30 @@ private void initTabs() {
mTabSegment.setDefaultNormalColor(normalColor);
mTabSegment.setDefaultSelectedColor(selectColor);
// mTabSegment.setDefaultTabIconPosition(QMUITabSegment.ICON_POSITION_BOTTOM);

// // 如果你的 icon 显示大小和实际大小不吻合:
// // 1. 设置icon 的 bounds
// // 2. Tab 使用拥有5个参数的构造器
// // 3. 最后一个参数(setIntrinsicSize)设置为false
// 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
// );

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.Tab(
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util),
ContextCompat.getDrawable(getContext(), R.mipmap.icon_tabbar_util_selected),
Expand Down

0 comments on commit f729e39

Please sign in to comment.