Android的用户界面可以通过code *和* XML产生的呢?可以通过、用户界面、Android、code

2023-09-05 04:06:16 作者:非你不可

我无法掌握一定的概念,在Android的UI设计。这本书我指的是第一个使用通常的技术,Java程序员用它来创建用户界面,那就是创建容器和UI组件添加到他们,并在必要时其嵌套。

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.

现在,这本书介绍了使用XML文件,其中创建整个UI的一个新概念。在code粘贴如下:

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

所以基本上,我可以使用任何他们?

推荐答案

简单的回答,是的,你可以使用任何一种方法。不过,也有一些限制每个这样的,因为有布局属性,必须在XML中,如果你想使用它们进行设置。我想不出任何东西都是副手,但是我可以看看他们。

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.

在大多数情况下,创造了布局简单得多做XML,但你必须设置意见的选项布局在Java中,如果你需要,如创建数目不详的按钮根据一些用户可定义的变量。

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.

当您在XML创建 UI 膨胀在你的Java code。这通常做的onCreate()

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

正如你在例子中看到。但它也可以用充气的完成。

要记住的事情是吹你的布局,使用这两种方法,试图初始化任何之前的观点布局或你会得到一个 NPE 试图调用一个视图的方法时充气布局它包含在之前定义的。

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.

有一个正确的方法

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