То, что я сделал: Я добавил навигационный ящик и нижнюю панель навигации, как показано ниже. xml и хотел работать с обоими. Но когда я пытаюсь запустить его, он говорит, что
Причина: java .lang.ClassCastException: android .widget.RelativeLayout не может быть приведен к androidx.drawerlayout.widget.DrawerLayout
main. 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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/navigationView" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorAccent"
app:itemBackground="?selectableItemBackgroundBorderless"
app:itemHorizontalTranslationEnabled="true"
app:itemIconTint="@drawable/nav_item_color_state"
app:itemTextColor="@drawable/nav_item_color_state"
app:menu="@menu/bottom_nav" />
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu"
/>
</RelativeLayout>
основной вид деятельности:
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
private DrawerLayout drawerLayout;
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this,drawerLayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close );
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
//loadFragment(new HomeFragment());
getSupportFragmentManager().beginTransaction().replace(R.id.container,new HomeFragment()).commit();
BottomNavigationView navigation = findViewById(R.id.navigationView);
navigation.setOnNavigationItemSelectedListener(navigationItemSelectedListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//swith bottom nav clicked
Fragment selectedFragment = null;
switch (item.getItemId()){
case R.id.home:
selectedFragment = new HomeFragment();
break;
case R.id.calander:
selectedFragment = new CalenderFragment();
break;
case R.id.notes:
selectedFragment = new NoteFragment();
break;
}
//this is to load fragemt
getSupportFragmentManager().beginTransaction().replace(R.id.container,selectedFragment).commit();
return true;
}
};
@Override
public void onBackPressed() {
if(drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else{
super.onBackPressed();
}
}
}
Затем я изменил выше это: Относительный макет был удален и заменен на «макет ящика», но теперь при компиляции ошибки нет, но я могу видеть только блок навигации в полноэкранном режиме, но не нижнюю панель навигации, панель инструментов или фрагмент.
отредактированный основной. xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/navigationView" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorAccent"
app:itemBackground="?selectableItemBackgroundBorderless"
app:itemHorizontalTranslationEnabled="true"
app:itemIconTint="@drawable/nav_item_color_state"
app:itemTextColor="@drawable/nav_item_color_state"
app:menu="@menu/bottom_nav" />
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu"
/>
</androidx.drawerlayout.widget.DrawerLayout>
отредактированный основной вид деятельности:
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this,drawerLayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close );
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
//loadFragment(new HomeFragment());
getSupportFragmentManager().beginTransaction().replace(R.id.container,new HomeFragment()).commit();
BottomNavigationView navigation = findViewById(R.id.navigationView);
navigation.setOnNavigationItemSelectedListener(navigationItemSelectedListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//swith bottom nav clicked
Fragment selectedFragment = null;
switch (item.getItemId()){
case R.id.home:
selectedFragment = new HomeFragment();
break;
case R.id.calander:
selectedFragment = new CalenderFragment();
break;
case R.id.notes:
selectedFragment = new NoteFragment();
break;
}
//this is to load fragemt
getSupportFragmentManager().beginTransaction().replace(R.id.container,selectedFragment).commit();
return true;
}
};
/*
this is to load fragemt
*/
/*
private void loadFragment (Fragment fragment){
//load fragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container,fragment);
transaction.addToBackStack(null);
transaction.commit();
}
*/
@Override
public void onBackPressed() {
if(drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else{
super.onBackPressed();
}
}
}
Буду очень признателен за любые предложения по улучшению этого. Спасибо!