简体   繁体   English

在模拟器中运行BottomNavigationView后不出现

[英]BottomNavigationView doesn't appear after running it in the emulator

my BottomNavigationView appear in the preview of the android studio but when i run it in emulator it doesn't appear so it's only the fragment which appear in the emulator and the program isn't give me any error so i tried to switch the home page layout and it also doesn't appear!我的BottomNavigationView出现在android studio的预览中,但是当我在模拟器中运行它时它没有出现所以它只是出现在模拟器中的片段并且程序没有给我任何错误所以我试图切换主页布局,它也没有出现!

and here is my code这是我的代码

in the Home_activity xml在 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;
    }
};

} }

in xml of the home layout page在主页布局页面的 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>

Add library in gradle file在 gradle 文件中添加库

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

Xml file without fragment tag.没有片段标签的 XML 文件。 fragment is giving error on my side.片段在我这边出错。

<?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>

bottom_nav_menu.xml file in menu folder in res res 中 menu 文件夹中的 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>

You can change your custom icons in menu.您可以在菜单中更改自定义图标。 Try it.尝试一下。 Hopefully, may resolve you issue your issue.希望可以解决您的问题。 I have tested it on Android Q emulator.我已经在 Android Q 模拟器上测试过了。 Thanks in advance.提前致谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM