Android 布局:可重用组件 UI 可能吗?

发布于 2024-08-22 05:27:56 字数 216 浏览 8 评论 0原文

我将以此作为序言,我刚刚开始学习 Android,所以要温柔。 我有 ASP.NET/Silverlight 背景,所以我一直在寻找与控件相关的东西。

我想在其他布局中重用布局(ListView 项模板)。

这样,在我的其他布局中,我只需添加 即可显示它。

这是可能的吗?或者有更好的方法吗?

I'll preface this with, I've just started learning Android so be gentle.
I come from an ASP.NET / Silverlight background so I was looking for something along the lines of controls.

I want to reuse a layout (a ListView item template) in other layouts.

Such that in my other layouts I can just add <myListItem /> to show it.

Is this, or anything like it possible? or are there better ways?

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

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

发布评论

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

评论(1

め可乐爱微笑 2024-08-29 05:27:56

这是很有可能的;您只需要使用 标签即可。基本上,您将布局放入文件中,然后执行

<include layout="@layout/my_list_item_template" />

以下操作:Romain Guy 详细介绍了如何使用它:http://www.curious-creature.org/2009/02 /25/android-layout-trick-2-include-to-reuse/

(Android 文档

(我假设您想要的是可重用布局,而不是自定义组件。区别在于,可重用布局就像重用标准组件的片段,而自定义组件则使用当您需要扩展特定小部件的功能时,例如 TextView 或 Button,如果您想要自定义组件,那么您将不得不对 Erich Douglass 的答案进行更多的跑腿工作以获取更多信息。)

This is very possible; you just need to use the <include /> tag. Basically, you put your layout in a file, then you do:

<include layout="@layout/my_list_item_template" />

Romain Guy goes into detail on how to use it here: http://www.curious-creature.org/2009/02/25/android-layout-trick-2-include-to-reuse/

(Android documentation)

(I am assuming that what you want is a reusable layout, not a custom component. The difference being, a reusable layout is like reusing snippets of standard components, whereas a custom component is used when you need to extend the functionality of a particular widget, like a TextView or Button. If you want a custom component, then you'll have to do a lot more legwork to Erich Douglass' answer for more on that.)

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