设置roughough底栏drawable背景

时间:2017-01-12 06:24:35

标签: android bottombar

我正在使用roughike底栏来浏览我项目中的片段,我正在尝试为我的底栏设置一个可绘制的背景但是很难这样做。官方指南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,但无法获得运气。

4 个答案:

答案 0 :(得分:1)

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

<?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>

答案 1 :(得分:0)

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

 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
            }
        }
    });

答案 2 :(得分:0)

此建议来自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" />

答案 3 :(得分:0)

我明白了。

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)

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