• Android开发者指南(26) —— Resource Types Layout


    前言

      本章内容为Android开发者指南的Framework Topics/Application Resources/Resource Types/Layout章节,译为"布局资源",版本为Android 3.2 r1,翻译来自:"呆呆大虾",欢迎访问他的微博:"http://weibo.com/popapa",再次感谢"呆呆大虾" !期待你一起参与翻译Android的相关资料,联系我over140@gmail.com。

    声明

      欢迎转载,但请保留文章原始出处:)

        博客园:http://www.cnblogs.com/

        Android中文翻译组:http://goo.gl/6vJQl


    Resources Types - Layout

    译者署名: 呆呆大虾

    译者微博:http://weibo.com/popapa

    版本:Android 3.2 r1

    原文

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

     

    参见

             声明 Layout

     

    布局资源

    布局(layout)资源用于定义Activity内用户界面(UI)或者用户界面控件的布局结构。

    文件位置:

    res/layout/filename.xml

    文件名filename将作为资源ID

     

    编译后资源的数据类型:

    指向View或其子类的资源指针。

     

    资源引用:

    Java代码:R.layout.filename

    XML代码:@[package:]layout/filename

     

    语法:

    <?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>

    注意:根元素可以是ViewGroupView<merge>但只能有一个根元素且必须包含带android 命名空间的xmlns:android属性,如上例所示。

    元素:

    <ViewGroup>

    作为View元素的容器,ViewGroup对象分很多种类,每一类都能用不同的方式指定其子元素的布局。ViewGroup对象的种类包括:LinearLayoutRelativeLayout FrameLayout

     

    并不是所有的ViewGroup派生类都能容纳嵌套的View 有些ViewGroupAdapterView类的实现,因此就只能容纳Adapter类型的子元素。

     

    属性:

    android:id

    资源 ID。为元素指定一个唯一的资源名称,用于应用程序能实现对ViewGroup引用。详见下文android:id的值

    android:layout_height

    度量或关键字。必填项。高度,用度量值、度量资源 ( dimension resource) 或关键字"fill_parent" "wrap_content"指定。参阅下文合法值

    android:layout_width

    度量或关键字。必填项。宽度,用度量值、度量资源 (dimension resource) 或关键字"fill_parent" "wrap_content"指定。参阅下文合法值

    ViewGroup基类支持更多的属性,每种子类的实现也支持很多其他属性。完整的可用属性请参阅各个类相关的参考文档,例如LinearLayout XML attributes

     

    <View>

    一个独立的UI控件,通常是指一个部件(widget)。View对象的种类包括:TextViewButtonCheckBox

     

    属性:

    android:id

    资源 ID。为元素指定一个唯一的资源名称,用于应用程序能实现对ViewGroup引用。详见下文android:id的值

    android:layout_height

    度量或关键字。必填项。高度,用度量值、度量资源 ( dimension resource) 或关键字"fill_parent" "wrap_content"指定。参阅下文合法值

    android:layout_width

    度量或关键字。必填项。宽度,用度量值、度量资源 ( dimension resource) 或关键字"fill_parent" "wrap_content"指定。参阅下文合法值

    View基类支持更多的属性,每种子类的实现也支持很多其他属性。更多信息请参阅声明Layout。完整的可用属性请参阅各个类相关的参考文档,例如TextView XML 属性

     

    <requestFocus>

    将屏幕的初始焦点设成其父元素,任何表示View类对象的元素都能包含这个内容为空的元素。但每个文件内只能出现一次本元素。

     

    <include>

    将另一个布局(layout)文件包含进来。

    属性:

    layout

    Layout资源必填项。引用布局资源。

    android:id

    资源ID。覆盖包含进来的layout资源中的根view ID

    android:layout_height

    度量或关键字。覆盖包含进来的layout资源中根view给出的高度。仅在同时给出android:layout_width时才生效。

    android:layout_width

    度量或关键字。覆盖包含进来的layout资源中根view给出的高度。仅在同时给出android:layout_height时才生效。

    只要是被包含的layout资源根元素支持的属性,都能在<include>元素中包含进来,并且会覆盖本资源内根元素已定义的属性。

    注意: 如果要覆盖layout的度量(长度和宽度),必须同时覆盖android:layout_height android:layout_width——不能只覆盖长度或只覆盖宽度。如果只覆盖其中一个,则不会生效。(其他布局属性,比如weight,仍然继承自原有的layout。)

    包含layout资源的另一种方式是使用ViewStub。这是个轻量级的View,它在实际被填充之前不占用layout空间。在实际被填充时,它再把android:layout属性指定的layout资源文件动态包含进来。使用ViewStub的更多信息请参阅Layout特技: ViewStubs

     

    <merge>

    layout 的层次结构图里并没画出这个可充当根的元素。当明确知道本layout会被放入某个父View中去时,可以用<merge>作为根元素来包裹其下的子元素。如果希望本layout能被其他layout<include>包含进去,并不再另外生成ViewGroup容器,本元素也特别有用。合并layout的更多信息请参阅Layout特技合并

     

    android:id的值

    对于ID的值,通常应该用"@+id/name"的语法格式。加号+表示这是个新的资源ID。如果R.java类中不存在此资源的话,aapt工具将在R.java类中生成一个新的resource整数标识。例如:

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

    名称nameTextbox现在作为资源ID和本元素关联。然后在Java代码里可以用此ID名称来引用这个TextView

    findViewById(R.id.nameTextbox) ;

    本行代码返回TextView对象。

    然而,如果已经定义了一个ID资源(并且没被使用过),就可以去掉android:id里的加号并将此ID赋予View元素

     

    android:layout_height android:layout_width的值

    高度和宽度值能用Android支持的度量单位px, dp, sp, pt, in, mm)或者以下关键字来表示:

    描述

    match_parent

    和父元素匹配(充满)。自API Level 8开始加入,以淘汰fill_parent

    fill_parent

    和父元素匹配(充满)。

    wrap_content

    仅本元素内容需要的大小。

     

    自定义View元素

    可以生成自定义的ViewViewGroup元素并且和标准布局元素一样放入layout。还可以在XML元素内定义其所支持的属性。更多内容请参阅创建自定义控件

     

    示例:

    存放在res/layout/main_activity.xmlXML文件:

    <?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>

     

    以下代码在ActivityonCreate()方法中装入layout

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView.(R.layout.main_activity);

    }

     

    参见

    *   Declaring Layout

    *  View

    *   ViewGroup

     

  • 相关阅读:
    RabbitMq安装笔记
    SpringBoot笔记--Jackson
    SpringBoot笔记--FastJson
    由一个“两次请求”引出的Web服务器跨域请求访问问题的解决方案
    转:SpringMVC之类型转换Converter(GenericConverter)
    npm 命令
    数据分页技巧
    Mongo 开发笔记
    Android 开发
    Bash 笔记
  • 原文地址:https://www.cnblogs.com/over140/p/2207643.html
Copyright © 2020-2023  润新知