简体   繁体   English

创建底部导航栏

[英]Create Bottom Navigation Bar

我试图制作一个底部导航栏,以根据按钮更改活动,但是每当尝试教程时,都会出现一个错误或另一个错误

I think if You want something as a nav bar You should try getting to GitHub because there are many great things there like: https://github.com/Nightonke/BoomMenu 我认为,如果您想将其作为导航栏,则应尝试进入GitHub,因为那里有很多很棒的东西,例如: https : //github.com/Nightonke/BoomMenu

Try it, looks way better than a simple nav bar. 试试看,看起来比简单的导航栏更好。 cheers 干杯

you can use this library for bottomnavifationbar 您可以将此库用于bottomnavifationbar

github lib , github lib 2 , githublib 3 , github lib 4 , github libgithub lib 2githublib 3github lib 4

or try this 或尝试这个

complie this depedencie 弥补这种缺陷

compile ‘com.android.support:design:25.3.1’

create a layout like this 创建这样的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent">

<!-- Content Container -->

<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:itemBackground="@color/colorPrimary"
    app:itemIconTint="@color/white"
    app:itemTextColor="@color/white"
    app:menu="@menu/bottom_navigation_main" />

</RelativeLayout>

create menu as per your reqirement 根据需要创建菜单

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_favorites"
    android:enabled="true"
    android:icon="@drawable/ic_favorite_white_24dp"
    android:title="@string/text_favorites"
    app:showAsAction="ifRoom" />
<item
    android:id="@+id/action_schedules"
    android:enabled="true"
    android:icon="@drawable/ic_access_time_white_24dp"
    android:title="@string/text_schedules"
    app:showAsAction="ifRoom" />
<item
    android:id="@+id/action_music"
    android:enabled="true"
    android:icon="@drawable/ic_audiotrack_white_24dp"
    android:title="@string/text_music"
    app:showAsAction="ifRoom" />

**Handling Enabled / Disabled states. Make selector file.**

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/white" android:state_enabled="true" />
    <item android:color="@color/colorPrimaryDark" android:state_enabled="false" />
</selector>

handle click events 处理点击事件

BottomNavigationView bottomNavigationView = (BottomNavigationView)
            findViewById(R.id.bottom_navigation);

bottomNavigationView.setOnNavigationItemSelectedListener(
    new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_favorites:

                    break;
                case R.id.action_schedules:

                    break;
                case R.id.action_music:

                    break;
            }
            return false;
        }
 });

该官员不再受支持,因此您可以尝试radiobuttonviewpager

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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