我的BottomNavigationView出现在android工作室的预览中,但当我在模拟器中运行它时,它没有出现,所以它只是出现在模拟器中的片段,程序没有给我任何错误,所以我试着切换主页布局,但它也没有出现!
这是我的代码
在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>
在渐变文件中添加库
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 菜单文件夹中的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模拟器上。提前谢谢。