Skip to content

Commit

Permalink
Add setIconMarginTop. Remove setIconCenter.
Browse files Browse the repository at this point in the history
  • Loading branch information
ittianyu committed May 20, 2017
1 parent 3cf18a9 commit 5a3eb27
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 24 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ An android lib for enhancing BottomNavigationView.
|getMenuItemPosition|Get menu item position in menu. Return position if success, -1 otherwise.
|getOnNavigationItemSelectedListener|Get OnNavigationItemSelectedListener.
|setCurrentItem|Set the current checked item.
|setIconMarginTop|set margin top for icon.
|setIconSize|Set all item ImageView size.
|setIconSizeAt|Set all item ImageView size which at position.
|setIconsMarginTop|set margin top for all icons.
|setIconTintList| Set the icon tint list.
|setIconVisibility|Change the visibility of icon.
|setItemBackground| Set background of item.
Expand Down Expand Up @@ -73,6 +75,7 @@ An android lib for enhancing BottomNavigationView.

![](/read_me_images/no_animation_shifting_mode_item_shifting_mode_icon.gif)

**Attention: Something wrong on Android 4.x**
![](/read_me_images/with_padding.jpg)

![](/read_me_images/center_icon_only.jpg)
Expand All @@ -85,6 +88,9 @@ An android lib for enhancing BottomNavigationView.

![](/read_me_images/icon_selector_1.jpg) ![](/read_me_images/icon_selector_2.jpg)

![](/read_me_images/icon_margin_top.jpg)


**With ViewPager**

![](/read_me_images/with_view_pager.gif)
Expand Down Expand Up @@ -114,12 +120,12 @@ allprojects {

Step 2. Add the dependency
```groovy
compile 'com.github.ittianyu:BottomNavigationViewEx:1.1.8'
compile 'com.github.ittianyu:BottomNavigationViewEx:1.1.9'
```

#### Manual: ####

Downloading [BottomNavigationViewEx.java](https://raw.githubusercontent.com/ittianyu/BottomNavigationViewEx/master/widget/src/main/java/com/ittianyu/bottomnavigationviewex/BottomNavigationViewEx.java) and copying it to you project
Downloading [BottomNavigationViewEx.java](https://raw.githubusercontent.com/ittianyu/BottomNavigationViewEx/master/widget/src/main/java/com/ittianyu/bottomnavigationviewex/BottomNavigationViewEx.java) and copying it to you project.


## Getting started ##
Expand Down Expand Up @@ -254,8 +260,10 @@ Thanks for [Adrián Mouly](https://github.com/amouly) | [liaolintao](https://git
|getMenuItemPosition|获取子菜单的索引。如果找不到,返回 -1。
|getOnNavigationItemSelectedListener|获取 OnNavigationItemSelectedListener。
|setCurrentItem|设置当前选中项。
|setIconMarginTop|设置 icon 的 MarginTop,用于调节图标垂直位置。
|setIconSize|设置所有的子项图标大小。
|setIconSizeAt|设置位于 position 的图标的大小。
|setIconsMarginTop|设置所有 icon 的 MarginTop,用于调节图标垂直位置。
|setIconTintList| 设置图片的渲染颜色列表(Selector)
|setIconVisibility|设置图片可见性。
|setItemBackground| 设置子项的背景。
Expand Down Expand Up @@ -300,6 +308,7 @@ Thanks for [Adrián Mouly](https://github.com/amouly) | [liaolintao](https://git

![](/read_me_images/no_animation_shifting_mode_item_shifting_mode_icon.gif)

**注意:这个 style 在安卓 4.x 上有 bug**
![](/read_me_images/with_padding.jpg)

![](/read_me_images/center_icon_only.jpg)
Expand All @@ -312,6 +321,8 @@ Thanks for [Adrián Mouly](https://github.com/amouly) | [liaolintao](https://git

![](/read_me_images/icon_selector_1.jpg) ![](/read_me_images/icon_selector_2.jpg)

![](/read_me_images/icon_margin_top.jpg)

**和 ViewPager 一起使用**

![](/read_me_images/with_view_pager.gif)
Expand Down Expand Up @@ -341,7 +352,7 @@ allprojects {

步骤 2. 添加依赖
```groovy
compile 'com.github.ittianyu:BottomNavigationViewEx:1.1.8'
compile 'com.github.ittianyu:BottomNavigationViewEx:1.1.9'
```

#### 手动导入: ####
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.ittianyu.bottomnavigationviewexsample"
minSdkVersion 9
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 12
versionName "1.1.8"
versionCode 13
versionName "1.1.9"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ private void init() {
initCustomTypeface();

bind.bnveIconSelector.enableAnimation(false);

initMargin();
}


private void initCenterIconOnly() {
bind.bnveCenterIconOnly.enableAnimation(false);
bind.bnveCenterIconOnly.enableShiftingMode(false);
Expand All @@ -94,7 +97,7 @@ private void initCenterIconOnly() {
bind.bnveCenterIconOnly.setItemBackground(centerPosition, R.color.colorGreen);
bind.bnveCenterIconOnly.setIconTintList(centerPosition,
getResources().getColorStateList(R.color.selector_item_gray_color));
bind.bnveCenterIconOnly.setIconCenter(centerPosition);
bind.bnveCenterIconOnly.setIconMarginTop(centerPosition, BottomNavigationViewEx.dp2px(this, 4));
// you could set a listener for bnve. and return false when click the center item so that it won't be checked.
bind.bnveCenterIconOnly.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
Expand Down Expand Up @@ -140,4 +143,13 @@ private void initCustomTypeface() {
// bind.bnveCustomTypeface.setTypeface(typeface);
}

private void initMargin() {
bind.bnveIconMarginTop.enableAnimation(false);
bind.bnveIconMarginTop.enableShiftingMode(false);
bind.bnveIconMarginTop.enableItemShiftingMode(false);
bind.bnveIconMarginTop.setTextVisibility(false);
bind.bnveIconMarginTop.setItemHeight(BottomNavigationViewEx.dp2px(this, 56));
bind.bnveIconMarginTop.setIconsMarginTop(BottomNavigationViewEx.dp2px(this, 16));
}

}
11 changes: 11 additions & 0 deletions app/src/main/res/layout/activity_style.xml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@
app:itemTextColor="@color/selector_item_primary_color"
app:menu="@menu/menu_navigation_selector" />

<TextView
style="@style/text_black"
android:text="@string/icon_margin_top" />

<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
android:id="@+id/bnve_icon_margin_top"
style="@style/bnve_white_bg"
app:itemIconTint="@color/selector_item_primary_color"
app:itemTextColor="@color/selector_item_primary_color"
app:menu="@menu/menu_navigation" />

</LinearLayout>

</ScrollView>
Expand Down
12 changes: 9 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
<resources>
<string name="app_name">BottomNavigationViewEx</string>

<string name="music">Music</string>
<string name="backup">Backup</string>
<string name="friends">Friends</string>
<string name="favor">Favor</string>

<string name="lamp">Lamp</string>
<string name="weather">Weather</string>
<string name="face">Face</string>

<string name="visibility">Visibility</string>
<string name="different_style">Different style</string>
<string name="with_view_pager">With ViewPager</string>

<string name="no_animation_shifting_mode_item_shifting_mode_icon" translatable="false">no animation/ shifting mode/ item shifting mode/ icon</string>
<string name="no_item_shifting_mode_icon" translatable="false">no item shifting mode/ icon</string>
<string name="no_shifting_mode_item_shifting_mode_icon" translatable="false">no shifting mode/ item shifting mode/ icon</string>
Expand All @@ -29,7 +36,6 @@
<string name="bigger_icon" translatable="false">bigger icon (custom icon size)</string>
<string name="custom_typeface" translatable="false">custom typeface</string>
<string name="icon_selector" translatable="false">icon selector</string>
<string name="lamp">Lamp</string>
<string name="weather">Weather</string>
<string name="face">Face</string>
<string name="icon_margin_top" translatable="false">icon margin top</string>

</resources>
Binary file added read_me_images/icon_margin_top.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions widget/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
defaultConfig {
minSdkVersion 9
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 12
versionName "1.1.8"
versionCode 13
versionName "1.1.9"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
import android.util.AttributeSet;
import android.util.SparseIntArray;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

Expand Down Expand Up @@ -876,7 +874,7 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
}

}

public void enableShiftingMode(int position, boolean enable) {
getBottomNavigationItemView(position).setShiftingMode(enable);
}
Expand All @@ -890,18 +888,28 @@ public void setIconTintList(int position, ColorStateList tint) {
}

/**
* <p>
* It will go back when it was checked {@code android.support.design.internal.BottomNavigationItemView#setChecked()}
* So you need call {@link #enableShiftingMode(int, boolean)} to make it center again when item selected
* </p>
*
* set margin top for all icons
* @param marginTop in px
*/
public void setIconsMarginTop(int marginTop) {
for (int i = 0; i < getItemCount(); i++) {
setIconMarginTop(i, marginTop);
}
}

/**
* set margin top for icon
* @param position
* @param marginTop in px
*/
public void setIconCenter(int position) {
enableShiftingMode(position, true);
ImageView iv = getIconAt(position);
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) iv.getLayoutParams();
lp.gravity = Gravity.CENTER;
public void setIconMarginTop(int position, int marginTop) {
/*
1. BottomNavigationItemView
2. private final int mDefaultMargin;
*/
BottomNavigationItemView itemView = getBottomNavigationItemView(position);
setField(BottomNavigationItemView.class, itemView, "mDefaultMargin", marginTop);
mMenuView.updateMenuView();
}

}

0 comments on commit 5a3eb27

Please sign in to comment.