如何向我的 Android 布局添加一些内容?

发布于 2024-12-23 11:13:45 字数 431 浏览 2 评论 0原文

我是 Android 的新手,并且看过显示通过 XML 和 Java 添加小部件的教程。我想知道行业专家更喜欢什么以及为什么。作者/叙述者似乎更喜欢 XML,说它提供了更多控制,但我想从退伍军人那里得到反馈。

  1. 带有 XML 教程

  2. 没有 XML 教程< /p>

谢谢你,我再次道歉一个完整的新手问题。

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.

  1. With XML Tutorial

  2. Without XML Tutorial

Thank You and again I apologize for a complete newb question.

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

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

发布评论

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

评论(3

血之狂魔 2024-12-30 11:13:45

与 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

︶ ̄淡然 2024-12-30 11:13:45

在 XML 中声明 UI 元素(大多数
常见且首选)

Android 提供了简单的 XML 词汇表
对应于 View 类和子类,
例如那些称为小部件的 UI 控件
(TextView、Button 等)和布局。

实例化布局元素
运行时(以 Java 代码编程)

您的应用程序可以创建 View 和 ViewGroup
对象(并操纵它们的属性)
以编程方式(在 Java 代码中)。

优点: 在 XML 中声明 UI

将表示与代码分离
控制其行为

您可以修改 UI,而无需修改您的
源码并重新编译
例如,您可以为以下内容创建 XML 布局
不同的屏幕方向,不同的设备屏幕
大小和不同的语言

更容易可视化 UI 的结构
(无需编写任何代码)

更容易设计/调试用户界面
可视化工具(如 Eclipse IDE 中的工具)

优点: 在运行时实例化布局元素

同时显示动态数据

当您的应用程序需要动态显示一些信息(例如从网络加载标题)时,您需要为每个标题提供尽可能多的文本视图,并且您在设计时对此不确定,此时可以首选此方法

您可以使您的应用程序设计两种方式两者都会对您有相同的帮助,这取决于您的要求是否需要在运行时创建它,无论如何,我认为虽然您可以制作单独的 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

You can modify UI without having to modify your
source code and recompile
For example, you can create XML layouts for
different screen orientations, different device screen
sizes, and different languages

Easier to visualize the structure of your UI
(without writing any code)

Easier to design/debug UI
Visualizer tool (like the one in Eclipse IDE)

Advantages of : Instantiate layout elements at runtime

While showing dynamic data

When your application need to show dynamically some information for example loading title from web then you need as many text-view per title and you are not sure at design time about this at that time this can be preffered

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.

奢望 2024-12-30 11:13:45

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.

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