我正在尝试用碎片创建导航抽屉。我想在下一个片段中隐藏/删除工具栏。我想要没有工具栏的完整的空片段。但每当我尝试我的导航抽屉不起作用。我想要带工具栏+导航抽屉的前三页菜单(片段)和带导航抽屉的下两个片段活动(不包括工具栏)
Below is my code activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#ffffff"
/>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<FrameLayout
android:id="@+id/fragment_container2"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu"
android:id="@+id/nav_view"
android:layout_gravity="start">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
这是mainactivity.java
package com.example.guvi;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
this.setTitle("");
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new CoursesFragment()).commit();
NavigationView naview = findViewById(R.id.nav_view);
naview.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@SuppressLint("NonConstantResourceId")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_course:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new CoursesFragment()).commit();
break;
case R.id.nav_invfriends:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new InviteFragment2()).commit();
break;
case R.id.nav_contactus:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new ContactFragment()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
1条答案
按热度按时间pbpqsu0x1#
你可以做的是:
在活动(包含片段的“base”)中,将工具栏设置为隐藏在oncreate()调用中的mainactivity.java中
“toolbar.setvisibility(view.invisible)”如果您想将其隐藏,或“toolbar.setvisibility(view.gone)”如果您想将其从活动中消失
我不知道您是否也要将xml片段调用到activity.xml中,但我建议将它们分开放在不同的xml中,这样更易于管理。
在mainactivity.java中,只要调用“toolbar.setvisibility(view.visible)”,就可以使用单独的类来表示每个片段
我希望它能起作用