java - 无法获取 BottomNavigationView 的 id

标签 java android bottomnavigationview

我正在为我的应用程序设置底部导航。我想设置/自定义菜单的操作,但我在代码中遇到问题。它找不到底部导航 View 的 id“bottomNavView_bar”。我收到错误“无法解析符号‘bottomNavView_bar’”

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
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">

<!--parent relative layout-->
<RelativeLayout
android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/parent_relative_layout">

    <!--top bar layout-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/relLayoutTopbar">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >

            <com.google.android.material.tabs.TabLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/tabs">

            </com.google.android.material.tabs.TabLayout>
        </com.google.android.material.appbar.AppBarLayout>
    </RelativeLayout>

    <!--middle layout-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/relLayoutMiddle"
        android:layout_below="@id/relLayoutTopbar"
        android:background="@color/mainbackgroud">

        <!--view pager-->
        <androidx.viewpager.widget.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/homecontainer">

        </androidx.viewpager.widget.ViewPager>
    </RelativeLayout>

    <!--bottom bar layout-->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/relLayoutBottombar"
        android:layout_alignParentBottom="true">


<com.google.android.material.bottomnavigation.BottomNavigationView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/bottomNavView_bar"
            app:itemBackground="@color/bgBottomNavigation"
            android:foreground="?attr/selectableItemBackground"
            app:itemIconTint="@android:color/white"
            app:itemTextColor="@android:color/white"
            app:menu="@menu/bottom_navigation_menu">

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

    </RelativeLayout>

</RelativeLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

这是菜单文件bottom_navigation_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/navigation_shop"
    android:icon="@drawable/news"
    android:title="@string/title_home" />

<item
    android:id="@+id/navigation_games"
    android:icon="@drawable/hockeystick"
    android:title="@string/title_games" />

<item
    android:id="@+id/navigation_stats"
    android:icon="@drawable/graphic"
    android:title="@string/title_stats" />

<item
    android:id="@+id/navigation_settings"
    android:icon="@drawable/settings"
    android:title="@string/title_settings" />
</menu>

这是我的 MainActivity.java 的一部分

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

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

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

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

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

    mSectionsPagerAdapter = new 
SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = findViewById(R.id.homecontainer);
    setupViewPager(mViewPager);

    TabLayout tabLayout = findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    tabLayout.getTabAt(0).setIcon(R.drawable.ic_rss_feed);
    tabLayout.getTabAt(1).setIcon(R.drawable.ic_alarm);

    //bottom navigation bar

    BottomNavigationView navigation = 
(BottomNavigationView)findViewById(R.id.bottomNavView_bar); //trying to get the view id here
BottomNavigationViewHelper.disableShiftMode(navigation);
}

最佳答案

选择工具>-android>-SDK 管理器

并下载适用于您项目的目标 SDK 版本的 SDK 构建工具、SDK 平台和 Google API(Google API 存储诸如“import android.view.Menu”等包)(如屏幕截图所示为 20) 。如果您不确定必须下载哪些项目,可以选择全部。 (当然会花费更多时间下载)。

重新启动IDE

关于java - 无法获取 BottomNavigationView 的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57429418/

相关文章:

java - 将 Firestore 时间戳存储到 ArrayList<String>,然后存储到日期

java - 如何将参数自定义 View 传递给 Activity 类

android - 由于 : 'null' - Android Studio 3. 5,安装失败

android - 使用 viewpager 在 View 中滚动子项

java - 安卓 : How to set a default value for an argument variable

java - 无法从 CentOS 8 服务器使用 MS JDBC 驱动程序连接到 SQL Server 服务器 : SSL error but not using secure connection

android - 对话框在屏幕旋转android时关闭

android - 仅在单击 BottomNavigationView 时显示的项目

android - 如何将新 Material BottomAppBar 实现为 BottomNavigationView

android - 返回到 'Navigation Architecture Component' 中的同一选项卡时,嵌套 fragment 的状态丢失