无法获取BottomNavigationView的ID

时间:2019-08-09 11:46:27

标签: java android bottomnavigationview

我正在为我的应用设置底部导航。我想为菜单设置/自定义操作,但是我在代码中遇到了问题。它找不到bottomnavigationview的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);
}

1 个答案:

答案 0 :(得分:0)

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

并为您的项目的目标SDK版本下载SDK构建工具,SDK平台和Google API(Google API存储诸如“ import android.view.Menu”之类的包)(如屏幕截图所示,为20) 。如果您不确定必须下载哪些项目,则可以选择所有这些项目。 (将花费更多时间下载课程)。

重新启动IDE