Android UI can be created by code *AND* XML?

亡梦爱人 提交于 2020-02-05 06:49:35

问题


I am having trouble grasping a certain concept in Android UI design. The book I am referring to first uses the usual technique that Java programmers use to create UIs and that is to to create containers and add UI components to them and nest them as necessary.

Now, the book introduces a new concept where the entire UI was created using an XML file. The code is pasted below:

package com.oreilly.android.intro;
import android.app.Activity;
import android.os.Bundle;
/**
* Android UI demo program
*/
public class AndroidDemo extends Activity {
  private LinearLayout root;
  @Override public void onCreate(Bundle state) {
   super.onCreate(state);
   setContentView(R.layout.main);
   root = (LinearLayout) findViewById(R.id.root);
 }
}  

so basically, I can use any of them ?


回答1:


Simple answer,yes, you can use either approach. However, there are some limitations to each such as there are layout properties that must be set in xml if you want to use them. I can't think of what any are off-hand but I can look them up.

For the most part, creating the layouts is much simpler to do in xml but you do have the option of setting Views and layouts in Java if you need to such as creating an unknown number of Buttons depending on some user-defined variable.

When you create your UI in xml then you inflate it in your Java code. This is normally done in onCreate() using

setContentView(R.layout.main);

as you see in your example. But it can also be done with an inflater.

The thing to remember here is to inflate your layout, using either method, before trying to initialize any views in the layout or you will get a NPE when trying to call a method on a View defined before inflating the layout it is contained in.

A correct way

**Examples of inflating views/layouts correctly**
    Button mBtn;
    public class AndroidDemo extends Activity {
  private LinearLayout root;
  @Override public void onCreate(Bundle state) {
   super.onCreate(state);
   setContentView(R.layout.main);
   root = (LinearLayout) findViewById(R.id.root);

   btn = (Button) findViewById(R.id.buttonId); // Button is initialized after inflating the layout
 }
} 

Incorrect way

    public class AndroidDemo extends Activity {
  private LinearLayout root;
  @Override public void onCreate(Bundle state) {
   super.onCreate(state);
   Button mBtn = (Button) findViewById(R.id.buttonId);  // Button is initialized before inflating layout which will return null
   setContentView(R.layout.main);
   root = (LinearLayout) findViewById(R.id.root);
}
}   

I added the above example because I have seen a lot of people make that mistake. So don't do it...you've been warned! :)




回答2:


Not entirely sure what you're asking, but the two are interchangeable. Most of the time your UI will be done via xml. In some cases though, the ui is heavily dependent of the data, so you may need to dynamically generate it.

It basically comes down to whichever is easiest for you at the time.




回答3:


Yes. But is preferable to use xml, it is more powerful, easier and will separate layout from your code. Take a look at the docs: http://developer.android.com/guide/topics/ui/declaring-layout.html



来源:https://stackoverflow.com/questions/17452181/android-ui-can-be-created-by-code-and-xml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!