`

【翻译】(36)布局资源

 
阅读更多

【翻译】(36)布局资源

 

see

http://developer.android.com/guide/topics/resources/layout-resource.html

 

原文见

http://developer.android.com/guide/topics/resources/layout-resource.html

 

-------------------------------

 

Layout Resource

 

布局资源

 

-------------------------------

 

See also

 

另见

 

XML Layouts XML布局

 

-------------------------------

 

A layout resource defines the architecture for the UI in an Activity or a component of a UI.

 

一个布局资源为一个Activity中的用户界面或用户界面的一个组件定义结构。

 

* file location:

 

* 文件位置:

 

res/layout/filename.xml

 

res/layout/<文件名>.xml

 

The filename will be used as the resource ID.

 

文件名将被用作资源ID。

 

* compiled resource datatype:

 

* 被编译的资源数据类型:

 

Resource pointer to a View (or subclass) resource.

 

指向一个View(或子类)资源的资源指针。

 

* resource reference:

 

* 资源引用:

 

In Java: R.layout.filename

 

在Java中: R.layout.<文件名>

 

In XML: @[package:]layout/filename

 

在XML中:@[<包名>:]layout/<文件名>

 

* syntax:

 

* 语法:

 

-------------------------------

 

<?xml version="1.0" encoding="utf-8"?>

<ViewGroup xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@[+][package:]id/resource_name"

    android:layout_height=["dimension" | "fill_parent" | "wrap_content"]

    android:layout_width=["dimension" | "fill_parent" | "wrap_content"]

    [ViewGroup-specific attributes] >

    <View

        android:id="@[+][package:]id/resource_name"

        android:layout_height=["dimension" | "fill_parent" | "wrap_content"]

        android:layout_width=["dimension" | "fill_parent" | "wrap_content"]

        [View-specific attributes] >

        <requestFocus/>

    </View>

    <ViewGroup >

        <View />

    </ViewGroup>

    <include layout="@layout/layout_resource"/>

</ViewGroup>

 

-------------------------------

 

-------------------------------

 

Note: The root element can be either a ViewGroup, a View, or a <merge> element, but there must be only one root element and it must contain the xmlns:android attribute with the android namespace as shown.

 

注意:根元素可以是一个ViewGroup,一个View,或一个<merge>元素,但必须仅有一个根元素,并且它必须包含带有android名字空间的xmlns:android元素,如同所示那样。

 

-------------------------------

 

* elements:

 

* 元素:

 

* <ViewGroup>

 

A container for other View elements. There are many different kinds of ViewGroup objects and each one lets you specify the layout of the child elements in different ways. Different kinds of ViewGroup objects include LinearLayout, RelativeLayout, and FrameLayout.

 

一个用于其它View元素的容器。有许多不同类型的ViewGroup对象,而每一种让你以不同的方式指定子元素的布局。不同类型的ViewGroup对象包括LinearLayout,RelativeLayout,以及FrameLayout。

 

You should not assume that any derivation of ViewGroup will accept nested Views. Some ViewGroups are implementations of the AdapterView class, which determines its children only from an Adapter.

 

你不应该假设ViewGroup的任意派生类将接受内嵌的View。一些ViewGroups是AdapterView类的实现,它仅从一个Adapter中决定它的子对象。

 

* attributes:

 

* 属性:

 

* android:id

 

Resource ID. A unique resource name for the element, which you can use to obtain a reference to the ViewGroup from your application. See more about the value for android:id below.

 

资源ID。元素的唯一资源名称,你可以使用它从你的应用程序中取得指向ViewGroup的引用。见下面更多关于android:id的值的信息。

 

* android:layout_height

 

Dimension or keyword. Required. The height for the group, as a dimension value (or dimension resource) or a keyword ("fill_parent" or "wrap_content"). See the valid values below.

 

尺寸或关键词。必需的。组的高度,作为一个尺寸值(或尺寸资源)或一个关键词("fill_parent"或"wrap_content")。参见下面的可用值。

 

* android:layout_width

 

Dimension or keyword. Required. The width for the group, as a dimension value (or dimension resource) or a keyword ("fill_parent" or "wrap_content"). See the valid values below.

 

尺寸或关键词。必需的。组的宽度,作为一个尺寸值(或尺寸资源)或一个关键词("fill_parent"或"wrap_content")。参见下面的可用值。

 

More attributes are supported by the ViewGroup base class, and many more are supported by each implementation of ViewGroup. For a reference of all available attributes, see the corresponding reference documentation for the ViewGroup class (for example, the LinearLayout XML attributes).

 

更多属性被ViewGroup基类支持,并且许多更多的属性被ViewGroup的每个实现支持。想获取所有可用属性的参考,请参见相应的ViewGroup类的参考文档(例如,LinearLayout的XML属性)。

 

* <View>

 

An individual UI component, generally referred to as a "widget". Different kinds of View objects include TextView, Button, and CheckBox.

 

一个单独的用户界面组件,通常被引用作为一个“部件”。不同类型的View对象包括TextView,Button,以及CheckBox。

 

* attributes:

 

* 属性:

 

* android:id

 

Resource ID. A unique resource name for the element, which you can use to obtain a reference to the View from your application. See more about the value for android:id below.

 

资源ID。元素的唯一资源名称,你可以从你的应用程序中使用它取得指向该View的引用。关于android:id的值的更多信息见下。

 

* android:layout_height

 

Dimension or keyword. Required. The height for the element, as a dimension value (or dimension resource) or a keyword ("fill_parent" or "wrap_content"). See the valid values below.

 

尺寸或关键词。必需的。元素的高度,作为一个尺寸值(或尺寸资源)或一个关键词("fill_parent"或"wrap_content")。可用值见下。

 

* android:layout_width

 

Dimension or keyword. Required. The width for the element, as a dimension value (or dimension resource) or a keyword ("fill_parent" or "wrap_content"). See the valid values below.

 

尺寸或关键词。必需的。元素的宽度,作为一个尺寸值(或尺寸资源)或一个关键词("fill_parent"或"wrap_content")。可用值见下。

 

More attributes are supported by the View base class, and many more are supported by each implementation of View. Read XML Layouts for more information. For a reference of all available attributes, see the corresponding reference documentation (for example, the TextView XML attributes).

 

更多属性被View基类支持,而且许多更多的属性被View的各种实现支持。阅读XML布局以获取更多信息。对于所有可用属性的参考文档,参见相应的参考文档(例如,TextView的XML属性)。

 

* <requestFocus>

 

Any element representing a View object can include this empty element, which gives it's parent initial focus on the screen. You can have only one of these elements per file.

 

任意代表一个View对象的元素可以包含这个空元素,它给它的父元素屏幕的最初焦点。每个文件你只可以有一个这样的元素。

 

* <include>

 

Includes a layout file into this layout.

 

包含一个布局文件进这个布局。

 

* attributes:

 

* 属性:

 

* layout

 

Layout resource. Required. Reference to a layout resource.

 

布局资源。必需的。指向一个布局资源的引用。

 

* android:id

 

Resource ID. Overrides the ID given to the root view in the included layout.

 

资源ID。覆盖在被包含的布局中赋给根视图的ID。

 

* android:layout_height

 

Dimension or keyword. Overrides the height given to the root view in the included layout. Only effective if android:layout_width is also declared.

 

尺寸或关键词。覆盖在被包含布局中被赋给根视图的高度。仅当android:layout_width也被声明时才有效。

 

* android:layout_width

 

Dimension or keyword. Overrides the width given to the root view in the included layout. Only effective if android:layout_height is also declared.

 

尺寸或关键词。覆盖在被包含布局中被赋给根视图的宽度。仅当android:layout_height也被声明时才有效。

 

You can include any other layout attributes in the <include> that are supported by the root element in the included layout and they will override those defined in the root element.

 

你可以在<include>中包含其它任意在被包含的布局中的根元素支持的布局属性,而它们将覆盖在根元素中定义的那些属性。

 

-------------------------------

 

Caution: If you want to override the layout dimensions, you must override both android:layout_height and android:layout_width—you cannot override only the height or only the width. If you override only one, it will not take effect. (Other layout properties, such as weight, are still inherited from the source layout.)

 

警告:如果你希望覆盖布局尺寸,你必须同时覆盖android:layout_height和android:layout_width——你不能只覆盖高度或只覆盖宽度。如果你只覆盖一个,它将没有效果。(其它布局属性,诸如权重,仍然从源布局中继承。)

 

-------------------------------

 

Another way to include a layout is to use ViewStub. It is a lightweight View that consumes no layout space until you explicitly inflate it, at which point, it includes a layout file defined by its android:layout attribute. For more information about using ViewStub, read Layout Tricks: ViewStubs.

 

另一种包含一个布局的方式是使用ViewStub。它是一个轻量级View,它不消耗布局空间,直至你显式地解压它,那时候,它包含它的android:layout属性定义的布局文件。关于使用ViewStub的更多信息,请阅读《布局技巧:ViewStub》

 

* <merge>

 

An alternative root element that is not drawn in the layout hierarchy. Using this as the root element is useful when you know that this layout will be placed into a layout that already contains the appropriate parent View to contain the children of the <merge> element. This is particularly useful when you plan to include this layout in another layout file using <include> and this layout doesn't require a different ViewGroup container. For more information about merging layouts, read Layout Tricks: Merging.

 

一个可选的根元素,它不在布局层级中被绘画。当你知道这个布局将被放置进一个已经包含合适父View的布局以容纳<merge>元素的子元素时,使用它作为根元素是有用的。它特别有用,当你计划使用<include>包含这个布局在另一个布局文件中,而这个布局不需要一个不同的ViewGroup容器时。想获取关于合并布局的更多信息,请阅读《布局技巧:合并》。

 

Value for android:id

 

用于android:id的值

 

For the ID value, you should usually use this syntax form: "@+id/name". The plus symbol, +, indicates that this is a new resource ID and the aapt tool will create a new resource integer in the R.java class, if it doesn't already exist. For example:

 

对于ID的值,你通常应该使用这个语法形式:"@+id/<名称>"。加号,"+",指示这是一个新的资源ID,而aapt工具将在R.java类中创建一个新的资源整型,如果它还不曾存在。例如:

 

-------------------------------

 

<TextView android:id="@+id/nameTextbox"/>

 

-------------------------------

 

The nameTextbox name is now a resource ID attached to this element. You can then refer to the TextView to which the ID is associated in Java:

 

nameTextbox的名称现在是一个依附到这个元素的资源ID。然后你可以引用该ID在Java中关联的TextView:

 

-------------------------------

 

findViewById(R.id.nameTextbox);

 

-------------------------------

 

This code returns the TextView object.

 

这段代码返回TextView对象。

 

However, if you have already defined an ID resource (and it is not already used), then you can apply that ID to a View element by excluding the plus symbol in the android:id value.

 

然而,如果你已经定义一个ID资源(并且它还未使用),那么你可以通过在android:id值中不包括加号来应用那个ID到一个View元素。

 

Value for android:layout_height and android:layout_width:

 

用于android:layout_height和android:layout_width的值:

 

The height and width value can be expressed using any of the dimension units supported by Android (px, dp, sp, pt, in, mm) or with the following keywords:

 

高度和宽度值可以使用Android支持的任意尺寸单位来表达(px,dp,sp,pt,in,mm)或使用以下关键词:(注:猜测这些单位的含义——px是指pixel像素,dp是指density-independent pixel密度无关像素,sp是指scale-independent pixels伸缩无关像素,pt是指point点,in是指inch英寸,mm是指millimeter毫米。详见http://stackoverflow.com/questions/2025282/difference-of-px-dp-dip-and-sp-in-android)

 

-------------------------------

 

* Value Description

 

* 值 描述

 

* match_parent Sets the dimension to match that of the parent element. Added in API Level 8 to deprecate fill_parent.

 

* match_parent 设置尺寸以匹配父元素的尺寸。在API级别8中新增以废弃fill_parent。

 

* fill_parent Sets the dimension to match that of the parent element.

 

* fill_parent 设置尺寸以匹配父元素的尺寸。

 

* wrap_content Sets the dimension only to the size required to fit the content of this element.

 

* wrap_content 设置尺寸仅为适应这个元素的内容所需的大小。

 

-------------------------------

 

Custom View elements

 

自定义视图元素

 

You can create your own custom View and ViewGroup elements and apply them to your layout the same as a standard layout element. You can also specify the attributes supported in the XML element. To learn more, see the Custom Components developer guide.

 

你可以创建你自己的自定义View和ViewGroup元素并且应用它们到你的布局,和一个标准布局元素相同。你还可以指定在XML元素中支持的属性。要学习更多,参见自定义组件开发者指引。

 

* example:

 

* 示例:

 

XML file saved at res/layout/main_activity.xml:

 

保存为res/layout/main_activity.xml的XML文件:

 

-------------------------------

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

              android:layout_width="fill_parent" 

              android:layout_height="fill_parent" 

              android:orientation="vertical" >

    <TextView android:id="@+id/text"

              android:layout_width="wrap_content"

              android:layout_height="wrap_content"

              android:text="Hello, I am a TextView" />

    <Button android:id="@+id/button"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Hello, I am a Button" />

</LinearLayout>

 

-------------------------------

 

This application code will load the layout for an Activity, in the onCreate() method:

 

这段应用程序代码将在onCreate()方法中为一个Activity加载该布局:

 

-------------------------------

 

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView.(R.layout.main_activity);

}

 

-------------------------------

 

* see also:

 

* 另见:

 

* XML Layouts XML布局

* View 视图

* ViewGroup 视图组

 

Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.

 

除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。

 

Android 4.0 r1 - 13 Jan 2012 0:46

 

-------------------------------

 

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

 

(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证描述的条款进行修改)

 

(本人翻译质量欠佳,请以官方最新内容为准,或者参考其它翻译版本:

* ソフトウェア技術ドキュメントを勝手に翻訳

http://www.techdoctranslator.com/android

* Ley's Blog

http://leybreeze.com/blog/

* 农民伯伯

http://www.cnblogs.com/over140/

* Android中文翻译组

http://androidbox.sinaapp.com/


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics