就这么迷失了...ViewSwitcher?创建一个 Activity 然后添加到 ViewSwitcher 中?

发布于 2024-12-05 19:51:27 字数 377 浏览 1 评论 0原文

我是 Android 新手,发现它很残酷(似乎有无数的细节和依赖项需要记住)。

无论如何,我让 TextSwitcher1 示例应用程序正常工作,它使用 ViewSwitcher。我假设 ViewSwitcher 是可行的方法,需要显示地图或表格,用户可以选择并来回切换。

所以我在另一个应用程序中创建了我的 MapActivity,似乎有效。接下来集成到主应用程序中。所以,打电话 视图 v = findViewById(R.layout.mapview); 进而 mSwitcher.addView(v); 除了“v”为空之外。为什么?我要创建活动吗?但我还不想展示它。是否有“创建活动但隐藏它直到需要时”之类的调用?还是我找错了树?

感谢您的任何见解。

I'm new to Android and find it brutal (there seems to be an near infinite number of details and dependencies to remember).

Anywho, I got the TextSwitcher1 example app working, which uses ViewSwitcher. I'm assuming ViewSwitcher is the way to go, need to either display a map or a table, user can pick, and switch back and forth.

So I created my MapActivity in another application, seems to work. Next integrate into main app. So, call
View v = findViewById(R.layout.mapview);
and then
mSwitcher.addView(v);
except "v" is null. Why? Do I create the activity? But I don't want to show it yet. Is there such a call as "create activity but hide it until needed"? Or am I barking up the wrong tree?

Thanks for any insight.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软甜啾 2024-12-12 19:51:27

findViewById 函数根据您在 Activity 中加载的任何视图的 ID 资源 (R.id.something) 返回一个 View(使用setContentView(R.layout.main))。在示例代码中,您使用布局资源 (R.layout.mapview)。您应该扩充 XML 文件,该文件将返回一个 View,您可以使用它添加到 ViewSwitcher

示例代码:

LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.mapview, null);
mSwitcher.addView(v);

但是,您应该能够定义 XML 文件中的所有内容,而不必手动将页面添加到 ViewSwitcher 中。以下是有关如何执行此操作的一些示例代码: http://inphamousdevelopment.wordpress.com/2010/10/11/using-a-viewswitcher-in-your-android-xml-layouts/

The findViewById function returns a View based on an ID resource (R.id.something) for whatever view you have loaded in your activity (using setContentView(R.layout.main)). In your sample code, you're using a layout resource (R.layout.mapview). You should inflate the XML file, which will return a View that you can use to add to the ViewSwitcher.

Example Code:

LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.mapview, null);
mSwitcher.addView(v);

However, you should be able to define everything in your XML file and not have to manually add the pages to your ViewSwitcher. Here's some example code on how to do that: http://inphamousdevelopment.wordpress.com/2010/10/11/using-a-viewswitcher-in-your-android-xml-layouts/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文