5.25

所花时间(包括上课):1

打码量(行):130

博客量(篇):1

了解到知识点:学习底部栏

 

 package com.example.myapp;

 

import android.os.Bundle;

import android.view.MenuItem;

import android.widget.Toast;

 

import androidx.annotation.NonNull;

import androidx.appcompat.app.AppCompatActivity;

 

import com.google.android.material.bottomnavigation.BottomNavigationView;

 

public class MainActivity extends AppCompatActivity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

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

        bottomNavigationView.setOnNavigationItemSelectedListener(

            new BottomNavigationView.OnNavigationItemSelectedListener() {

                @Override

                public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                    switch (item.getItemId()) {

                        case R.id.menu_home:

                            // 处理点击首页按钮的操作

                            Toast.makeText(MainActivity.this, "Home clicked", Toast.LENGTH_SHORT).show();

                            return true;

                        case R.id.menu_dashboard:

                            // 处理点击仪表盘按钮的操作

                            Toast.makeText(MainActivity.this, "Dashboard clicked", Toast.LENGTH_SHORT).show();

                            return true;

                        case R.id.menu_notifications:

                            // 处理点击通知按钮的操作

                            Toast.makeText(MainActivity.this, "Notifications clicked", Toast.LENGTH_SHORT).show();

                            return true;

                    }

                    return false;

                }

            });

    }

}

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent">

 

    <!-- 主要内容布局 -->

 

    <com.google.android.material.bottomnavigation.BottomNavigationView

        android:id="@+id/bottomNavigationView"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"

        app:menu="@menu/bottom_navigation_menu" />

 

</RelativeLayout>

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item

        android:id="@+id/menu_home"

        android:title="Home"

        android:icon="@drawable/ic_home" />

 

    <item

        android:id="@+id/menu_dashboard"

        android:title="Dashboard"

        android:icon="@drawable/ic_dashboard" />

 

    <item

        android:id="@+id/menu_notifications"

        android:title="Notifications"

        android:icon="@drawable/ic_notifications" />

</menu>

posted @ 2024-05-25 22:40  赵千万  阅读(1)  评论(0编辑  收藏  举报