前言
该篇文章紧接着上篇 讲解android的布局资源,如果吧Activity比喻成一个人 ,那么我们今天所说的这个布局资源就是人穿的衣服 ,通过不同的布局资源可以让Activity显示不同的界面样式。
布局资源
- 布局资源存储的位置 :Android程序布局资源存储在res/layout文件夹下边,
- 编译之后的资源类型:每个布局文件编译之后会成为一个View或者是View的子类
- 资源的使用:在xml中我们通过 @[package:]layout/filename使用布局文件,在java代码中我们通过 R.layout.filename使用布局文件
- 语法
<?xml version="1.0" encoding="utf-8"?>
其他的View元素的容器,ViewGroup对象有很多种类型,每一种对应着不同的布局方式
<ViewGroup
xmlns:android="http://schemas.android.com/apk/res/android"
元素的唯一资源名称
android:id="@[+][package:]id/resource_name"
容器的高度
android:layout_height=["dimension" | "match_parent" | "wrap_content"]
容器的宽度
android:layout_width=["dimension" | "match_parent" | "wrap_content"]
[ViewGroup-specific attributes] >
单个界面的组建
<View
android:id="@[+][package:]id/resource_name"
android:layout_height=["dimension" | "match_parent" | "wrap_content"]
android:layout_width=["dimension" | "match_parent" | "wrap_content"]
[View-specific attributes] >
任何代表 View 对象的元素都可以包含这个空元素,它会在屏幕上为其父项提供初始焦点。每个文件只能包含一个这种元素。
添加布局的另一种方式是使用 ViewStub。这是一个轻型视图,在对其进行明确膨胀(此时它会添加由其 android:layout 属性定义的布局文件)之前,它不会占用任何布局空间。如需详细了解如何使用 ViewStub,请参阅按需加载视图。
<requestFocus/>
</View>
<ViewGroup >
<View />
</ViewGroup>
在此布局中包含一个布局文件
<include layout="@layout/layout_resource"/>
</ViewGroup>