Devel/ui/xml

From Android中文网

Android中文网(androidcn.net) 版权申明 : creativecommons licenses
Jump to: navigation, search

[编辑] 使用XML设计你的屏幕显示

Because designing a screen in code can be cumbersome, Android supports an XML syntax to design 通过代码来设计屏幕显示始终是一件麻烦的事情,Android通过支持XML文档设计屏幕显示来解决。

screens. Android defines a large number of custom elements, each representing a specific Android View Android定义了大量的自定义元素,各自代表了特定的Android显示子类。

subclass. You can design a screen the same way you create HTML files, as a series of nested tags, 你可以象创建HTML文档一样,通过保存在应用res/layout/目录下的XML文件中一系列的嵌套标签

saved in an XML file inside the application's res/layout/ directory. To learn what elements are 来设计你的屏幕显示。 对XML文档元素的作用和格式感兴趣的,请参考

exposed, and the format of the XML file, see Layout Resources. Each file describes a single Layout Resources[[1]]

android.view.View element, but this element can be either a simple visual element, or a layout 每个文档描述一个android.view.View[2]元素,但是这个元素既可以是

一个简单的显示元素,也可以是一个在子节点中包含了一个集合的版面设计

element that contains a collection of child objects (a screen or a portion of a screen). When Android 元素(屏幕或者屏幕的一部分)。 当Android

compiles your application, it compiles each file into an android.view.View resource that you can load 编译你的应用时,他将每个文件都编译进android系统。你可以在代码Activity.onCreate()实现中通过调用

in code by calling setContentView(R.layout.layout_file_name) in your Activity.onCreate() setContentView(R.layout.layout_file_name)方法加载显示资源

implementation.

Each XML file is made of tags that correspond to Android GUI classes. These tags have attributes that 每个XML文件通过与Android图形用户接口类相当的一系列标签组成。 这些标签与对应类中的方法特性

roughly correspond to methods in that class (for example, EditText has a text attribute that 大致相同(例如,EditText有一个text特性与EditText.setText相当)

corresponds to EditText.setText).

Note that there is not an exact correspondence between class and method names, and element and 注意:类和方法名称、元素和属性名称之间没有精确一致关系--

attribute names — they're close, but not always 1:1. 他们很接近,但是他们不是1对1的关系。

Also note that Android tends to draw elements in the order in which they appear in the XML. 同样注意:Android趋向于XML文件中元素的顺序来绘制

Therefore, if elements overlap, the last one in the XML file will probably be drawn on top of any 这样的话,如果元素交迭,XML文件中最后出现的元素可能会重叠在相同区域先出现并被绘制的元素之上。

previously listed elements in that same space.


Each XML file is compiled into a tree rooted by single View or ViewGroup object, and so must contain 每个XML文件被单独的显示(View)或者显示组合(ViewGroup)对象编译成一个树型,并且必须包含

a single root tag. In the following example, it evaluates to the outermost LinearLayout object. 一个单独的根节点。在下面的例子中,他就是在外面的LinearLayout对象的值。

Attributes named layout_something apply to that object's LayoutParams member. Layout Resources also 属性以提供给对象的LayoutParams成员版式内容来命名,Layout Resources[[http://code.google.com/android/reference/available-

resources.html#layoutresources]]

describes how to learn the syntax for specifying LayoutParams properties. 同样提供了怎么学习指定的LayoutParams LayoutParams属性结构。

The following values are supported for dimensions (described in TypedValue): 下面被支持的尺寸单位(在TypedValue[3]

  • px (pixels)像素
  • dip (device independent pixels)设备独立像素
  • sp (scaled pixels — best for text size)放大像素--对文本大小最好
  • pt (points)点
  • in (inches)英寸
  • mm (millimeters)毫米

Example: android:layout_width="25px" 例子:android:layout_width="25px"

For more information about these dimensions, see Dimension Values. 如果想得到更多的尺寸信息,请参考Dimension Values[4]

The following XML file creates the screen shown. Note that the text on the top of the screen was set 下面的XML文件创建了一个屏幕显示,注意:屏幕上面的文本通过调用Activity.setTitle来设置。

by calling Activity.setTitle. Note that the attributes that refer to relative elements (i.e., 注意:在有关元素中(例子:layout_toLeft)提到的属性

layout_toLeft) refer to the ID using the syntax of a relative resource (@id/id_number). 通过使用相关资源的ID句法(@id/id_number)来使用。

   <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android
               android:layout_width="fill_parent" 
               android:layout_height="wrap_content"
               android:background="@drawable/blue"
               android:padding="10px">
   <TextView id="@+id/label" 
             android:layout_width="fill_parent" 
             android:layout_height="wrap_content" 
             android:text="Type here:"/>
   <EditText id="@+id/entry" 
             android:layout_width="fill_parent" 
             android:layout_height="wrap_content" 
             android:background="@android:drawable/editbox_background"
             android:layout_below="@id/label"/>
   <Button id="@+id/ok" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:layout_below="@id/entry"
           android:layout_alignParentRight="true"
           android:layout_marginLeft="10px"
           android:text="OK" />
   <Button android:layout_width="wrap_content" 
           android:layout_height="wrap_content"
           android:layout_toLeft="@id/ok"
           android:layout_alignTop="@id/ok"
           android:text="Cancel" />
   </RelativeLayout>

Screen shot showing how this layout XML file is rendered.


[编辑] 加载XML资源

Loading the compiled layout resource is very easy, and done with a single call in the application's 加载编译过的版式资源非常的简单,只需要在应用中的onCreate()方法中调用一次相应方法就可以了,

onCreate() method, as shown here: 如下所示:

   protected void onCreate(Bundle savedValues)
   {
      // Be sure to call the super class.
      super.onCreate(savedValues);
   
      // Load the compiled layout resource into the window's
      // default ViewGroup.
      // The source file is res/layout/hello_activity.xml
      setContentView(R.layout.hello_activity);
 
      // Retrieve any important stored values.
      restoreValues(savedValues);
   }
Personal tools