这个新的 Android studio 3.5 Navigation Drawer 示例代码如何运行
Posted
技术标签:
【中文标题】这个新的 Android studio 3.5 Navigation Drawer 示例代码如何运行【英文标题】:How does this new Android studio 3.5 Navigation Drawer sample code run 【发布时间】:2020-01-11 14:33:28 【问题描述】:我在学习 android Studio 并了解了有关抽屉式导航的一些知识。 后来我将我的 Android Studio 更新到 3.5 版本 现在我看到旧代码已经消失了。 我看到的新代码,因为我是一个新学习者,我完全困惑这个新代码如何用于片段, 是的,我尝试过搜索,但没有一个有效 有人可以帮我解决以下代码吗?
public class HomeActivity extends AppCompatActivity
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
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();
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();
我想在点击菜单后打开相关的片段。
【问题讨论】:
【参考方案1】:来自文件 xml >>activity_main.xml 查看默认顺序
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
<include
layout="@layout/app_bar_main"
android:layout_
android:layout_ />
更改为以下顺序:
<include
layout="@layout/app_bar_main"
android:layout_
android:layout_ />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
在java中使用
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener()
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull
NavDestination destination, @Nullable Bundle arguments)
if (destination.getId() == R.id.nav_home)
Toast.makeText(MainActivity.this, "home", Toast.LENGTH_LONG).show();
if (destination.getId() == R.id.nav_gallery)
Toast.makeText(MainActivity.this, "nav_gallery", Toast.LENGTH_LONG).show();
if (destination.getId() == R.id.nav_slideshow)
Toast.makeText(MainActivity.this, "nav_slideshow", Toast.LENGTH_LONG).show();
if (destination.getId() == R.id.nav_tools)
Toast.makeText(MainActivity.this, "nav_tools", Toast.LENGTH_LONG).show();
if (destination.getId() == R.id.nav_share)
Toast.makeText(MainActivity.this, "nav_share", Toast.LENGTH_LONG).show();
if (destination.getId() == R.id.nav_send)
Toast.makeText(MainActivity.this, "nav_send", Toast.LENGTH_LONG).show();
);
【讨论】:
很棒的兄弟...我用的是旧方法,检查了这个方法完美...以上是关于这个新的 Android studio 3.5 Navigation Drawer 示例代码如何运行的主要内容,如果未能解决你的问题,请参考以下文章
Android Studio 3.5测试版加载SDK java源码
Android Studio 3.5 错误:无法解析“:app@debug/compileClasspath”的依赖关系
android studio 升级到3.5版本报错gradle无法获取pom文件
Android Studio 3.5出现的Could not Get'XXX.pom的解决方法