对于Android的布局,下面的一个例子可以很好的说明Android的布局的特性和Android的xml的设置。
首先是layout中的xml文件的各个函数的内容:
- android:idà为控件指定相应的ID
- android:textà指定控件当中显示的文字,需要注意的是,这里尽量用string.xml
- android:gravityà指定控件的基本位置。比如说居中,居右等
- android:textSizeà指定控件当中字体的大小
- android:backgroundà指定该控件所使用的背景色,RGB命名方法
- android:widthà指定控件的宽度
- android:heightà指定控件的高度
- android:paddingà指定控件的内边距,也就是说控件当中的内容
- android:singleLineà如果设置为true的话,则将控件的内容在一行当中进行显示
- android:layout_”fill_parant”à填满父控件;”wrap_content”à包围住本身。
- android:layout_heigth:”fill_parant”à填满父控件;”wrap_content”à包围住本身。
- android:orientation:布局方向。其中有两个选项,一个是水平的,一个是垂直的。水平的是horizental,垂直方向的布局是verical
上面的各个属性都是在layout文件夹里面的,下面通过一个例子来表示上面所有显示的函数的用法:
例子:这个例子的结果图如下:
例子:这个例子的结果图如下:
上面这个显示就是利用了上面的函数来实现了上下两个不一样的TextView的内容的显示,采取的布局模式是LinearLayout的布局模式。
下面是这个显示的代码。
代码主要是在layout的文件夹里面的main.xml,其他的代码都可以采取Android默认的。下面贴代码:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id = "@+id/firstText"
- android:text = "第一行"
- android:gravity = "center_vertical"
- android:textSize = "15pt"
- android:background = "#aa0000"
- android:layout_width = "fill_parent"
- android:layout_height = "wrap_content"
- android:paddingLeft = "10dip"
- android:paddingTop = "20dip"
- android:paddingRight = "30dip"
- android:paddingBottom = "40dip"
- android:layout_weight="1"
- android:singleLine="true"
- />
- <TextView
- android:id = "@+id/secondText"
- android:text = "第二行"
- android:gravity = "center_vertical"
- android:textSize = "15pt"
- android:background = "#0000aa"
- android:layout_width = "fill_parent"
- android:layout_height = "wrap_content"
- android:layout_weight="2"
- />
- </LinearLayout>