BottomBar类似QQ下面的3个按钮

来源:互联网 发布:如何注册淘宝客 编辑:程序博客网 时间:2024/05/19 23:13

1.导包        compile 'com.roughike:bottom-bar:2.3.1'

我先把要用到的图片准备一下


这些图片自己下载下来          放在mipmap-xxxhdpi这个文件夹里面     不然到后面图片太大文字显示不出来




2.创建一个res/xml/bottombar_menu.xml;






3.bottombar_menu.xml里面的代码

<tabs xmlns:android="http://schemas.android.com/apk/res/android">    <!--inActiveColor:-->    <!--未被选择时,标签的颜色,作用于 icon 和 title。-->    <!--activeColor:-->    <!--被选择时,标签的颜色,作用于 icon 和 title,与上面相对应。-->    <!--barColorWhenSelected:-->    <!--当该标签被选择时,整个 BottomBar 的背景色。(就是一点,整个底部渐变的那个颜色)-->    <!--badgeBackgroundColor:-->    <!--设置 Badges 的背景色,就是右上角显示数字那个。-->    <!--badgeHidesWhenActive:-->    <!--选择此选项卡时是否应该隐藏徽章,默认为true。-->    <tab        id="@+id/tab_recents"        title="Recents"        icon="@mipmap/ic_launcher"        barColorWhenSelected="#FE3D81"        inActiveColor="#00FF00"        activeColor="#FF0000"        badgeBackgroundColor="#FF0000"        />    <tab        id="@+id/tab_favorites"        title="Favorites"        icon="@mipmap/ic_launcher"        barColorWhenSelected="#5D3C35"        />    <tab        id="@+id/tab_nearby"        title="Nearby"        icon="@mipmap/ic_launcher"        barColorWhenSelected="#7B1FA2"        />    <tab        id="@+id/tab_friends"        title="Friends"        icon="@mipmap/ic_launcher"        barColorWhenSelected="#FF5252"        />    <tab        id="@+id/tab_restaurants"        title="Restaurants"        icon="@mipmap/ic_launcher"        barColorWhenSelected="#FF9800"        /></tabs>





4.主函数

public class MainActivity extends AppCompatActivity {    private BottomBar bottomBar;    private BottomBarTab nearby;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        bottomBar=findViewById(R.id.bottomBar);        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {            @Override            public void onTabSelected(int tabId) {                if (tabId == R.id.tab_favorites) {                    // 选择指定 id 的标签                    nearby = bottomBar.getTabWithId(R.id.tab_nearby);                    //这是设置消息的条数                    nearby.setBadgeCount(8);                }               }        });        bottomBar.setOnTabReselectListener(new OnTabReselectListener() {            @Override            public void onTabReSelected(@IdRes int tabId) {                if (tabId == R.id.tab_favorites) {                    // 已经选择了这个标签,又点击一次。即重选。                    nearby.removeBadge();                }            }        });        bottomBar.setTabSelectionInterceptor(new TabSelectionInterceptor() {            @Override            public boolean shouldInterceptTabSelection(@IdRes int oldTabId, @IdRes int newTabId) {                // 点击无效                if (newTabId == R.id.tab_restaurants ) {                    // ......                    return true;                }                return false;            }        });            }}





5.主布局

<?xml version="1.0" encoding="utf-8"?><!--<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/contentContainer"-->        <!--android:layout_width="match_parent"-->        <!--android:layout_height="match_parent"-->        <!-->-->        <!--<TextView-->            <!--android:id="@+id/but1"-->            <!--android:text="aaaa"-->            <!--android:layout_width="match_parent"-->            <!--android:layout_height="wrap_content" />-->    <!--</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_menu"-->        <!--app:bb_behavior="shy"/>--><!--</android.support.design.widget.CoordinatorLayout>--><!--上面是正常的安卓手机的布局--><!--下面是平板模式的布局--><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">    <!-- 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" />    <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_menu"        app:bb_tabletMode="true" />    <!--<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_titleTextAppearance="@style/MyTextAppearance"-->        <!--app:bb_titleTypeFace="fonts/MySuperDuperFont.ttf"-->        <!--app:bb_showShadow="true" />-->    bb_tabXmlResource:    设置标签的 xml 资源标识,在 res/xml/ 目录下。    bb_tabletMode:    是否是平板模式。true:是;false:不是。    bb_behavior:(三种模式)    shifting: 选定的标签比其他的更宽。    shy: 将 Bottombar 放在 Coordinatorlayout 它会自动隐藏在滚动!(需要特定的布局)    underNavbar: 正常模式(默认)。    bb_inActiveTabAlpha:    没选中时标签的透明度,icon 和 titiles 有用。(取值:0-1)    bb_activeTabAlpha:    选中时标签的透明度,与上一个相对应。(取值:0-1)    bb_inActiveTabColor:    没选时标签的颜色,icon 和 titiles 有用。    bb_activeTabColor:    选中时标签的颜色,与一个相对应。    bb_badgeBackgroundColor:    设置 Badges 的背景色,就是右上角显示数字那个。    bb_badgesHideWhenActive:    小红点是否隐藏,默认为 true 隐藏。就是右上角显示数字那个。    bb_titleTextAppearance:    利用 style 重新设置自定的格式,例如:大小、加粗等。    bb_titleTypeFace:    设置自定的字体, 例: app:bb_titleTypeFace="fonts/MySuperDuperFont.ttf"。    将字体放在 src/main/assets/fonts/MySuperDuperFont.ttf 路径下,    只需要写 fonts/MySuperDuperFont.ttf 即可,将自动填充。    bb_showShadow:    控制阴影是否显示或隐藏,类似于蒙板,默认为true。</RelativeLayout>








原创粉丝点击