简体   繁体   中英

How to set Bottom Navigation Bar in Android

Can anyone help me, my bottom navigation bar looks like this: 在此处输入图像描述

I want to make it to look like this: 在此处输入图像描述

Offcourse, with blue color.

My code is below: Thanks everyone!!

layout.xml
 <android.support.design.widget.BottomNavigationView

            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            app:menu="@menu/main_manu"
            app:itemBackground="@color/blue"
            app:itemIconTint="@android:color/white"
            app:itemTextColor="@android:color/white"
            app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"

            />
Application.java
 final BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation); Menu menu = bottomNavigationView.getMenu(); MenuItem menuItem = menu.getItem(2); menuItem.setChecked(true); bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.action_pocetna: new ListaVoznji.Home().execute(); break; case R.id.action_unos: new ListaVoznji.Login().execute(); break; case R.id.action_pregled: Intent intent2 = new Intent(ListaVoznji.this,ListaSvihVoznji.class); intent2.putExtra("voznja",voznja); startActivity(intent2); break; case R.id.action_shutdown: Intent homescreen=new Intent(ListaVoznji.this,LoginActivity.class); log = 1; homescreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(homescreen); finish(); break; } return true; } });

Add app:labelVisibilityMode="labeled" in your layout in botton navigation

Just use the background attribute to change the whole background, and if you want to customize the item's text and icon color, you can do this by: First create a drawable file name item_background, add to it the following lines:

<selector>  
<item android:state_checked="true" android:color="@color/colorPrimary" />
 
 <item android:state_checked="false" android:color="@color/colorAccent"/>  

</selector>

Then in your bottom navigation view add attributes itemTextColor & itemIconTint with the value @drawable/item_background

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM