动态VS在Android的XML布局? [英] Dynamic vs XML layout in Android?

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

问题描述

我是新的Andr​​oid开发,并已开始建立自己的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已经有文件的资源 - >布局 - > main.xml中的。所以,你可以创建用户界面的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>

所以我的问题是我应该使用哪一个?有什么建议,什么是利弊/动态利弊VS在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 /布局/ )允许您定义多个用户界面在不同的情况下使用,以在需要时,系统会自动选择是正确的。 Java中的等价物会为一个讨厌的一套如果开关语句。

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),林特将有助于指出缺点在你的布局,因为它会指出中唯一的一个子集等价的Java code。

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.

四,所有样品code约98%,你会发现将使用布局XML文件和所有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.

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

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