From 5a3eb27c335e3b1d353be5125d854acacf3e9b9e Mon Sep 17 00:00:00 2001 From: ittianyu <86839868@qq.com> Date: Sat, 20 May 2017 12:37:48 +0800 Subject: [PATCH] Add setIconMarginTop. Remove setIconCenter. --- README.md | 17 +++++++-- app/build.gradle | 4 +-- .../features/style/StyleActivity.java | 14 +++++++- app/src/main/res/layout/activity_style.xml | 11 ++++++ app/src/main/res/values/strings.xml | 12 +++++-- read_me_images/icon_margin_top.jpg | Bin 0 -> 9441 bytes widget/build.gradle | 4 +-- .../BottomNavigationViewEx.java | 34 +++++++++++------- 8 files changed, 72 insertions(+), 24 deletions(-) create mode 100644 read_me_images/icon_margin_top.jpg diff --git a/README.md b/README.md index 82b251e..75c6009 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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) @@ -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) @@ -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 ## @@ -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| 设置子项的背景。 @@ -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) @@ -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) @@ -341,7 +352,7 @@ allprojects { 步骤 2. 添加依赖 ```groovy -compile 'com.github.ittianyu:BottomNavigationViewEx:1.1.8' +compile 'com.github.ittianyu:BottomNavigationViewEx:1.1.9' ``` #### 手动导入: #### diff --git a/app/build.gradle b/app/build.gradle index 927b012..7c94f76 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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 { diff --git a/app/src/main/java/com/ittianyu/bottomnavigationviewexsample/features/style/StyleActivity.java b/app/src/main/java/com/ittianyu/bottomnavigationviewexsample/features/style/StyleActivity.java index 27d83cc..33b7591 100644 --- a/app/src/main/java/com/ittianyu/bottomnavigationviewexsample/features/style/StyleActivity.java +++ b/app/src/main/java/com/ittianyu/bottomnavigationviewexsample/features/style/StyleActivity.java @@ -81,8 +81,11 @@ private void init() { initCustomTypeface(); bind.bnveIconSelector.enableAnimation(false); + + initMargin(); } + private void initCenterIconOnly() { bind.bnveCenterIconOnly.enableAnimation(false); bind.bnveCenterIconOnly.enableShiftingMode(false); @@ -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 @@ -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)); + } + } diff --git a/app/src/main/res/layout/activity_style.xml b/app/src/main/res/layout/activity_style.xml index 1d3cd5a..cbd9deb 100644 --- a/app/src/main/res/layout/activity_style.xml +++ b/app/src/main/res/layout/activity_style.xml @@ -236,6 +236,17 @@ app:itemTextColor="@color/selector_item_primary_color" app:menu="@menu/menu_navigation_selector" /> + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c3fcd5e..475ed03 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,12 +1,19 @@ BottomNavigationViewEx + Music Backup Friends Favor + + Lamp + Weather + Face + Visibility Different style With ViewPager + no animation/ shifting mode/ item shifting mode/ icon no item shifting mode/ icon no shifting mode/ item shifting mode/ icon @@ -29,7 +36,6 @@ bigger icon (custom icon size) custom typeface icon selector - Lamp - Weather - Face + icon margin top + diff --git a/read_me_images/icon_margin_top.jpg b/read_me_images/icon_margin_top.jpg new file mode 100644 index 0000000000000000000000000000000000000000..460ffa21d3e04f75e9ccc77e827792a1ae34d3e0 GIT binary patch literal 9441 zcmd5>2UJsAx;{zhAciJ*5e=b(lq7UTT96i`BV9@Ygc4c+K`A04AR;9Q0Ysz|5T!Q} zR1gV8DIy)D3J560hQPx+dhe|FX3flN^WI)7Ywi92`|scW_c=NH!~1Um9<7Ub7XZ-J z6$coAKga%ifDIks>=6ur0Gb0b76A6&LU`PWL?2~oX@b9$Bi7pqC*|zzC4JG+M;aj| zBMqEZz3AiU?13Y~oN%sqf(m@SsRa(hV^!ei=k_3N&UmDOje`i0u4-xN8 zfc?mDwuHSe26HRb`y74!6`6Y0a`*QQ{n&WmjWXKXIlv1?AnKx3;0F{bEFPZtKxt@c$!aR0Jt2kjs$1iFIoJbvKoI^R$0>z=ScMSGxPTL{8c?8H*cc1 zznixYOj8qf9#6n}2l@ZV>rXdve)vEf_N* z@>=RL@(3*@t+OauS$TOy_)i(^Uj_RQWuy;`OaG|iUsd%N=>RG}rhj1cVDSgaafAbC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AB~exa`IAK10(ewoNcE#dfL=1an=g{i zEIlQgzcZna1%%lLR&MSMsFFU~hDo&7WqRFQ6mkm1fAs4qIr2@|gKspT-zfwmfB4Ew zr$NHy+FSEeC-JK1Qu~QJB7IxoTw<}rXoghc}2oz1xh91jwa3e<1oe=j$G!gTunIR<$=jk&2zU3CAnJuMuZ&>d7Sck&+d!0Bj zkqh*Pr~+5i0yVJ(Jj^dWOE`=M(Fu1fO-~K=^v+;&9e9~A*_0qdc?|dzPS-rRGLx@A z5r!K6*_lsE+2pL!W;ALk9LmbiW#ui?D2{tqW^7=VifI*6c85L%gLxR#Jf3e^X%huf zmzhGj?)0r$j3U%nE4RMIF-Gw=9K-l`wLJxrXTLQ?Db8MrXRCQ!SG96eS&(urF~5(E zf1xUXK^QOL=}bSSIAdf*z1ES5rgxj+S>vQplXpF9-0yxwtv5RA}XNF!+m)! zhioccBE2$X)rEU9^Nrv-uvn06GUi@A4L=ib!i>J+;ECP*?#l~P>+`ZS1um)})NYMM zIH$q1kj}w}&78GpD3{(BtgC24a!I;H(9_njm1`DVnYlf`C3_t}b~Vu8AzinyznEr; ze@PeJ{Gnvnhpt?W-9%BkIfL)sn0mS=JZzYrGnFL-BT% zm2(2`qA?ode|U-K@Zr>@ySq*BeSj*o4}7|K7@vRp+!OoYs6xw5uOw;ZzN;40_nDU; z&4@wA1h%3T)%<*Z;s&Oo-EGx9s^}==KCvQ#XOgm3*11t=%S-&PbjJ1@fuW=N8e@ty?<9JKsDAgrL?&n)j3caB$4 z=Uw>(mP(O;m!RVWDXtJUbgw!T3T;($P>c&9C-x^#rE|!D5@ZuOcqp13qhj y)}kQ)56GbR_a2`ZJ@aKB=vWUAUyg{}2Nc5B!=LX1UphXS?*pgy0Wf5L - * 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 - *

- * + * 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(); } }