android - 避免工具栏被 NavigationDrawer 覆盖
问题描述
我创建了一个覆盖工具栏的导航抽屉。但我希望有我的导航抽屉而不覆盖工具栏。我怎样才能做到这一点?
我想要的是左边,我的应用程序目前是怎样的:
这些是我的代码:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
DrawerLayout 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.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
这是我的主要活动:
<?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:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
解决方案
您可以添加layout_marginTop
如下
<android.support.design.widget.NavigationView
android:layout_marginTop="@dimen/abc_action_bar_default_height_material"
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
看这里,你可能会在这里找到更有趣的解决方案
推荐阅读
- visual-studio-code - 如何在 vscode 资源管理器菜单中为文件和文件夹名称设置不同的颜色?
- c# - 无法在 Windows 2012 R2 服务器上加载 DLL 'tensorflow'
- java - 未找到测试 测试 NG
- c# - 如何编写 OrderByDescending 以获取最后一条记录
- javascript - Get a number from an array different from the one in it
- marklogic - “ml-gradle”> v3.4.1 中的 401 未授权问题
- java - 如何在android studio中填充excel工作表数据并以给定应用程序的工作表格式获取日期数据
- git - 如何下载 Heroku 蛞蝓?
- python - Pytorch unet 语义分割
- karate - karate-gatling:当 gatling 馈线和模拟工作流程由于登录请求错误而失败时,不会生成报告