Android新的底部导航栏或BottomNavigationView

zjy*_*ill 130 android bottomnavigationview

看到新的指南出来了,并在google photos最新的应用程序中使用.不知道如何使用新的底部导航栏.通过新的支持lib查看,没有发现任何领先优势.

在此输入图像描述

找不到任何官方样品.

如何使用新的底栏?不想做任何自定义.

Jay*_* RJ 170

我想你可能会寻找这个.

这是一个快速启动代码片段:

public class MainActivity extends AppCompatActivity {
    private BottomBar mBottomBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Notice how you don't use the setContentView method here! Just
        // pass your layout to bottom bar, it will be taken care of.
        // Everything will be just like you're used to.
        mBottomBar = BottomBar.bind(this, R.layout.activity_main,
                savedInstanceState);

        mBottomBar.setItems(
                new BottomBarTab(R.drawable.ic_recents, "Recents"),
                new BottomBarTab(R.drawable.ic_favorites, "Favorites"),
                new BottomBarTab(R.drawable.ic_nearby, "Nearby"),
                new BottomBarTab(R.drawable.ic_friends, "Friends")
        );

        mBottomBar.setOnItemSelectedListener(new OnTabSelectedListener() {
            @Override
            public void onItemSelected(final int position) {
                // the user selected a new tab
            }
        });
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mBottomBar.onSaveInstanceState(outState);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是参考链接.

https://github.com/roughike/BottomBar

编辑新版本.

底部导航视图已经在材料设计指南中使用了一段时间,但我们并不容易将其实现到我们的应用程序中.一些应用程序已经构建了自己的解决方案,而其他应用程序则依赖第三方开源库来完成工作.现在设计支持库正在看到这个底部导航栏的添加,让我们来看看我们如何使用它!

如何使用 ?

首先,我们需要更新我们的依赖!

compile ‘com.android.support:design:25.0.0’
Run Code Online (Sandbox Code Playgroud)

设计xml.

<?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>
Run Code Online (Sandbox Code Playgroud)

根据您的要求创建菜单.

<?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" />
</menu>
Run Code Online (Sandbox Code Playgroud)

处理启用/禁用状态.制作选择器文件.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item
      android:state_checked="true"
      android:color="@color/colorPrimary" />
  <item
      android:state_checked="false"
      android:color="@color/grey" />
 </selector>
Run Code Online (Sandbox Code Playgroud)

处理点击事件.

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;
            }
});
Run Code Online (Sandbox Code Playgroud)

如果您想了解更多关于它的方法及其工作原理,请阅读此内容.

当然它会帮助你.


Mak*_*aev 48

您应该使用v25 Android支持库中的BottomNavigationView.它代表了应用程序的标准底部导航栏.

这是一篇关于Medium的帖子,它有一个分步指南: https ://medium.com/@hitherejoe/exploring-the-android-design-support-library-bottom-navigation-drawer-548de699e8e0#.9vmiekxze

  • @DroidDev,因为BottomNavigationView是在发布此答案的那天发布的.在此之前,只有第三方解决方案可用. (5认同)
  • http://stackoverflow.com/questions/40153888/bottomnavigationview-not-showing-in-my-activity hello maxim ..我实现了这个,但它没有出现.. (2认同)

wal*_*r86 16

您还可以使用选项卡布局和自定义选项卡视图来实现此目的.

custom_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?attr/selectableItemBackground"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="10dp"
    android:paddingTop="8dp">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:scaleType="centerInside"
        android:src="@drawable/ic_recents_selector" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:textAllCaps="false"
        android:textColor="@color/tab_color"
        android:textSize="12sp"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v4.view.ViewPager

        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        style="@style/AppTabLayout"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="?attr/colorPrimary" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private TabLayout mTabLayout;

    private int[] mTabsIcons = {
            R.drawable.ic_recents_selector,
            R.drawable.ic_favorite_selector,
            R.drawable.ic_place_selector};


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

        // Setup the viewPager
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        MyPagerAdapter pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);

        mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
        mTabLayout.setupWithViewPager(viewPager);

        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            tab.setCustomView(pagerAdapter.getTabView(i));
        }

        mTabLayout.getTabAt(0).getCustomView().setSelected(true);
    }


    private class MyPagerAdapter extends FragmentPagerAdapter {

        public final int PAGE_COUNT = 3;

        private final String[] mTabsTitle = {"Recents", "Favorites", "Nearby"};

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        public View getTabView(int position) {
            // Given you have a custom layout in `res/layout/custom_tab.xml` with a TextView and ImageView
            View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_tab, null);
            TextView title = (TextView) view.findViewById(R.id.title);
            title.setText(mTabsTitle[position]);
            ImageView icon = (ImageView) view.findViewById(R.id.icon);
            icon.setImageResource(mTabsIcons[position]);
            return view;
        }

        @Override
        public Fragment getItem(int pos) {
            switch (pos) {

                case 0:
                    return PageFragment.newInstance(1);

                case 1:
                    return PageFragment.newInstance(2);
                case 2:
                    return PageFragment.newInstance(3);

            }
            return null;
        }

        @Override
        public int getCount() {
            return PAGE_COUNT;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTabsTitle[position];
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

下载完整示例项目

  • 根据Google的设计指南,不应使用滑动动作在标签之间切换.https://www.google.com/design/spec/components/bottom-navigation.html#bottom-navigation-style (8认同)

San*_*0rd 13

谷歌在25.0.0版的设计支持库之后推出了BottomNavigationView.但它带有以下限制:

  1. 您无法删除标题和中心图标.
  2. 你不能改变标题文字大小.
  3. Y̶o̶u̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶̶
  4. 它没有BottomNavigationBehavior:所以没有通过CordinatorLayout与FAB或SnackBar集成.
  5. 每个menuItem都是FrameLayout的纯扩展,因此它没有任何漂亮的圆形显示效果

因此,使用这个拳头版本的BottomNavigationView可以做到的最大值是:(没有任何反映或自己实现lib).

在此输入图像描述

所以,如果你想要其中任何一个.你可以使用像roughike/BottomBar这样的第三方库,或者自己实现lib.

  • 只是为了记录,你可以改变背景颜色,你可以改变标题文字大小(因为我发现了我正在使用的方法的其他问题).采用了android:背景="XXX"将改变其背景颜色,但如果还指定了一个`应用:itemBackground ="XXX"`所有项目分享这个颜色,你不能看到背景(除非你加透明度,但仍然,所有图标共享相同的颜色).非常蹩脚的是,Android团队发布了这样一个糟糕的组件......总是75%完成,错过了额外的一英里,这将使它变得更好. (4认同)

Sur*_*gch 12

如何添加底部导航栏

接受的答案是好的,但我发现组织有点难以遵循,一些内容是不必要的.以下完整示例显示了如何使底部导航栏与问题中的图像类似.

在此输入图像描述

添加设计支持库

将此行添加到应用程序的build.grade文件旁边的其他支持库事物.

implementation 'com.android.support:design:28.0.0'
Run Code Online (Sandbox Code Playgroud)

用当前的版本替换版本号.

创建活动布局

我们在布局中添加的唯一特殊内容是BottomNavigationView.要在单击图标和文本时更改其颜色,可以使用a BottomAppBar而不是直接指定颜色.为简单起见,这里省略了.

activity_main.xml中

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

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

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

请注意,我们过去常常BottomAppBar把它放在底部.

定义菜单项

上面提到的底部导航视图的xml BottomAppBar.这是我们视图中定义每个项目的内容.我们现在就做.您所要做的就是添加菜单资源,就像操作栏或工具栏一样.

bottom_nav_menu.xml

<?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_recents"
        android:enabled="true"
        android:icon="@drawable/ic_action_recents"
        android:title="Recents"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/action_favorites"
        android:enabled="true"
        android:icon="@drawable/ic_action_favorites"
        android:title="Favorites"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/action_nearby"
        android:enabled="true"
        android:icon="@drawable/ic_action_nearby"
        android:title="Nearby"
        app:showAsAction="ifRoom" />
</menu>
Run Code Online (Sandbox Code Playgroud)

您需要在项目中添加适当的图标.如果您转到文件>新建>图像资源并选择操作栏和选项卡图标作为图标类型,这不是很困难.

添加一个选定的侦听器项

这里没有什么特别的事情发生.我们只是在Activity的BottomNavigationView方法中向Bottom Navigation Bar添加一个监听器.

public class MainActivity extends AppCompatActivity {

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

        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_recents:
                        Toast.makeText(MainActivity.this, "Recents", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.action_favorites:
                        Toast.makeText(MainActivity.this, "Favorites", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.action_nearby:
                        Toast.makeText(MainActivity.this, "Nearby", Toast.LENGTH_SHORT).show();
                        break;

                }
                return true;
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

需要更多帮助?

我学习了如何观看以下YouTube视频.电脑的声音有点奇怪,但演示非常明确.


Bal*_*lei 10

正如Sanf0rd所述,Google推出BottomNavigationView作为Design Support Library版本25.0.0的一部分.他提到的限制大多是正确的,除了你可以改变视图的背景颜色甚至文本颜色和图标色调颜色.当您添加4个以上的项目时,它也会有动画(遗憾的是它无法手动启用或禁用).

我通过示例和附带的存储库编写了一个详细的教程,您可以在这里阅读: https ://blog.autsoft.hu/now-you-can-use-the-bottom-navigation-view-in-the-设计支持库/


它的要点

您必须在应用级别添加以下内容build.gradle:

compile 'com.android.support:appcompat-v7:25.0.0'  
compile 'com.android.support:design:25.0.0'
Run Code Online (Sandbox Code Playgroud)

您可以将它包含在您的布局中,如下所示:

<android.support.design.widget.BottomNavigationView  
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/bottom_navigation_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:itemBackground="@color/darkGrey"
        app:itemIconTint="@color/bottom_navigation_item_background_colors"
        app:itemTextColor="@color/bottom_navigation_item_background_colors"
        app:menu="@menu/menu_bottom_navigation" />
Run Code Online (Sandbox Code Playgroud)

您可以通过菜单资源指定项目,如下所示:

<?xml version="1.0" encoding="utf-8"?>  
<menu  
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/action_one"
        android:icon="@android:drawable/ic_dialog_map"
        android:title="One"/>
    <item
        android:id="@+id/action_two"
        android:icon="@android:drawable/ic_dialog_info"
        android:title="Two"/>
    <item
        android:id="@+id/action_three"
        android:icon="@android:drawable/ic_dialog_email"
        android:title="Three"/>
    <item
        android:id="@+id/action_four"
        android:icon="@android:drawable/ic_popup_reminder"
        android:title="Four"/>
</menu>
Run Code Online (Sandbox Code Playgroud)

您可以将色调和文本颜色设置为颜色列表,以便突出显示当前选定的项目:

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android">

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

</selector>
Run Code Online (Sandbox Code Playgroud)

最后,您可以使用OnNavigationItemSelectedListener处理项目的选择:

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {  
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment fragment = null;
        switch (item.getItemId()) {
            case R.id.action_one:
                // Switch to page one
                break;
            case R.id.action_two:
                // Switch to page two
                break;
            case R.id.action_three:
                // Switch to page three
                break;
        }
        return true;
    }
});
Run Code Online (Sandbox Code Playgroud)


Ash*_*rma 8

您可以尝试其他替代库: - https://github.com/Ashok-Varma/BottomNavigation

<com.ashokvarma.bottomnavigation.BottomNavigationBar
    android:layout_gravity="bottom"
    android:id="@+id/bottom_navigation_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

BottomNavigationBar bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);

bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.ic_home_white_24dp, "Home"))
                .addItem(new BottomNavigationItem(R.drawable.ic_book_white_24dp, "Books"))
                .addItem(new BottomNavigationItem(R.drawable.ic_music_note_white_24dp, "Music"))
                .addItem(new BottomNavigationItem(R.drawable.ic_tv_white_24dp, "Movies & TV"))
                .addItem(new BottomNavigationItem(R.drawable.ic_videogame_asset_white_24dp, "Games"))
                .initialise();
Run Code Online (Sandbox Code Playgroud)