【问题标题】:Change background color Bottom Bar android更改背景颜色底部栏android
【发布时间】:2016-06-23 07:14:31
【问题描述】:

我的 Android 项目有问题。 我无法更改底栏的颜色。
这就是我希望底栏的外观:

这是我的代码

菜单 > tabhost_bottom.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"

>
<item
    android:id="@+id/home_item"
    android:icon="@drawable/ic_home"
    android:color="@color/colorPrimary"
    android:title="Home"
    />
<item
    android:id="@+id/setting_item"
    android:icon="@drawable/ic_setting"
    android:color="@color/colorPrimary"
    android:title="Setting" />

HomeActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    coordinatorLayout = (CoordinatorLayout) findViewById(R.id.tabhost_activity);

    BottomBar bottomBar = BottomBar.attach(this, savedInstanceState);
    bottomBar.setItemsFromMenu(R.menu.tabhost_bottom, new OnMenuTabSelectedListener() {
        @Override
        public void onMenuItemSelected(int itemId) {

        }
    });

    // Set the color for the active tab. Ignored on mobile when there are more than three tabs.
    bottomBar.setActiveTabColor("#55a8e5");

    // Use the dark theme. Ignored on mobile when there are more than three tabs.
    bottomBar.setBackgroundColor(Color.parseColor("#55a8e5"));
    // Use custom text appearance in tab titles.
    //bottomBar.setTextAppearance(R.style.MyTextAppearance);

    // Use custom typeface that's located at the "/src/main/assets" directory. If using with
    // custom text appearance, set the text appearance first.
    //bottomBar.setTypeFace("MyFont.ttf");
}

这是我的参考

http://androidgifts.com/build-android-material-design-bottom-navigation/

【问题讨论】:

  • 想要改变标签的颜色?

标签: android android-actionbar bottombar


【解决方案1】:

我终于用下面的代码(Xamarin C#)来改变它

var bottomBarBackground = FindViewById(Resource.Id.bb_bottom_bar_background_view);
bottomBarBackground.SetBackgroundResource(Resource.Drawable.tabbar_background);

tabbar_background.axml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/YourColor"/>
        </shape>
    </item>
</layer-list>

在这里也找到了,尽管最终解决方案对我来说不适用于 Xamarin 版本:Question about background color

【讨论】:

    【解决方案2】:

    这是library吗?

    其他选项:

    似乎您需要以不同的方式设置 Background 颜色:

        // Setting colors for different tabs when there's more than three of them.
        // You can set colors for tabs in three different ways as shown below.
        mBottomBar.mapColorForTab(0, ContextCompat.getColor(this, R.color.colorAccent));
        mBottomBar.mapColorForTab(1, 0xFF5D4037);
        mBottomBar.mapColorForTab(2, "#7B1FA2");
        mBottomBar.mapColorForTab(3, "#FF5252");
        mBottomBar.mapColorForTab(4, "#FF9800");
    

    更多信息: Android new Bottom Navigation bar

    【讨论】:

    【解决方案3】:

    对于所有 Kotlin 粉丝:要为底部导航栏中的所有项设置背景颜色,只需在类 MainActivity 函数 onCreate 中添加以下行:

    // finds view in bottom_nav_menu.xml
    val navView: BottomNavigationView = findViewById(R.id.nav_view)
    // sets background color for the whole bar 
    navView.setBackgroundColor(ContextCompat.getColor(this, R.color.yourColor))
    

    【讨论】:

      【解决方案4】:

      当你使用this 库(roughike 的底部栏)时,你可以试试这个。

      您可以使用以下行设置特定选项卡的背景颜色:

      bottomBar.getTabAtPosition(0).setBackgroundColor(backgroundColorInt);
      

      当你为每个标签多次使用这条线时,你可以改变整个标签的背景色。

      【讨论】: