android自学之旅——layout资源文件

本文只是我在看官方帮助文档的翻译和理解,以方便自己日后查看。

什么是layout资源文件?layout一个定义了Activity或者组件的布局的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、View或者merge节点,但是只能有一个有一个根节点,并且它必须有xml:android属性来定义命名空间。

节点介绍:

  <ViewGroup>

    它是一个可以放很多view的容器。ViewGroup有很多种,主要包含LinearLayout、RelativeLayout和FrameLayout。

    属性:

    android:id

      它是一个节点唯一标识名,你可以在应用程序中通过该标识名来应用一个ViewGroup。

    android:layout_height

      ViewGroup的高度,值可以是一个尺寸值,也可以是“fill_parent”或者“wrap_content”,它是必须的。

    android:layout_width

      ViewGroup的宽度,值可以是一个尺寸值,也可以是“fill_parent”或者“wrap_content”,它是必须的。

    以上只是一个ViewGroup最基本的属性,不同的ViewGroup还有很多各自属性。

  <view>

    一个UI组件比如:TextView、Button、CheckBox。

    通用的属性同ViewGroup,还有很多属于不同View各自的属性。

  <requestFocus>

    任何一个View节点都可以包含改属性,该属性使其父元素在初始化的时候获取焦点,但是每个layout中只允许一个节点拥有该属性。

  <include>

    该标签可以通过用的方式将其他定义好的layout文件加入到当前layout。

    属性:

    layout:

      layout资源的引用,必须的。

    android:id

      资源ID,重写当前的包含的layout的ID提供给根视图。

    android:layout_height

      重写当前包含的layout的高度提供给根视图,只有同时定义了android:layout_width才有效。

    android:layout_width

      重写当前包含的layout的宽度提供给根视图,只有同时定义了android:layout_height才有效。

    你也可以在include中重写根视图支持的include layout中的其他的属性。

    另一个包含一个layout方式是使用ViewStub。它是一个轻量级的视图不占用任何layout控制直到你明确的inflate它,可以通过android:layout来使用它。

  <merge>

    当前你想使用的ViewGroup已经包含的在layout中,并且你想通过include加入到当前ViewGroup中,可以使用当前元素作为根节点,将include layout中的

    ViewGroup与当前ViewGroup合并。

  Value for android:id

  定义改属性的值一般都是通过“@+id/name”这种形式,“+”表示这个是一个新的ID,如果这个“name”不存在,那么aapt工具将会创建一个新的resource integer在R.java类

  中。例如:

  

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

  这个“nameTextbox”现在就可以作为当前TextView的资源ID使用。你可以通过资源ID以下方式来引用TextView在java代码中:

findViewById(R.id.nameTextbox);

  这段代码将返回TextView的对象。

  Value for android:layout_heigth and android:layout_width

  heigth和width值可以是android支持的尺寸单位的值(px、sp、sp、pt、in、nm)或者是以下内容:

ValueDescrption
match_parent设置控件的尺寸和父级元素一致,API8中添加。
fill_parent设置当前控件的尺寸与父级元素一致,在API8以上过时。
wrap_content设置控件尺寸为包含控件中内容(图片或文字)

  实例:

  res/layout/main_activity.xml中的layout布局文件:

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

在Acitivity的onCreate加载该布局:

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

转载于:https://www.cnblogs.com/qinbang/p/5231709.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值