Android 中的动态与 XML 布局? [英] Dynamic vs XML layout in Android?

查看:37
本文介绍了Android 中的动态与 XML 布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 开发新手,已经开始创建自己的 UI.我看到您可以像这样动态创建它(动态布局):

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):

@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);
}

但我也看到netbeans有一个文件Resources->layout->main.xml.所以你可以为 UI 创建一个 XML 布局(声明 XML 布局):>

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):

<?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>

所以我的问题是我应该使用哪个?Android 开发中动态布局与 XML 布局的推荐和优缺点是什么?

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 资源文件.

Use layout XML resource files.

首先,资源集(例如,res/layout-land/ 除了 res/layout/)允许您定义多个 UI 以用于不同的情况,系统会根据需要自动选择合适的.Java 中的等价物是一组令人讨厌的 ifswitch 语句.

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.

其次,有一些工具可以帮助您成功创建这些布局资源.即使 Eclipse 的拖放式 GUI 构建不是您的菜(例如,您使用的是 NetBeans),Lint 也会帮助指出您的布局中的缺陷,它只会指出布局中的一个子集等效的 Java 代码.

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.

第三,它往往更简洁,因此如果您手动输入这些内容,XML 的输入会更少.

Third, it tends to be more terse, so if you're typing this stuff by hand, the XML will be less typing.

第四,您将找到的所有示例代码中大约 98% 将使用布局 XML 文件,您在 StackOverflow(和其他支持资源)上找到的所有 UI 答案中大约 98% 将假定您使用布局 XML 文件.虽然您可以自由地避免使用 XML——也许您在年幼时曾受到尖括号的攻击或其他什么——但与大多数 Android 开发人员所做的相比,您将逆流而上,与潮流作斗争.

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.

这篇关于Android 中的动态与 XML 布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆