我没有时间再维护这个了。我基本上是匆忙编写了整个库,没有进行测试,而当时我是一个认真的专家初学者。因此,存在许多不可预测的移动部件,并且测试可能也不是那么好。真的不知道,因为我已经很久没有碰过这个了。
我建议您使用 Google 的官方 BottomNavigationView 并敦促他们实现您需要的功能。或者使用另一个第三方库。
之前的最新版本可以在 v1 分支中找到
如何贡献
变更日志
模仿新的 Material Design 底部导航模式的自定义视图组件。
没有。 minSDK 版本为API 级别 11 (Honeycomb)。
compile ' com.roughike:bottom-bar:2.3.1 '
行家:
< dependency >
< groupId >com.roughike</ groupId >
< artifactId >bottom-bar</ artifactId >
< version >2.3.1</ version >
< type >pom</ type >
</ dependency >
您可以通过编写 XML 资源文件来添加项目。
图标必须是完全不透明、纯黑色、24dp 并且没有填充。例如,使用 Android Asset Studio 通用图标生成器,选择“TRIM”并确保填充为 0dp。您的图标应如下所示:
在 XML 资源文件中定义选项卡。
res/xml/bottombar_tabs.xml:
< tabs >
< tab
id = " @+id/tab_favorites "
icon = " @drawable/ic_favorites "
title = " Favorites " />
< tab
id = " @+id/tab_nearby "
icon = " @drawable/ic_nearby "
title = " Nearby " />
< tab
id = " @+id/tab_friends "
icon = " @drawable/ic_friends "
title = " Friends " />
</ tabs >
然后,将 BottomBar 添加到您的布局中,并为其指定选项卡 xml 文件的资源 ID。
布局/activity_main.xml
< RelativeLayout xmlns : android = " http://schemas.android.com/apk/res/android "
android : layout_width = " match_parent "
android : layout_height = " match_parent "
xmlns : app = " http://schemas.android.com/apk/res-auto " >
<!-- This could be your fragment container, or something -->
< FrameLayout
android : id = " @+id/contentContainer "
android : layout_width = " match_parent "
android : layout_height = " match_parent "
android : layout_above = " @+id/bottomBar " />
< com .roughike.bottombar.BottomBar
android : id = " @+id/bottomBar "
android : layout_width = " match_parent "
android : layout_height = " 60dp "
android : layout_alignParentBottom = " true "
app : bb_tabXmlResource = " @xml/bottombar_tabs " />
</ RelativeLayout >
默认情况下,选项卡不会执行任何操作,除非您侦听选择事件并在选择选项卡时执行某些操作。
MainActivity.java:
public class MainActivity extends Activity {
@ Override
protected void onCreate ( @ Nullable Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState );
setContentView ( R . layout . activity_main );
BottomBar bottomBar = ( BottomBar ) findViewById ( R . id . bottomBar );
bottomBar . setOnTabSelectListener ( new OnTabSelectListener () {
@ Override
public void onTabSelected ( @ IdRes int tabId ) {
if ( tabId == R . id . tab_favorites ) {
// The tab with id R.id.tab_favorites was selected,
// change your content accordingly.
}
}
});
}
}
如果您想监听重选事件,请按以下步骤操作:
bottomBar . setOnTabReselectListener ( new OnTabReselectListener () {
@ Override
public void onTabReSelected ( @ IdRes int tabId ) {
if ( tabId == R . id . tab_favorites ) {
// The tab with id R.id.tab_favorites was reselected,
// change your content accordingly.
}
}
});
如果您想有条件地取消对任何选项卡的选择,您绝对可以。只需将TabSelectionInterceptor
分配给 BottomBar,并从shouldInterceptTabSelection()
方法返回 true 即可。
bottomBar . setTabSelectionInterceptor ( new TabSelectionInterceptor () {
@ Override
public boolean shouldInterceptTabSelection ( @ IdRes int oldTabId , @ IdRes int newTabId ) {
if ( newTabId == R . id . tab_pro_feature && ! userHasProVersion ()) {
startProVersionPurchaseFlow ();
return true ;
}
return false ;
}
});
如果您希望在选择特定选项卡时具有不同的图标,只需使用状态列表可绘制对象即可。
res/drawable/my_tab_icon.xml
< selector xmlns : android = " http://schemas.android.com/apk/res/android " >
< item android : drawable = " @drawable/ic_myicon_selected " android : state_selected = " true " />
< item android : drawable = " @drawable/ic_myicon_default " android : state_selected = " false " />
</ selector >
res/xml/bottombar_tabs.xml
...
< tab
id = " @+id/tab_favorites "
icon = " @drawable/my_tab_icon "
title = " Favorites " />
<!-- You can use @color resources too! -->
...
只需将barColorWhenSelected
添加到每个选项卡即可。当选择该选项卡时,整个 BottomBar 背景颜色会以漂亮的动画发生变化。
res/xml/bottombar_tabs.xml
< tabs >
< tab
id = " @+id/tab_favorites "
icon = " @drawable/ic_favorites "
title = " Favorites "
barColorWhenSelected = " #5D4037 " />
<!-- You can use @color resources too! -->
</ tabs >
首先,定义一个作为主应用程序主题的子主题的样式:
res/values-v21/styles.xml
< style name = " AppTheme.TransNav " parent = " AppTheme " >
< item name = " android:navigationBarColor " >@android:color/transparent</ item >
< item name = " android:windowTranslucentNavigation " >true</ item >
< item name = " android:windowDrawsSystemBarBackgrounds " >true</ item >
</ style >
您还必须制作同一主题的存根版本,以避免在 Lollipop 之前的 API 级别中发生崩溃:
资源/值/styles.xml
< style name = " AppTheme.TransNav " parent = " AppTheme " />
还要在您的values-land-v21.xml
中包含相同的存根,以避免透明的导航栏和景观上的奇怪行为。
res/values-land-v21.xml:
< style name = " AppTheme.TransNav " parent = " AppTheme " />
将AndroidManifest.xml
中的主题应用于您的 Activity。
AndroidManifest.xml:
< activity android : name = " .MyAwesomeActivity " android : theme = " @style/AppTheme.TransNav " />
最后,设置bb_behavior
以包含underNavbar
标志,然后就可以开始了!
Activity_my_awesome.xml:
< com .roughike.bottombar.BottomBar
android : id = " @+id/bottomBar "
android : layout_width = " match_parent "
android : layout_height = " 56dp "
android : layout_alignParentBottom = " true "
app : bb_tabXmlResource = " @xml/my_awesome_bottombar_tabs "
app : bb_behavior = " underNavbar " />
在res/layout-sw600dp
文件夹中为您的 Activity 指定不同的布局,并将bb_tabletMode
设置为 true。
res/layout-sw600dp/activity_main.xml:
< RelativeLayout
xmlns : android = " http://schemas.android.com/apk/res/android "
xmlns : app = " http://schemas.android.com/apk/res-auto "
android : layout_width = " match_parent "
android : layout_height = " match_parent " >
< com .roughike.bottombar.BottomBar
android : id = " @+id/bottomBar "
android : layout_width = " wrap_content "
android : layout_height = " match_parent "
android : layout_alignParentLeft = " true "
app : bb_tabXmlResource = " @xml/bottombar_tabs_three "
app : bb_tabletMode = " true " />
<!-- This could be your fragment container, or something -->
< FrameLayout
android : id = " @+id/contentContainer "
android : layout_width = " match_parent "
android : layout_height = " match_parent "
android : layout_toRightOf = " @+id/bottomBar " />
</ RelativeLayout >
简单易行!
活动_main.xml:
< android .support.design.widget.CoordinatorLayout xmlns : android = " http://schemas.android.com/apk/res/android "
xmlns : app = " http://schemas.android.com/apk/res-auto "
android : layout_width = " match_parent "
android : layout_height = " match_parent " >
< android .support.v4.widget.NestedScrollView
android : id = " @+id/myScrollingContent "
android : layout_width = " match_parent "
android : layout_height = " match_parent " >
<!-- Your loooooong scrolling content here. -->
</ android .support.v4.widget.NestedScrollView>
< com .roughike.bottombar.BottomBar
android : id = " @+id/bottomBar "
android : layout_width = " match_parent "
android : layout_height = " 60dp "
android : layout_gravity = " bottom "
app : bb_tabXmlResource = " @xml/bottombar_tabs_three "
app : bb_behavior = " shy " />
</ android .support.design.widget.CoordinatorLayout>
您可以轻松添加徽章来显示未读消息计数或新项目/任何您喜欢的内容。
BottomBarTab nearby = bottomBar . getTabWithId ( R . id . tab_nearby );
nearby . setBadgeCount ( 5 );
// Remove the badge when you're done with it.
nearby . removeBadge /();
< com .roughike.bottombar.BottomBar
android : id = " @+id/bottomBar "
android : layout_width = " match_parent "
android : layout_height = " 60dp "
android : layout_alignParentBottom = " true "
app : bb_tabXmlResource = " @xml/bottombar_tabs_three "
app : bb_tabletMode = " true "
app : bb_behavior = " shifting|shy|underNavbar "
app : bb_inActiveTabAlpha = " 0.6 "
app : bb_activeTabAlpha = " 1 "
app : bb_inActiveTabColor = " #222222 "
app : bb_activeTabColor = " @color/colorPrimary "
app : bb_badgesHideWhenActive = " true "
app : bb_titleTextAppearance = " @style/MyTextAppearance "
app : bb_titleTypeFace = " fonts/MySuperDuperFont.ttf "
app : bb_showShadow = " true " />
values/xml/
中shifting
:所选选项卡比其余选项卡更宽。 shy
:将 BottomBar 放入 CoordinatorLayout 中,它会在滚动时自动隐藏! underNavbar
:在导航栏下方绘制BottomBar!fonts/MySuperDuperFont.ttf
。在这种情况下,您的字体路径将类似于src/main/assets/fonts/MySuperDuperFont.ttf
,但您只需要提供fonts/MySuperDuperFont.ttf
,因为资源文件夹将自动为您填充。< tab
id = " @+id/tab_recents "
title = " Recents "
icon = " @drawable/empty_icon "
inActiveColor = " #00FF00 "
activeColor = " #FF0000 "
barColorWhenSelected = " #FF0000 "
badgeBackgroundColor = " #FF0000 "
badgeHidesWhenActive = " true " />
向我发送带有修改后的 README.md 的拉取请求以获得好评!
请随意创建问题和拉取请求。
创建拉取请求时,越多越好:我希望看到按功能分隔的十个小拉取请求,而不是将所有这些合并为一个巨大的拉取请求。
BottomBar library for Android
Copyright (c) 2016 Iiro Krankka (http://github.com/roughike).
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
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.