- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试创建一个带有抽屉导航 Activity 的新项目。使用 View 模型获得自动生成的 fragment 。单击导航菜单时,它不会导航到相应的 fragment 。
build.gradle
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment:2.0.0'
implementation 'androidx.navigation:navigation-ui:2.0.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
MainActivity.class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// 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();
final NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
我用导航菜单验证了导航图中的 ID,它们是相同的。我是导航图的新手。任何帮助将不胜感激。
最佳答案
在你的activity_main
布局中使用
<androidx.drawerlayout.widget.DrawerLayout>
<include/>
<com.google.android.material.navigation.NavigationView/>
</androidx.drawerlayout.widget.DrawerLayout>
代替
<androidx.drawerlayout.widget.DrawerLayout>
<com.google.android.material.navigation.NavigationView/>
<include/>
</androidx.drawerlayout.widget.DrawerLayout>
原因在doc :
To use a
DrawerLayout
, position your primary content view as the first child with width and height ofmatch_parent
and nolayout_gravity
>. Add drawers as child views after the main content view and set thelayout_gravity
appropriately. Drawers commonly usematch_parent
for height with a fixed width.
关于android - 带导航图的抽屉导航不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58387993/
我有三个 Controller 。让我们依次说 Controller A、B 和 C。 Controller A 中的导航标题颜色为红色。 Controller B 为蓝色, Controller C
我有一个导航图,它使用此 fragment 作为主 Activity XML 中的主页。 我有一个带有菜单的抽屉布局,当我单击抽屉导航按钮时,我无法使导航正常工作(它在主 fragm
我是一名优秀的程序员,十分优秀!