java Android 中的动态与 XML 布局?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11960501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamic vs XML layout in Android?
提问by David Kroukamp
I'm new to Android development and have started creating my own UI. I see that you can either create it dynamically something like this (Dynamic Layouts):
我是 Android 开发的新手,已经开始创建自己的 UI。我看到您可以像这样动态创建它(动态布局):
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
TextView tv = new TextView(this);
tv.setText("Name");
ll.addView(tv);
EditText et = new EditText(this);
ll.addView(et);
Button b = new Button(this);
b.setText("Ok");
ll.addView(b);
}
but I also see that netbeans has a file Resources->layout->main.xml. So you can create an XML layout for the UI (Declaring XML layout):
但我也看到 netbeans 有一个文件Resources->layout->main.xml。所以你可以为 UI 创建一个 XML 布局(声明 XML 布局):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, AndroidTest"
/>
</LinearLayout>
So my question is which should I use? what is recommended and what are the pros/cons of dynamic vs XML layouts in Android development?
所以我的问题是我应该使用哪个?什么是推荐的,动态与 XML 布局在 Android 开发中的优缺点是什么?
回答by CommonsWare
Use layout XML resource files.
使用布局 XML 资源文件。
First, the resource sets (e.g., res/layout-land/
in addition to res/layout/
) allow you to define multiple UIs to be used in different circumstances, with the system automatically choosing the right one as needed. The equivalent in Java would be one nasty set of if
or switch
statements.
首先,资源集(例如,res/layout-land/
除了res/layout/
)允许您定义要在不同情况下使用的多个 UI,系统会根据需要自动选择正确的 UI。Java 中的等价物将是一组令人讨厌的if
orswitch
语句。
Second, there are tools that can help you create those layout resources successfully. Even if the drag-and-drop GUI building of Eclipse isn't your cup of tea (e.g., you're using NetBeans), Lint will help point out flaws in your layouts, for which it will point out only a subset in the equivalent Java code.
其次,有一些工具可以帮助您成功创建这些布局资源。即使 Eclipse 的拖放式 GUI 构建不是您的菜(例如,您使用的是 NetBeans),Lint 也会帮助指出您的布局中的缺陷,它只会指出布局中的一个子集等效的 Java 代码。
Third, it tends to be more terse, so if you're typing this stuff by hand, the XML will be less typing.
第三,它往往更简洁,因此如果您手动输入这些内容,XML 将减少输入。
Fourth, approximately 98% of all sample code you will find will use layout XML files, and approximately 98% of all UI answers you find here on StackOverflow (and other support resources) will assume that you use layout XML files. While you are free to avoid XML -- perhaps you were attacked by angle brackets as a young child or something -- you will be swimming upstream, fighting the current compared to what most Android developers do.
第四,您将找到的所有示例代码中大约 98% 将使用布局 XML 文件,并且您在 StackOverflow(和其他支持资源)上找到的所有 UI 答案中大约 98% 将假定您使用布局 XML 文件。虽然您可以自由地避免使用 XML —— 或许您在年幼时曾受到尖括号的攻击或其他什么 —— 与大多数 Android 开发人员所做的相比,您将逆流而上,与潮流作斗争。
回答by Entreco
I would recommend using xml layouts for most parts of your projects. You can put the layouts in different folders such as:
我建议您对项目的大多数部分使用 xml 布局。您可以将布局放在不同的文件夹中,例如:
layout-land -> for landscape
layout-land -> 用于景观
layout-port -> for portrait
布局端口 -> 用于纵向
layout-v15 -> for android version >= 15
layout-v15 -> 安卓版本 >= 15
layout-sw600dp -> for screens with a certain width
layout-sw600dp -> 用于具有一定宽度的屏幕
Using these resource classifiers you can quickly have a wide variety in your layouts to support the wide range of android devices, without have to code a lot extra.
使用这些资源分类器,您可以快速地在布局中拥有各种各样的布局,以支持各种 android 设备,而无需编写大量额外代码。
In my oppinion, that's the biggest advantage of using xml resources vs creating all layouts dynamically. For more info see this link about resources
在我看来,这是使用 xml 资源与动态创建所有布局的最大优势。有关更多信息,请参阅有关资源的此链接
回答by bkowalikpl
Layout based on XML depends on casting, because you have to use:
基于 XML 的布局取决于强制转换,因为您必须使用:
Button myButton = (Button) findViewById(R.id.my_button);
so you have one casting and time consuming XML searching. When you use dynamic creation of a UI is much more difficult to manage it. You have to organize everything but you don't need to cast as much.
所以你有一个转换和耗时的 XML 搜索。当您使用动态创建 UI 时,管理它要困难得多。你必须组织一切,但你不需要投那么多。
I prefer the third solution - RoboGuiceIt uses dependency injection pattern and you don't care about casting and it's faster to create an app. Also more flexible. Consider that you have a normal text field. Then you want to change it to text area. In RoboGuice it's only matter of 2 changes (except usage changes). It's also faster when you use Context (Context is very memory consuming object and storing reference to it is sign of bad coding).
我更喜欢第三个解决方案 - RoboGuice它使用依赖注入模式,你不关心转换,而且创建应用程序更快。也更灵活。假设您有一个普通的文本字段。然后您想将其更改为文本区域。在 RoboGuice 中,只有 2 个更改(使用更改除外)。当您使用 Context 时它也更快(Context 是非常消耗内存的对象并且存储对它的引用是错误编码的标志)。
So my advice is: use XML because of it's simplicity to manage UI. Use RoboGuiceto code fast.
所以我的建议是:使用 XML,因为它管理 UI 很简单。使用RoboGuice快速编码。
回答by Nikola Ninkovic
Well, I declare mu UI in xml (99% of time), becouse it's easier for me to work that way.
好吧,我在 xml 中声明了 mu UI(99% 的时间),因为这样工作对我来说更容易。
It's good practice to separate UI and Code logic, XML is easier for describing UI (consider using this old designeror switch to Eclipse IDE and it's designer)
将 UI 和代码逻辑分开是很好的做法,XML 更容易描述 UI(考虑使用这个旧设计器或切换到 Eclipse IDE 和它的设计器)
Also, one android app can have multiple designs (for phones and tablets) and it will be painfull to hand-code that in Java
此外,一个 android 应用程序可以有多种设计(适用于手机和平板电脑),用 Java 手动编写代码会很痛苦
Simply, use XML
简单地说,使用 XML
回答by Zachary Moshansky
I prefer XML for most things as it allows you to separate content from code and it tends to be cleaner to edit especially when the project gets big and you forget what you wrote a while ago. Using XML and styles will allow you to get a consistent look and feel throughout the app as well. It also allows the layout system to take care of the heavy lifting and you won't end up with views that have a reference to your activity's context. (Which is bad, when you rotate and your activity is recreated). Overall it tends to be a quicker option and if you can do it in XML than I like to. Most of the android archtiecture/textbooks seem to follow this setup as well. (See Android Dev Docs, Mark Murphy of CommonsWare)
对于大多数事情,我更喜欢 XML,因为它允许您将内容与代码分开,并且编辑起来往往更清晰,尤其是当项目变大并且您忘记了之前写的内容时。使用 XML 和样式还可以让您在整个应用程序中获得一致的外观和感觉。它还允许布局系统处理繁重的工作,并且您最终不会得到引用您的活动上下文的视图。(这很糟糕,当您旋转并重新创建您的活动时)。总的来说,它往往是一个更快的选择,如果你能在 XML 中做到这一点,那么它比我喜欢的要快。大多数 android 架构/教科书似乎也遵循此设置。(参见 Android 开发文档,CommonsWare 的 Mark Murphy)
Dynamic has the upside that it is able to customize some things in more advanced ways and can fix some layout issues fairly easily. There are some upsides other than that but I prefer to do everything in XML first and if it can't be done or is tough then do it in code. Some things can be done in XML far easier than in code as well.
Dynamic 的优点是它能够以更高级的方式自定义一些东西,并且可以相当容易地修复一些布局问题。除此之外还有一些好处,但我更喜欢先在 XML 中做所有事情,如果它不能完成或很难完成,那么在代码中完成。有些事情在 XML 中比在代码中更容易完成。
回答by Luke Taylor
It's completely your own choice. You'll probably find most Android Developers use XML to code their layouts of their app, since this was specifically designed for this. Yet I personally come from a game development background and don't really require Android's provided UI widgets, so when ever I do use a UI widget, I simply code it in java.
这完全是你自己的选择。您可能会发现大多数 Android 开发人员使用 XML 对其应用程序的布局进行编码,因为这是专门为此设计的。然而,我个人来自游戏开发背景,并不真正需要 Android 提供的 UI 小部件,所以当我使用 UI 小部件时,我只是用 java 编写代码。
I hope this helps.
我希望这有帮助。
回答by jelic98
This is a quite late response but someone could find it helpful. I'm currently making a librarythat lets you use dynamic layouts declared on the server. You can find it here: https://github.com/jelic98/dynamico
这是一个很晚的回复,但有人会发现它很有帮助。我目前正在制作一个库,让您可以使用在服务器上声明的动态布局。你可以在这里找到它:https: //github.com/jelic98/dynamico