Android: 在导航抽屉中链接到URL

huangapple go评论39阅读模式
英文:

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来连接目的地和菜单项 - 这是在点击菜单项时启动新目的地的方式。导航支持目的地,允许你在点击菜单项时启动一个活动。你会在图中添加一个使用与你的菜单项相同ID的活动目的地:

;

然后默认的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.

huangapple
  • 本文由 发表于 2020年8月3日 06:24:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63221599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定