如何在每个 activity android 中添加抽屉?
How to add drawer in every activity android?
我是 android 编程新手。我想在每个 activity 中添加抽屉视图。
我已经尝试了很多通过扩展 main activity 在每个 activity 中添加抽屉。但它正在崩溃。
请有人指导我如何从 scratch.below 添加所有 activity 的抽屉我添加了我的 HomeActivity
HomeActivity.java
public class Home extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {
protected DrawerLayout drawer;
private AppBarConfiguration mAppBarConfiguration;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
R.id.nav_tools, R.id.nav_share, R.id.nav_send)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
return false;
}
}
尝试创建一个 single-activity app,这意味着使用 fragments
而不是活动。然后您可以轻松地在 MainActivity
中创建抽屉并从那里开始片段。
有关片段的更多信息,请查看here
以及如何将 drawer 添加到现有的 activity。
我是 android 编程新手。我想在每个 activity 中添加抽屉视图。 我已经尝试了很多通过扩展 main activity 在每个 activity 中添加抽屉。但它正在崩溃。
请有人指导我如何从 scratch.below 添加所有 activity 的抽屉我添加了我的 HomeActivity
HomeActivity.java
public class Home extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {
protected DrawerLayout drawer;
private AppBarConfiguration mAppBarConfiguration;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
R.id.nav_tools, R.id.nav_share, R.id.nav_send)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
return false;
}
}
尝试创建一个 single-activity app,这意味着使用 fragments
而不是活动。然后您可以轻松地在 MainActivity
中创建抽屉并从那里开始片段。
有关片段的更多信息,请查看here
以及如何将 drawer 添加到现有的 activity。