[英]BottomNavigationView doesn't appear after running it in the emulator
我的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>
在 gradle 文件中添加庫
dependencies {
implementation 'com.google.android.material:material:1.1.0'
}
沒有片段標簽的 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">
<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 中 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>
您可以在菜單中更改自定義圖標。 嘗試一下。 希望可以解決您的問題。 我已經在 Android Q 模擬器上測試過了。 提前致謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.