background image

 

XML 布局资源定义线性布局

设计程序用户界面最方便和可维护的方法是创建

XML 布局资源。这个方法极大地简化

UI 设计过程,它将很多静态创建和用户界面控件的布局以及控件属性的定义移到了

XML 中,而不是写代码。

XML 布局资源必须被存储在项目目录的/res/layout 下。让我们看看前一节介绍的彩虹线

性布局。这个屏幕基本上就是一个设置为铺满整个屏幕的垂直线性布局,这通过设置它的
layout_width 和 layout_height 属 性 为 fill_parent 来 实 现 。 适 当 地 将 其 命 名

/res/layout/rainbow.xml,XML 定义如下:

1

<?xml version="1.0" encoding="utf-8"?>    

2

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    

3

android:layout_width="fill_parent" android:layout_height="fill_parent"    

4

android:orientation="vertical">    

5

<TextView android:text="RED" android:id="@+id/TextView01"    

6

android:layout_height="wrap_content" android:background="#f00"    

7

android:layout_width="fill_parent" android:layout_weight=".14"    

8

android:gravity="center" android:textColor="#000"></TextView>    

9

<TextView android:text="ORANGE" android:id="@+id/TextView02"    

10

android:layout_height="wrap_content" android:layout_width="fill_parent"    

11

android:layout_weight=".15" android:background="#ffa500"