(使用Android Studio 2021.1.1)
action_settings
菜单和配置菜单项。onOptionsItemSelected()
以MainActivity.java
处理设置菜单,如下所示:@Override
public boolean onOptionsItemSelected(MenuItem item) {
Bundle bundle = new Bundle();
switch (item.getItemId()) {
case R.id.action_settings:
Navigation
.findNavController(this, R.id.nav_host_fragment_content_main)
.navigate(R.id.nav_settings);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Run Code Online (Sandbox Code Playgroud)
运行项目,抽屉菜单工作正常并按预期打开片段。问题是,当您单击溢出菜单打开设置片段时,它可以工作,但打开主页片段时,抽屉菜单不再工作。
经过一些测试,我发现这是因为依赖版本的原因,将其从2.4.1降级到2.3.5可以解决问题。
我的代码有问题还是因为 API 更改?如何在不降级的情况下处理这个问题?
MainActivity
在的方法中onCreate()
我添加了以下内容:
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow, R.id.nav_settings)
.setOpenableLayout(drawer)
.build();
Run Code Online (Sandbox Code Playgroud)
app
模块的build.gradle
:
plugins {
id 'com.android.application'
}
android {
compileSdk 31
defaultConfig { …
Run Code Online (Sandbox Code Playgroud) 看起来使用插件 DSL 声明一些Kotlin 插件有两种方法:使用id()
方法和kotlin()
方法。例如,可以使用 或 添加 androidid("kotlin-android")
插件kotlin("android")
。对于 也是如此,kapt
但对于 则不然parcelize
。为什么不能这样呢kotlin("parcelize")
?这种差异有原因吗?我尝试查找相关文档,但这并没有让我走得太远。
我很难尝试更改选定/未选定图标和活动指示器的颜色。这些文档没有示例或正确的 Kdocs,而且我似乎无法在网上找到任何示例(请向我指出您知道的任何示例)。图标只是不改变颜色并保持黑色。
我的NavigationBar
看起来像这样:
NavigationBar(
containerColor = NavBarColor,
contentColor = ContentColor, // <-- Can't tell what this is for.
modifier = Modifier
.align(Alignment.BottomCenter)
) {
// ...
destinations.forEachIndexed { index, item ->
NavigationBarItem(
selected = currentDestination?.hierarchy?.any { it.route == item.route } == true,
onClick = {
// ...
},
icon = {
when (index) {
0 -> {
Icon(
imageVector = Icons.Rounded.Add,
contentDescription = stringResource(id = item.description)
)
}
1 -> {
Icon(
imageVector = Icons.Rounded.Home,
contentDescription …
Run Code Online (Sandbox Code Playgroud) android material-ui android-jetpack-compose material-you android-jetpack-compose-material3
android ×3
android-jetpack-compose-material3 ×1
kotlin ×1
kotlin-dsl ×1
material-ui ×1
material-you ×1