如何向我的 Android 布局添加一些内容?
我是 Android 的新手,并且看过显示通过 XML 和 Java 添加小部件的教程。我想知道行业专家更喜欢什么以及为什么。作者/叙述者似乎更喜欢 XML,说它提供了更多控制,但我想从退伍军人那里得到反馈。
谢谢你,我再次道歉一个完整的新手问题。
I am a complete newb to Android and have seen tutorials that show widgets being added via XML and also via Java. I want to know what industry experts prefer and why. The author/narrator seems to prefer XML saying it gives more control but I wanted to get feedback from veterans.
Thank You and again I apologize for a complete newb question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与 Java 代码相比,XML 布局非常简单。编码也比java少很多。
您可以选择这两者中的任何一个,但所有作者都更喜欢 XML,因为它很简单。
请阅读此链接中提供的信息,您将会得到一个想法。
XML 布局
XML layouts are very easy compared to java code. The coding also very less than java.
You can prefer any of these two, but all authors prefer XML because its easy.
Please read information available at this link, you will get an idea.
XML Layouts
在 XML 中声明 UI 元素(大多数
常见且首选)
Android 提供了简单的 XML 词汇表
对应于 View 类和子类,
例如那些称为小部件的 UI 控件
(TextView、Button 等)和布局。
实例化布局元素
运行时(以 Java 代码编程)
您的应用程序可以创建 View 和 ViewGroup
对象(并操纵它们的属性)
以编程方式(在 Java 代码中)。
优点: 在 XML 中声明 UI
• 将表示与代码分离
控制其行为
• 更容易可视化 UI 的结构
(无需编写任何代码)
优点: 在运行时实例化布局元素
• 同时显示动态数据
您可以使您的应用程序设计两种方式两者都会对您有相同的帮助,这取决于您的要求是否需要在运行时创建它,无论如何,我认为虽然您可以制作单独的 XML 文件,但为什么需要在 Activity java 文件中执行此操作并获得更多复杂性不必要的代码。
Declare UI elements in XML (most
common and preferred)
Android provides a straightforward XML vocabulary
that corresponds to the View classes and subclasses,
such as those for UI controls called widgets
(TextView, Button, etc.) and layouts.
Instantiate layout elements at
runtime (programmatically in Java code)
Your application can create View and ViewGroup
objects (and manipulate their properties)
programmatically (in Java code).
Advantages of : Declaring UI in XML
• Separation of the presentation from the code
that controls its behavior
• Easier to visualize the structure of your UI
(without writing any code)
Advantages of : Instantiate layout elements at runtime
• While showing dynamic data
You can make your application design both ways and both will help you the same,It will depened on your requirement whether you require to create it at runtime or not,Anyways I think while you can make separate XML file why need to do it in your Activity java file and get more complexity in code unnecessarily.
XML布局方法非常简单、快速且易于可视化。您可以先进行纸笔工作,然后简单地在 XML 中进行。
XML 布局易于管理 - 您可以更改标题、位置、外观和布局。 XML 布局中的感觉、大小、颜色等,无需更改一行 Java 代码。该应用程序将从您的 XML 文件生成布局的 Java 代码。
XML 布局并不强制您编写整个 XML 代码。相反,您可以使用 GUI 编辑器并在屏幕上排列控件,它会为您生成 XML。有很多 GUI 设计工具。例如: http://www.droiddraw.org
现在,您可以使用 Java 代码 UI 布局仅当您在编写应用程序时无法确定布局时。假设您正在为 Web 设计师设计一个 Android 应用程序 - 一个用于创建 HTML 表单并创建 HTML 代码(一个简单的 HTML 编辑器工具)的应用程序,那么您无法知道用户将要执行哪种类型的布局。此时,更好的选择是选择动态布局(或Java代码布局)
几乎每个Android程序员都选择在XML中进行布局。这是最野兽也是最简单的方法。
XML layout method is very easy, fast and easy to visualize. You can do a paper-pencil work first and simply do it in XML.
XML layouts are easy to manage - you can change the caption, position, look & feel, size, colors etc. in the XML layout, without altering a single line of the Java Code. The App will generate the Java Code for the layout from your XML file.
XML layout does not compell that you should write the whole XML codes. Instead, you can use a GUI editor and you arrange the controls on screen, and it will generate the XML for you. There are lots of GUI design tools. Eg.: http://www.droiddraw.org
Now, you can go for Java Code UI layout only if you cannot determine the layout at the time of writing the App. Say, you are designing an Android App for Web Designers - An app to create HTML Forms and create the HTML code (a simple HTML editor tool), then you are unable to know which type of layout the user is going to do. At this point, the better option is choosing Dynamic Layout (or Java Code layout)
Almost every Android programmer chooses to do layout in XML. That's the beast and easiest way to do it.