【问题标题】:BottomNavigationView doesn't appear after running it in the emulator在模拟器中运行BottomNavigationView后没有出现
【发布时间】:2020-03-10 21:28:36
【问题描述】:

我的 BottomNavigationView 出现在 android studio 的预览中,但是当我在模拟器中运行它时它没有出现所以它只是出现在模拟器中的片段并且程序没有给我任何错误所以我尝试切换主页布局,也没有出现!

这是我的代码

在 Home_activity xml 中

public class HomeActivity extends AppCompatActivity {

@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.activity_home);

    BottomNavigationView bottomNavigationView =findViewById(R.id.nav_view);
    bottomNavigationView.setOnNavigationItemSelectedListener(navlistener);


}

private BottomNavigationView.OnNavigationItemSelectedListener navlistener = new BottomNavigationView.OnNavigationItemSelectedListener(){

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        Fragment selectedFragment =null;

        switch (menuItem.getItemId()){

            case R.id.navigation_home:
                selectedFragment=new Home_fragment();
                break;

                case R.id.navigation_dashboard:
                selectedFragment=new Dashboard_fragment();
                break;



            case R.id.navigation_notifications:
                selectedFragment=new Notificatons_fragment();
                break;
        }

        //to show them

        getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,selectedFragment).commit();

        return true;
    }
};

}

在首页布局页面的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"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

  <fragment
      android:id="@+id/nav_host_fragment"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_above="@id/nav_view"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
      android:id="@+id/nav_view"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      app:menu="@menu/bottom_nav_menu"

      />

</RelativeLayout>

【问题讨论】:

    标签: java android xml android-studio layout


    【解决方案1】:

    在 gradle 文件中添加库

    dependencies {
       implementation 'com.google.android.material:material:1.1.0'
    }
    

    没有片段标签的 XML 文件。 fragment 在我这边出错了。

    <?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"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="?attr/actionBarSize">
    
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_alignParentBottom="true"
            app:menu="@menu/bottom_nav_menu"
    
            />
    
    </RelativeLayout>
    

    res 中菜单文件夹中的bottom_nav_menu.xml 文件

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:id="@+id/home"
            android:icon="@android:drawable/arrow_down_float"
            android:title="home" />
        <item
            android:id="@+id/id1"
            android:icon="@android:drawable/btn_plus"
            android:title="deeplink" />
    </menu>
    

    您可以在菜单中更改自定义图标。 试试看。希望可以解决您的问题。我已经测试过了 在 Android Q 模拟器上。提前致谢。

    【讨论】:

      猜你喜欢
      • 2018-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多