英文:
Android: Linking to URL in Navigation Drawer
问题
我正在使用导航组件库来创建我的导航抽屉。我想要在抽屉中的一个项目链接到外部网站。官方文档 没有提到外部链接的内容。我该如何实现这个功能?
我可以创建一个临时的片段并首先导航到它,但在我看来,这个解决方案并不是非常优雅。
英文:
I am using the Navigation Components library for my Navigation Drawer. I want one item in said drawer to link to an external Website. The offical documentation doesn't say anything about external links. How do I implement that?
I could create a temporary fragment and navigate to that first but this solution is not very elegant in my opinion.
答案1
得分: 3
以下是已翻译的内容:
这行代码
NavigationUI.setupWithNavController(navigationView, navController);
在内部调用setNavigationItemSelectedListener来连接目的地和菜单项 - 这是在点击菜单项时启动新目的地的方式。导航支持
然后默认的setupWithNavController将为你调用startActivity。
英文:
The line
NavigationUI.setupWithNavController(navigationView, navController);
Calls setNavigationItemSelectedListener
internally to connect destinations to menu items - this is how a new destination is launched when you click a menu item. Navigation supports <activity>
destinations, allowing you to start an activity when clicking on a menu item. You'd add an activity destination to your graph that uses the same id as your menu item:
<activity
android:id="@+id/nav_login_activity"
app:action="android.intent.action.VIEW"
app:data="https://www.your_url_here.com"/>
Then the default setupWithNavController
will call startActivity
for you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论