Android开发之路——Android的布局初步


对于Android的布局,下面的一个例子可以很好的说明Android的布局的特性和Android的xml的设置。

首先是layout中的xml文件的各个函数的内容:

1.android:idà为控件指定相应的ID
2.android:textà指定控件当中显示的文字,需要注意的是,这里尽量用string.xml
3.android:gravityà指定控件的基本位置。比如说居中,居右等
4.android:textSizeà指定控件当中字体的大小
5.android:backgroundà指定该控件所使用的背景色,RGB命名方法
6.android:widthà指定控件的宽度
7.android:heightà指定控件的高度
8.android:paddingà指定控件的内边距,也就是说控件当中的内容
9.android:singleLineà如果设置为true的话,则将控件的内容在一行当中进行显示
10.android:layout_width:”fill_parant”à填满父控件;”wrap_content”à包围住本身。
11.   android:layout_heigth:”fill_parant”à填满父控件;”wrap_content”à包围住本身。
12.   android:orientation:布局方向。其中有两个选项,一个是水平的,一个是垂直的。水平的是horizental,垂直方向的布局是verical

相关阅读:

Android开发之路——走进Android(工程结构剖析)
Android开发之路——第一个Android小程序(Android电话拨号器)
Android开发之路——第二个Android小程序(Android短信发送)
Android开发之路——第三个Android小程序(Android的Activity显示)
Android开发之路——Android的布局初步
Android开发之路——Android的布局初步2——TableLayout布局
Android开发之路——单选框,复选框,弹出框等控件操作
Android开发学习笔记补充记录——Activity的生命周期

上面的各个属性都是在layout文件夹里面的,下面通过一个例子来表示上面所有显示的函数的用法:

例子:这个例子的结果图如下:

上面这个显示就是利用了上面的函数来实现了上下两个不一样的TextView的内容的显示,采取的布局模式是LinearLayout的布局模式。

下面是这个显示的代码。

代码主要是在layout的文件夹里面的main.xml,其他的代码都可以采取Android默认的。下面贴代码:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.       
  8. <TextView    
  9.     android:id = "@+id/firstText"  
  10.     android:text = "第一行"  
  11.     android:gravity = "center_vertical"  
  12.     android:textSize = "15pt"  
  13.     android:background = "#aa0000"  
  14.     android:layout_width = "fill_parent"  
  15.     android:layout_height = "wrap_content"  
  16.     android:paddingLeft = "10dip"  
  17.     android:paddingTop = "20dip"  
  18.     android:paddingRight = "30dip"  
  19.     android:paddingBottom = "40dip"  
  20.     android:layout_weight="1"  
  21.     android:singleLine="true"  
  22.     />  
  23.       
  24. <TextView  
  25.     android:id = "@+id/secondText"  
  26.     android:text = "第二行"  
  27.     android:gravity = "center_vertical"  
  28.     android:textSize = "15pt"  
  29.     android:background = "#0000aa"  
  30.     android:layout_width = "fill_parent"  
  31.     android:layout_height = "wrap_content"  
  32.     android:layout_weight="2"  
  33.     />  
  34. </LinearLayout>  

相关内容