我怎样才能允许多个活动共享同一个膨胀的“包含”活动?看法?

发布于 2024-11-01 00:01:10 字数 295 浏览 1 评论 0原文

我在这里看到这个问题,这让我想知道我所问的是否真的不可能:

如何在各种活动之间共享视图

基本上,我有一个共同的页脚视图,我在所有视图中都夸大了(包括)该视图。然而,它使用相同的重复代码来做到这一点。我的想法是创建一个父活动类来执行此操作,但让一个活动呈现另一个活动的视图似乎并不正确。那么我应该创建某种实用程序类,还是有更好的方法?

I see this question here, and it makes me wonder if what I'm asking isn't really possible:

How to share a view across various activities

Basically, I have a common footer view that I'm inflating (including) in all of my views. However, it uses the same repetitive code to do that. My thought was to create a parent activity class to do this, but it doesn't seem correct to have one activity render the view of another. So should I just create a utility class of some sort, or is there a better way?

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

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

发布评论

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

评论(3

·深蓝 2024-11-08 00:01:10

您可以将其他布局 XML 文件直接包含在另一个布局文件中。因此,每当您将内容设置到布局文件时,页脚就会随之而来。

如果您的页脚需要代码来驱动它,只需为其创建一个自定义类以及布局文件。然后也许在实例化期间您可以驱动需要执行的代码。

这是一个关于如何做的博客。

You can include other layout XML files directly in another layout file. So whenever you set content to a layout file, along comes your footer for the ride.

If your footer needs code to drive it, just create a custom class for it along with the layout file. Then perhaps during instantiation you can drive the code that needs to execute.

This is a blog of how to do it.

挥剑断情 2024-11-08 00:01:10

include 在重用 View 组件时非常有用。

但请记住,如果使用 include 标记时出现任何问题,请通过任意布局包装包含的视图。

include is very useful while reusing View components.

But remember, if any problem occurs while using include tag, wrap the included view by an arbitrary layout.

悲凉≈ 2024-11-08 00:01:10

如果我理解你的问题,让多个 Activity 使用同一个 View 实例的另一种方法是创建你自己的 Application 类(这非常简单)。

MyApplication 扩展了应用程序...

@Override
公共无效onCreate(),onConfigurationChanged(),onLowMemory(),onTerminate(),getIstance()。

由于只有一个静态可用的“应用程序”实例,因此它成为存储和共享需要传递的各种对象的好地方。

If I understand your question, another way of having multiple Activities use the same View instance is by doing something like creating your own Application class (it's seriously easy).

MyApplication extends Application ...

@Override
public void onCreate(), onConfigurationChanged(), onLowMemory(), onTerminate(), getIstance().

As there is only a single intance of "Application" that is statically available it makes it a good place to store and share various objects that need to be passed around.

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