Android 布局:可重用的组件 UI 是否可能? [英] Android Layout: Is reusable component UI possible?
问题描述
我会先介绍一下,我刚刚开始学习 Android,所以请保持温和.我来自 ASP.NET/Silverlight 背景,所以我一直在寻找控件方面的东西.
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.
我想在其他布局中重用一个布局(一个 ListView 项目模板).
I want to reuse a layout (a ListView item template) in other layouts.
这样,在我的其他布局中,我只需添加 <myListItem/>
来显示它.
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?
推荐答案
这很有可能;你只需要使用
标签.基本上,你把你的布局放在一个文件中,然后你做:
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 在此处详细介绍了如何使用它:http://www.curious-creature.org/2009/02/25/android-layout-trick-2-include-to-reuse/
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/
(我假设您想要的是可重用的布局,而不是自定义组件.不同之处在于,可重用的布局就像重用标准组件的片段,而自定义组件则是在您需要扩展功能时使用一个特定的小部件,如 TextView 或 Button.如果您想要一个自定义组件,那么您将不得不为 Erich Douglass 的回答做更多的工作以获取更多信息.)
(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.)
这篇关于Android 布局:可重用的组件 UI 是否可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!