gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:01:07 25 4
gpt4 key购买 nike

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

这是我的代码

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

最佳答案

Add library in gradle file

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

Xml file without fragment tag. 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

<?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. Thanks in advance.

关于java - 在模拟器中运行后不出现BottomNavigationView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60626196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com