如何在Android中设置底部导航栏

时间:2021-02-25 11:43:40

标签: java android android-layout

谁能帮帮我,我的底部导航栏是这样的: enter image description here

我想让它看起来像这样:enter image description here

当然,蓝色。

我的代码如下:谢谢大家!!

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"

            />
应用程序.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;
            }
        });

2 个答案:

答案 0 :(得分:2)

在底部导航的布局中添加 touch åäö

答案 1 :(得分:2)

只需使用 background 属性来改变整个背景,如果你想自定义项目的文本和图标颜色,你可以这样做: 首先创建一个可绘制的文件名 item_background ,添加以下几行:

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

</selector>

然后在底部导航视图中添加属性 itemTextColoritemIconTint,值为 @drawable/item_background