Android资源——布局资源

来源:互联网 发布:乐器模拟软件排行 编辑:程序博客网 时间:2024/05/02 03:09

一、概述

       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



原创粉丝点击