Android Studio初学笔记-Day29-ButtomNavigationView

BottomNavigationView

BottomNavigationView为底部导览按钮,及类似於LINE里最底下的那一排(首页、聊天等),今天就来介绍BottomNavigationView的用法并和前一篇的Fragment做结合。
首先,先引用此套件并sync now

implementation 'com.google.android.material:material:1.3.0-alpha01'

在res的资料夹中新增menu资料夹,如同ToolBar时的流程,忘记的同学可以回头看看。
创建完并在此资料夹中新增一xml档

activity_main.layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    
    <FrameLayout
        android:id="@+id/fl_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/navigation"
        android:layout_marginBottom="0dp"
        tools:layout_editor_absoluteX="0dp" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="0dp"
        android:layout_marginBottom="0dp"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/buttonlist" />
</RelativeLayout>

Fragment的部分大致与前几篇中的内容相同,就不多做讲解,不清楚怎麽设定的可以去前几天的Fragment回顾一下。BottomNavigationView比较需要注意的是app:menu属性,这里要把刚才menu档中的xml档放进来做绑定,最後来做转换Fragment的设定吧。

public class MainActivity extends AppCompatActivity {
    private AFragment aFragment;
    private BFragment bFragment;
    private CFragment cFragment;
    private Button bt1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        aFragment = new AFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.fl_container,aFragment,"A")
                                                      .commitAllowingStateLoss();
        BottomNavigationView bottomNavigationView = 
                            (BottomNavigationView)findViewById(R.id.navigation);
        //BottomNavigationView的监听事件设定
        bottomNavigationView.setOnNavigationItemSelectedListener(new 
                        BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            //检测哪个项目被选择
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home:
                        if(aFragment==null)
                            aFragment=new AFragment();
       getSupportFragmentManager().beginTransaction().replace(R.id.fl_container,aFragment,"A")
                                                     .commitAllowingStateLoss();
                        break;
                    case R.id.dashboard:
                        if(bFragment==null)
                            bFragment=new BFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_container,bFragment,"B")
                                                      .commitAllowingStateLoss();
                        break;
                    case R.id.notify:
                        if(cFragment==null)
                            cFragment=new CFragment();   
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_container,cFragment,"C")
                                                      .commitAllowingStateLoss();
                        break;
                }
                return true;
            }
        });
    }
}

Fragment的切换就如之前一样,依样画葫芦地加入监听事件内设定就完成了,跟之前的toolbar有点像,可以回头复习看看它们的相似之处。

成果

https://ithelp.ithome.com.tw/upload/images/20210908/20139136SlFon0kmfv.pnghttps://ithelp.ithome.com.tw/upload/images/20210908/20139136fl7dy2hsbp.png

今天BottomNavigationView就讲解到这,谢谢大家~/images/emoticon/emoticon41.gif


<<:  Day29 MANO开源专案使用之kube5gnfvo - 样板介绍篇

>>:  #28- D3.js 地图动起来!用SVG viewbox/D3 fitExtent让地图置中

C# delegate 委派

IT邦第二篇 就献给委派了 记得当年第一次看到 += 这东西的时候 问问前辈这是什麽 前辈只有跟我说...

[Day28] Snackbar提示功能

Snackbar是一个类似Toast的提示功能,但却比Toast更多功能,外观上也较为美观,现在就来...

EP24 - [TDD] OrderPayQuery 查询付款结果 (2/2)

Youtube 频道:https://www.youtube.com/c/kaochenlong ...

Day 12: 人工神经网路初探 深度学习

深度学习 深度学习是多层人工精神网路或多层感知器的另一种称呼,还有多种不同型态的深度学习系统,根据神...

表单处理 Object 里的 Object

今天来看看一个常见问题。 { first_name: 'chris', last_name: 'wa...