Dynamic vs XML layout in Android?

2019-01-18 11:03发布

问题:

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

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>

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?

回答1:

Use layout XML resource files.

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.

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.

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

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.



回答2:

I would recommend using xml layouts for most parts of your projects. You can put the layouts in different folders such as:

layout-land -> for landscape

layout-port -> for portrait

layout-v15 -> for android version >= 15

layout-sw600dp -> for screens with a certain width

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.

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



回答3:

Layout based on XML depends on casting, because you have to use:

 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.

I prefer the third solution - RoboGuice It 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).

So my advice is: use XML because of it's simplicity to manage UI. Use RoboGuice to code fast.



回答4:

Well, I declare mu UI in xml (99% of time), becouse it's easier for me to work that way.

It's good practice to separate UI and Code logic, XML is easier for describing UI (consider using this old designer or switch to Eclipse IDE and it's designer)

Also, one android app can have multiple designs (for phones and tablets) and it will be painfull to hand-code that in Java

Simply, use XML



回答5:

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)

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.



回答6:

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.

I hope this helps.



回答7:

This is a quite late response but someone could find it helpful. I'm currently making a library that lets you use dynamic layouts declared on the server. You can find it here: https://github.com/jelic98/dynamico