Layout Resource

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

FILE LOCATION:
res/layout/filename.xml
The filename will be used as the resource ID.
COMPILED RESOURCE DATATYPE:
Resource pointer to a  View (or subclass) resource.
RESOURCE REFERENCE:
In Java:  R.layout.filename
In XML:  @[package:]layout/filename
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.

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  LinearLayoutRelativeLayout, and  FrameLayout.

You should not assume that any derivation of ViewGroup will accept nested Views. SomeViewGroups are implementations of the AdapterView class, which determines its children only from an 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 forandroid:id below.
android:layout_height
Dimension or keywordRequired. 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.
android:layout_width
Dimension or keywordRequired. 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.

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

<View>
An individual UI component, generally referred to as a "widget". Different kinds of  View objects include  TextViewButton, and  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:idbelow.
android:layout_height
Dimension or keywordRequired. 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.
android:layout_width
Dimension or keywordRequired. 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.

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

<requestFocus>
Any element representing a  View object can include this empty element, which gives its parent initial focus on the screen. You can have only one of these elements per file.
<include>
Includes a layout file into this layout.

attributes:

layout
Layout resourceRequired. Reference to a layout resource.
android:id
Resource ID. Overrides the ID given to the root view in the included layout.
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
Dimension or keyword. Overrides the width given to the root view in the included layout. Only effective if  android:layout_height is also declared.

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.

Caution: If you want to override layout attributes using the <include> tag, you must override both android:layout_height and android:layout_width in order for other layout attributes to take effect.

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 itsandroid:layout attribute. For more information about using ViewStub, read Loading Views On Demand.

<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  Re-using Layouts with <include/>.
Value for 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:

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

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

findViewById(R.id.nameTextbox);

This code returns the TextView object.

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.

Value for android:layout_height and 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:

Value Description
match_parent Sets the dimension to match that of the parent element. Added in API Level 8 to deprecate fill_parent.
fill_parent Sets the dimension to match that of the parent element.
wrap_content Sets the dimension only to the size required to fit the content of this element.
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.

EXAMPLE:
XML file saved at  res/layout/main_activity.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:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
}
SEE ALSO:

一个布局资源定义了一个活动的用户界面或用户界面的组件体系结构。

文件位置:
水库/布局/ 文件名 ​​的.xml
文件名 ​​将被用作资源ID。
编译的资源数据类型:
资源指针 视图(或子类)的资源。
资源引用:
在Java: 。R.layout 
在XML: @ [ :]布局/ 文件名
句法:
<?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 >

注:根元素可以是一个 ViewGroup中,一个视图,或<合并>元素,但只能有一个根元素,它必须包含的xmlns:android的属性与Android的 命名空间,如图所示。

内容:
<ViewGroup中>
一种其它容器 视图元素。有许多不同种类的 的ViewGroup对象和每一个,可以指定以不同的方式子元素的布局。不同种类  的ViewGroup对象包括 的LinearLayout,  RelativeLayout的,和 的FrameLayout

你不应该假定任何派生的ViewGroup 会接受嵌套视图秒。有些ViewGroup中 s为的的实现适配器视图类,它只能从决定其子女适配器

属性:

机器人:ID
资源ID。为元素,您可以用它来 ​​获得对一个参考一个独特的资源名称 的ViewGroup从您的应用程序。查看更多关于 为价值机器人:ID下面。
机器人:layout_height
尺寸或关键字必需。高度为组,作为一个尺寸值(或 维度资源)或关键字( “FILL_PARENT”“WRAP_CONTENT” )。见 有效值下方。
机器人:layout_width
尺寸或关键字必需。宽度为组,作为尺寸值(或 维资源)或关键字( “FILL_PARENT”“WRAP_CONTENT” )。见 有效值下方。

更多的属性由支持的ViewGroup 基类,多是由每个实现支持 的ViewGroup。对于所有可用的属性的一个参考,请参见相应的参考文档的ViewGroup类(例如,的LinearLayout XML属性)。

<查看>
单个UI组件,通常被称为“小部件”。不同的 视图对象包括 TextView的,  按钮复选框

属性:

机器人:ID
资源ID。为元素,您可以用它来 ​​获得对一个参考的唯一资源名称 查看你的应用程序。查看更多关于 为价值机器人:ID下面。
机器人:layout_height
尺寸或关键字必需。的高度为元素,作为尺寸值(或 维资源)或关键字( “FILL_PARENT”“WRAP_CONTENT” )。见 有效值下方。
机器人:layout_width
尺寸或关键字必需。宽度为元素,作为尺寸值(或 维资源)或关键字( “FILL_PARENT”“WRAP_CONTENT” )。见 有效值下方。

更多的属性由支持查看 基类,多是由每个实现支持 查看。阅读布局以获取更多信息。对于所有可用的属性的一个参考,参阅相应的参考文献(例如,TextView的XML属性)。

<requestFocus的>
代表任何元素 视图对象可以包括该空元素,这给其母公司最初的重点在屏幕上。你只能有每个文件这些元素中的一个。
<包括>
包括布局文件到这个布局。

属性:

布局
布局资源必需。参照布局的资源。
机器人:ID
资源ID。覆盖给予了包括布局的根视图的ID。
机器人:layout_height
尺寸或关键字。覆盖给予了包括布局的根视图的高度。只有有效的,如果 机器人:layout_width也声明。
机器人:layout_width
尺寸或关键字。覆盖给予了包括布局的根视图的宽度。只有有效的,如果 机器人:layout_height也声明。

您可以在任何其他布局属性的<包括>由根元素中包含布局的支持,他们将覆盖在根元素定义。

注意:如果你想使用覆盖布局属性<包括>标签,则必须同时覆盖 Android版 ​​本:layout_height机器人:layout_width为了让其他布局属性才能生效。

以包括一个布局的另一种方法是使用ViewStub。它是一个轻量级的观点,即不消耗布置空间,除非明确它充气,在这一点上,它包括由它定义的布局文件的Android:布局属性。有关使用的详细信息ViewStub,读取加载浏览点播

<合并>
未在布局层次结构绘制的替代的根元素。以此为根元素时,你知道该布局将被放置到已包含适当的父视图包含的儿童的布局是非常有用的  <合并>元素。当你计划使用包括此布局的另一个布局文件,这是特别有用的 <包括>这个布局不要求有不同 的ViewGroup容器。有关合并布局的更多信息,请阅读 重复使用布局与<包括/> 
对于值机器人:ID

对于ID值,你通常应该使用这种语法形式:“@ + ID / 名称 ”。加号,+,表示这是一个新的资源ID和AAPT工具将在创建新的资源整数R.java类,如果它不存在。例如:

<TextView的 机器人:ID = “@ + ID / nameTextbox” />

nameTextbox名称现在是连接到该元素的资源ID。然后,您可以参考的TextView到的ID是用Java有关:

findViewById ř ID nameTextbox );

此代码返回TextView的对象。

但是,如果您已经定义了一个ID资源(和尚未使用),那么你可以申请该ID来一个视图排除在加符号元素ID:Android的价值。

对于值layout_height:机器人和 机器人:layout_width

高度和宽度值可以使用任何的表示 标注单位 Android支持(PX,DP,SP,PT,在,毫米)或以下关键字:

描述
match_parent 设置维度匹配的父元素。在API级别8弃用FILL_PARENT
FILL_PARENT 设置维度匹配的父元素。
WRAP_CONTENT 设置尺寸仅适合该元件的内容所需的尺寸。
自定义视图元素

您可以创建自己的自定义视图一个ViewGroup 元素,并将它们应用到你的布局一样的标准布局元素。您还可以指定在XML元素支持的属性。要了解更多信息,请参阅自定义组件开发人员指南。

例:
在保存XML文件 RES /布局/ main_activity.xml
<?xml的 
 
               
               
               
     
              
              
              我是一个  
     
            
            
            我是一个按钮“  /> 
</ LinearLayout中>

此应用程序代码将加载布局的活动,在 的onCreate()方法:

public  void onCreate ( Bundle savedInstanceState )  { 
    super . onCreate ( savedInstanceState ); 
    setContentView ( R . layout . main_activity ); 
}
也可以看看:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值