在 Android 中构建用户界面 - 视图与 XML
我对 Android 开发非常陌生,虽然我了解了一般前提(甚至构建了一个小型应用程序),但我一直在查看其他开发人员的源代码,以了解如何更好地进行大型项目的开发。
阅读一名开发人员的代码基本上是对 UI 的各个部分使用 XML 布局和视图(类似于 这个问题)。我明白他在做什么,但对我来说似乎过于复杂。 XML 布局已经提供了创建对操作的响应的功能。 (例如,为 XML 中的大多数组件提供了“onClick”。)使用 XML 可以非常轻松地生成布局。
所以,我的问题是 - 我可以只使用 Activity 和 XML 布局构建整个应用程序吗?我可以选择不使用任何视图吗? (当然,这是假设一个相对简单的应用程序 - 想想任务列表或类似的东西。)或者,我是否试图简化太多?
I am very new to Android development and, while I get the general premise (and have even built a small application), I have been looking at other developer's source code to get an idea of how to better approach my development for larger projects.
One developer's code is read is basically using both XML layouts and Views for the various parts to the UI (similar to what is being asked in this question). I understand what he is doing, but it seems overly complicated to me. The XML layouts provide functionality already to create responses to actions. (For example, "onClick" is provided for most components in the XML.) Layouts can be generated very easily with the XML.
So, my question is - can I get away with building my entire application using just Activities and XML layouts? Can I choose not to use any Views? (Of course, this is assuming a relatively simple app - think task list or something similar.) Or, am I trying to simplify too much?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用的一般策略是尽可能多地使用 XML。这是一种与其他一些 UI 开发系统非常不同的思维方式,但一旦你克服了学习曲线,它就会非常酷。
我不知道你选择“不使用任何视图”是什么意思。每个 UI 组件都是某种类型的视图。如果您的意思是不使用任何自定义 View 子类,那么是的,这绝对是可能的。创建您自己的自定义视图类(然后在 XML 中使用它们!)的唯一原因是当库存小部件和视图不能执行您想要的操作时。由于它们非常灵活,因此这往往相当不常见(直到您开始进入奇特的行为或需要自定义图形行为)。
The general strategy I use is to push as much as possible into XML. It's a very different way of thinking from some other UI development systems, but it's very cool once you get past the learning curve.
I don't know what you mean by choosing "not to use any Views". Every UI component is a View of some sort. If you mean not using any custom View subclasses, then yes, it is definitely possible. The only reason to create your own custom View classes (and then use them in XML!) is when the stock widgets and views don't do what you want. Since they are quite flexible, this tends to be fairly uncommon (until you start getting into fancy behavior or need custom graphics behavior).
为 Android 应用程序创建 UI 有两种方法。它们是
使用 XML - 您可以使用 xml 来设计旨在支持多设备的 UI。 XML 还可以帮助您创建静态组件。
Java 代码 - 一般来说,用 java 创建 UI 并不是一个好的做法。如果您创建小型应用程序,它很适合。当您想要使用动态组件开发应用程序时,它也很有用。如果您想在 UI 中创建动态组件,Java 代码可以帮助您实现这一目标。
好的方法是通过 XML 创建 UI,除非 UI 中不需要动态组件。如果您需要动态 UI 创建,那么您可以进行自定义 UI 创建,即使用 Java 代码。
由于您是 Android 新手,我希望您参考 Android 开发者网站
There are two ways for Creating UI for Android Application. They are
Using XML - You can use xml for designing UI targeted for supporting Multiple device. Also XML helps you to create Static components.
Java Code -Generally it's not a good practice to creating UI in java. Its suitable, if you creating a samll application. Its also useful when you want to develop application with dynamic components. If you want to create Dynamic Components in UI, Java code helps you to achieve this.
The Good Approach is to create UI via XML, unless there's no dynamic component needed in the UI. if you need dynamic UI creation then you go custom UI creation i,e., Using Java Code.
Since you are New to Android, i would like you to refer android developer site
我认为您误解了,XML 布局只是创建视图的快捷方式。无论哪种方式,您最终都会在运行时得到相同的结果。混合、匹配、使用其中之一,这取决于你。
I think you misunderstand, XML layouts are just a shortcut for creating views. You end up with the same results at runtime either way. Mix, match, use one or the other, it's up to you.