首页 > 解决方案 > android studio中的底部导航栏

问题描述

我创建了一个简单的应用程序,它有一个底部导航栏。此导航栏有 3 个项目(拨号盘、设备、同步)。我也使用了片段。基本上,当用户单击菜单项(例如:设备)时,应用程序会显示设备片段。请注意,我之前没有实现底部导航栏。但是,当我运行应用程序时,会一直停止。我找不到任何解决方案。谁能帮我这个?

// this is the main activity
package com.example.bottomnavigation;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.view.MenuItem;

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 bottomNav = findViewById(R.id.bottom_navigation);
        bottomNav.setOnNavigationItemSelectedListener(navListner);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                new DialPadFragment()).commit();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListner =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                    Fragment selectedFragment = null;

                    switch (menuItem.getItemId()){
                        case R.id.nav_dial:
                            selectedFragment = new DialPadFragment();
                            break;
                        case R.id.nav_device:
                            selectedFragment = new DeviceFragment();
                            break;
                        case R.id.nav_sync:
                            selectedFragment = new SyncFragment();
                            break;
                    }
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                            selectedFragment).commit();
                    return  true;
                }
            };
}

// this is the main activity 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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_navigation"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_navigation"
        app:itemTextColor="@color/colorBlack"
        android:background="?android:attr/windowActionBar"/>


</RelativeLayout>

// This is one of the fragment I have created.
<?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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Device"
        android:textSize="30sp"
        android:layout_centerInParent="true"/>

</RelativeLayout>

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

    <item
        android:id="@+id/nav_dial"
        android:icon="@drawable/ic_dialpad_black_24dp"
        android:title="@string/dialPad"/>

    <item
        android:id="@+id/nav_device"
        android:icon="@drawable/ic_device_black_24dp"
        android:title="@string/device"/>

    <item
        android:id="@+id/nav_sync"
        android:icon="@drawable/ic_sync_black_24dp"
        android:title="@string/Sync"/>

</menu>

标签: android-studio

解决方案


推荐阅读