设置 roughike 底部栏可绘制背景

Set roughike bottombar drawable background

我正在使用 roughike bottombar 在我的项目中浏览片段,我试图为我的 bottombar 设置一个可绘制的背景,但是很难做到。官方指南https://github.com/roughike/BottomBar中的示例仅展示了如何更改条形图的颜色。有什么可行的方法吗?

    resId = R.drawable.footer_bg_02;
    bottomBar = BottomBar.attach(this, savedInstanceState);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        bottomBar.setBackground(resId);
    }
    bottomBar.setFragmentItems(getSupportFragmentManager(), R.id.fragmentContainer,
            new BottomBarFragment(StoreList.newInstance("Content for fragment 1."), R.drawable.ic_01, "fragment 1"),
            new BottomBarFragment(QRscanner.newInstance("Content for fragment 2."), R.drawable.ic_02, "fragment 2"),
            new BottomBarFragment(MyBooking.newInstance("Content for fragment 3."), R.drawable.ic_03, "fragment 3"),
    );

    bottomBar.setOnItemSelectedListener(new OnTabSelectedListener() {
        @Override
        public void onItemSelected(int position) {
            switch (position) {
                case 0:
                    getSupportActionBar();
                    //return;
                case 1:
                    //getSupportActionBar().hide();
                case 2:
                    //return;
                case 3:
                    //getSupportActionBar().hide();
                case 4:
                    //return;
                    // Item 1 Selected
            }
        }
    });

我正在尝试使用 setBackground,但运气不好。

试试这个: 将此添加到父布局:

 xmlns:app="http://schemas.android.com/apk/res-auto"

然后

 <com.roughike.bottombar.BottomBar
        android:id="@+id/bottomBar"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_alignParentBottom="true"
        app:bb_activeTabAlpha="1"
        app:bb_activeTabColor="@color/color_white"
        app:bb_behavior="shifting"
        app:bb_inActiveTabAlpha="0.6"
        app:bb_inActiveTabColor="#1B5E20"
        app:bb_tabXmlResource="@xml/bottombar_tabs"
        app:bb_titleTextAppearance="@style/style_text"
        app:paddingEnd="0dp" />

在您的 res/xml 文件夹中:

bottombar_tabs.xml写:

 <?xml version="1.0" encoding="utf-8"?>
<tabs>
    <tab
        icon="@drawable/drawable1"
        id="@+id/areas_item"
        title="HOME" />
    <tab
        icon="@drawable/drawable2"
        id="@+id/nearby_item"
        title="Nearby" />
    <tab
        icon="@drawable/drawable3"
        id="@+id/my_marker_item"
        title="My marker" />

</tabs>

这是风格:

<style name="style_text">
    <item name="android:textSize">12sp</item>
    <item name="android:textColor">@color/color_white</item>
    <item name="android:textStyle">bold</item>
</style>

对于以编程方式设置的项目,请使用:

  bottomBar = BottomBar.attach(this, savedInstanceState);

    bottomBar.setFragmentItems(getSupportFragmentManager(), R.id.fragmentContainer,
            new BottomBarFragment(SampleFragment.newInstance("Content for recents."), R.drawable.home, "Recents"),
            new BottomBarFragment(SampleFragment.newInstance("Content for food."), R.drawable.food, "Food"),
            new BottomBarFragment(SampleFragment.newInstance("Content for favorites."), R.drawable.favourite, "Favorites"),
            new BottomBarFragment(SampleFragment.newInstance("Content for locations."), R.drawable.location, "Location")
    );

    // Setting colors for different tabs 
    bottomBar.mapColorForTab(0, "#3B494C");
    bottomBar.mapColorForTab(1, "#00796B");
    bottomBar.mapColorForTab(2, "#7B1FA2");
    bottomBar.mapColorForTab(3, "#FF5252");

    bottomBar.setOnItemSelectedListener(new OnTabSelectedListener() {
        @Override
        public void onItemSelected(int position) {
            switch (position) {
                case 0:
                    // Item 1 Selected
            }
        }
    });

此建议来自issue,您可以这样使用:

<com.roughike.bottombar.BottomBar 
    android:id="@+id/bottomBar" 
    android:layout_width="match_parent" 
    android:layout_height="60dp"      
    android:background="@color/colorAccent"  
    android:layout_alignParentBottom="true"  
    app:bb_tabXmlResource="@xml/bottombar_tabs" />

我想通了。

BottomBar bBar = (BottomBar) findViewById(R.id.bottomBar);
bBar.setOnTabSelectListener(this);

BottomBarTab tab1 = bBar.getTabAtPosition(0);
tab1.setBarColorWhenSelected(Color.WHITE)
BottomBarTab tab2 = bBar.getTabAtPosition(1);
tab2.setBarColorWhenSelected(Color.WHITE)

bBar.selectTabAtPosition(0)

它有点笨拙,但它确实有效。 只需设置您想要的任何颜色,它就会以编程方式更新。

这对我有用,创建一个布局并将您想要的背景设置为布局,然后将底部栏放在您创建的布局内并将背景设置为透明色:

<?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="60dp"
android:background="@drawable/gradient_color"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom|end">
<com.roughike.bottombar.BottomBar
    android:id="@+id/bottombar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:bb_activeTabColor="@color/White"
    app:bb_inActiveTabColor="@color/White"
    app:bb_tabXmlResource="@xml/bottombar_tabs"
    android:background="@color/colorTransparent"/>
</android.support.design.widget.CoordinatorLayout>