Android资源——布局资源

一、概述

       1、android的图形界面展示可以分为三层:activity、Window和View,而View又可以分为View(组件,如TextView)和ViewGroup(布局,如线性布局)。

        2、布局是指组件在activity中的呈现方式,有两种方式创建布局:

        a)利用xml文件,activity通过setContentView(R.layout.activity_main)的方式获取布局资源;

            xml文件必须具备一个根元素(可以是View或者ViewGroup)和一个xmlns:android属性(属性值为android的命名空间)

        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>

        b)硬代码实现;

 

 

二、布局分类

      布局可以分为5种类型:线性布局(LinearLayout)、相对布局、帧布局、表格布局、绝对布局。

2.1 线性布局

     线性布局是指将组件按照水平或者垂直方向放置。

    常见属性:

   方向控制(属性值为horizontal或者vertical)


三、实例解析

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ImageView01"
        android:src="@drawable/g1"
    />

</RelativeLayout>


属性xmlns:android表示命名空间;

 

 

 

参考文献:

1、《Android应用开发详解》

2、http://www.w3school.com.cn/xml/xml_syntax.asp

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



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值