---
title: "主题：三，android编码规范 & 常用布局 & 常用控件"
author: "he91_com"
date: "2011-11-28T20:25:00Z"
tags: []
slug: "android"
draft: false
lastmod: "2013-02-20T11:19:40Z"
id: 1808272
url: "https://he91-com.iteye.com/blog/1808272"
---

1，android编码规范

Android官方并没有给出相应编码规范。以下都是我从源码、示例中总结的所谓规范。若公司有相应规范以公司为准。

首先从布局文件说起，布局文件名称的定义必须为小写字母，否者无法生成R类，尽量不要用缩写。以表达清楚该文件用途为本，通常情况下用下划线连接各语义单词，例如dialog\_title\_icons.xml或者list\_menu\_item\_checkbox.xml。

控件ID的定义，ID的定义一律为小写,例如：一用户名TextView可以定义为：@+id/username\_view。以“名词\_控件名称”这种形式定义。

其次是图片的定义格式，图片的定义也是为解释清楚用途为准，参照这种定义格式“btn\_background\_ok.png”

string类的name定义，这里可以按照JAVA中变量的定义方式定义。首字母小写，驼峰命名法。例 如： &lt;stringname="userName\_view"&gt;用户名：&lt;/string&gt;

最后类名与变量的定义，定义与用户交互的类，××Activity.java。自定义变量一律以小写m开头例如:EditTextmUserName=(EditText)findViewById(R.id.username\_edit);

2，常用布局

Android提供了一组视图类来充当视图的容器，这些容器类称为布局或者布局管理器，每一个都实现一种具体的策略来管理其子控件的大小和位置。最常用的布局有以下这几种：

LinearLayout,RleativeLayout,TableLayout,FrameLayout等。有两种方式可以声明布局，一种是编码的方式，另外一中通过XML配置的方式。Android默认是通过xml的方式构建应用程序的。这种方式最大的优点是代码与视图分离，这意味着你可以修改或调整，而无需修改源代码并重新编译。例如你可以创建不同的屏幕方向，支持不同分辨率的设备。也更直观更容易调试。

（1）LinearLayout：线性布局

最常用的一种布局方式，所有子控件的对齐方式，取决于如何定义orientation的属性：vertical 垂直方向，如果按照这种方向所有的子控件将按照垂直的方式分布在布局上，每行只允许有 一 个子元素，horizontal水平方向，这时子控件将会以水平的方向分布在布局中。以下线性布 局的简单例子。先上图：

![](http://dl.iteye.com/upload/attachment/549862/673cc39b-ab85-32c1-9147-f3c83652df8b.jpg)

Xml代码 [![复制代码](http://www.iteye.com/images/icon_copy.gif)](http://www.iteye.com/topic/1114402 "复制代码")[![收藏代码](http://www.iteye.com/images/icon_star.png)![](http://www.iteye.com/images/spinner.gif)]()

01. &lt;?xmlversion="1.0"encoding="utf-8"?&gt;
02. &lt;!--线性布局，最外面包裹一个水平线性布局--&gt;
03. &lt;!--orientation表示线性布局的方向,horizontal:水平方向vertical:垂直方向--&gt;
04. &lt;!--@代表R类，如果是自定义的ID则用@+id/×××表示，如果是引用R类的资源则@string/×××--&gt;
05. &lt;LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
06. android:orientation="horizontal"
07. android:background="@drawable/bg"
08. android:layout\_width="fill\_parent"
09. android:layout\_height="fill\_parent"
10. &gt;
11. &lt;LinearLayout
12. android:layout\_width="wrap\_content"
13. android:layout\_height="wrap\_content"
14. android:orientation="vertical"
15. &gt;
16. &lt;TextView
17. android:layout\_width="fill\_parent"
18. android:layout\_height="wrap\_content"
19. android:text="@string/linear"
20. /&gt;
21. &lt;Button
22. android:id="@+id/button"
23. android:layout\_width="183dp"
24. android:layout\_height="wrap\_content"
25. android:text="@string/button"
26. /&gt;
27. &lt;ImageButton
28. android:id="@+id/imagebutton"
29. android:layout\_width="180dp"
30. android:layout\_height="48dp"
31. android:src="@drawable/imagebutton"
32. /&gt;
33. &lt;/LinearLayout&gt;
34. &lt;!--android:layout\_gravity与android:gravity区别，拿一个button作为例子
35. 前者的意思，是这个按钮的位置，如果设置为right则表示这个按钮整体位置靠右;
36. 后者的意思，这个按钮上显示内容的位置。
37. --&gt;
38. &lt;LinearLayout
39. android:gravity="right"
40. android:layout\_width="fill\_parent"
41. android:layout\_height="wrap\_content"
42. android:orientation="horizontal"
43. &gt;
44. &lt;ImageView
45. android:id="@+id/imageview"
46. android:layout\_marginTop="5dp"
47. android:src="@drawable/imageview"
48. android:layout\_width="131dp"
49. android:layout\_height="131dp"
50. /&gt;
51. &lt;/LinearLayout&gt;
52. &lt;/LinearLayout&gt;

```
<?xml version="1.0" encoding="utf-8"?>
<!--线性布局，最外面包裹一个水平线性布局-->
<!--orientation表示线性布局的方向,horizontal:水平方向  vertical:垂直方向 -->
<!-- @代表R类，如果是自定义的ID 则用@+id/××× 表示，如果是引用R类的资源则@string/×××-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:background="@drawable/bg"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:orientation="vertical"
    >
		<TextView  
		    android:layout_width="fill_parent" 
		    android:layout_height="wrap_content" 
		    android:text="@string/linear"
		    />
		 <Button
		 	android:id="@+id/button"
		 	android:layout_width="183dp" 
		    android:layout_height="wrap_content" 
		    android:text="@string/button"
		 	/>
		 <ImageButton 
		 	android:id="@+id/imagebutton"
		 	android:layout_width="180dp"
		 	android:layout_height="48dp"
		 	android:src="@drawable/imagebutton"
		 	/>
		 	
	 </LinearLayout>
	 <!-- android:layout_gravity与android:gravity区别，拿一个button作为例子
	 	  前者的意思，是这个按钮的位置，如果设置为right则表示这个按钮整体位置靠右;
	 	  后者的意思，这个按钮上显示内容的位置。
	  -->
	 <LinearLayout
	 	android:gravity="right"
	 	android:layout_width="fill_parent"
	 	android:layout_height="wrap_content"
	 	android:orientation="horizontal"
	 	>
	 	<ImageView
	 		android:id="@+id/imageview"
	 		android:layout_marginTop="5dp"
	 		android:src="@drawable/imageview"
	 		android:layout_width="131dp"
	 		android:layout_height="131dp"
	 	/>
	 </LinearLayout>
	
</LinearLayout>
```

package net.csdn.blog.androidtoast;

Java代码 [![复制代码](http://www.iteye.com/images/icon_copy.gif)](http://www.iteye.com/topic/1114402 "复制代码")[![收藏代码](http://www.iteye.com/images/icon_star.png)![](http://www.iteye.com/images/spinner.gif)]()

01. importandroid.app.Activity;
02. importandroid.os.Bundle;
03. importandroid.view.View;
04. importandroid.view.View.OnClickListener;
05. importandroid.widget.Toast;
06. publicclassMainActivityextendsActivityimplementsOnClickListener{
07. /\*\*Calledwhentheactivityisfirstcreated.\*/
08. @Override
09. publicvoidonCreate(BundlesavedInstanceState){
10. super.onCreate(savedInstanceState);
11. setContentView(R.layout.linearlayout);
12. //实例化以下控件，并设置监听事件，传入实现了OnClickListener接口的对象
13. findViewById(R.id.button).setOnClickListener(this);
14. findViewById(R.id.imagebutton).setOnClickListener(this);
15. findViewById(R.id.imageview).setOnClickListener(this);
16. }
17. /\**
18. \*点击事件判断所点击是哪个控件并toast提示。
19. \*/
20. @Override
21. publicvoidonClick(Viewv){
22. intid=v.getId();//得到所点对象ID
23. if(id==R.id.button){
24. Toast.makeText(getApplicationContext(),R.string.promptButton,1).show();
25. }elseif(id==R.id.imagebutton){
26. Toast.makeText(getApplicationContext(),R.string.promptImageButton,1).show();
27. }elseif(id==R.id.imageview){
28. Toast.makeText(getApplicationContext(),R.string.promptImageView,1).show();
29. }
30. }
31. }

```
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linearlayout);
        //实例化以下控件，并设置监听事件，传入实现了OnClickListener接口的对象
        findViewById(R.id.button).setOnClickListener(this);
        findViewById(R.id.imagebutton).setOnClickListener(this);
        findViewById(R.id.imageview).setOnClickListener(this);
        
    }

    /**
     * 点击事件判断所点击是哪个控件并toast提示。
     */
	@Override
	public void onClick(View v) {
		int id=v.getId();//得到所点对象ID
		if(id==R.id.button){
			Toast.makeText(getApplicationContext(), R.string.promptButton, 1).show();
		}else if(id==R.id.imagebutton){
			Toast.makeText(getApplicationContext(), R.string.promptImageButton, 1).show();
		}else if(id==R.id.imageview){
			Toast.makeText(getApplicationContext(), R.string.promptImageView, 1).show();
		}
	}
}
```

（2）RleativeLayout：相对布局

如果你的程序中出现了多个LinearLayout嵌套，就应该考虑使用相对布局了。相对局部顾名思义一个控件的位置相对于其他控件或者容器的位置。使用很简单直接上示例：

![](http://dl.iteye.com/upload/attachment/549865/9513d52f-a05d-3d9b-9a18-ba1e6e76ae9a.jpg)

![](http://dl.iteye.com/upload/attachment/549867/2b730825-8ae5-35c1-80f3-4e3d1233dcc4.jpg)

![](http://dl.iteye.com/upload/attachment/549869/ceda3825-3b0d-3ce8-bb98-7f55237bb21e.jpg)

Xml代码 [![复制代码](http://www.iteye.com/images/icon_copy.gif)](http://www.iteye.com/topic/1114402 "复制代码")[![收藏代码](http://www.iteye.com/images/icon_star.png)![](http://www.iteye.com/images/spinner.gif)]()

001. &lt;?xmlversion="1.0"encoding="utf-8"?&gt;
002. &lt;!--相对布局一个控件相对于另一个控件或者容器的位置。--&gt;
003. &lt;RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
004. android:orientation="vertical"
005. android:background="@drawable/bg"
006. android:layout\_width="fill\_parent"
007. android:layout\_height="fill\_parent"
008. &gt;
009. &lt;TextView
010. android:id="@+id/describe\_view"
011. android:layout\_width="fill\_parent"
012. android:layout\_height="wrap\_content"
013. android:text="@string/hello"
014. android:textColor="#556055"
015. /&gt;
016. &lt;!--这个TextView相对于上一个TextView在它的下方所以设置属性为layout\_below--&gt;
017. &lt;TextView
018. android:id="@+id/username\_view"
019. android:layout\_width="wrap\_content"
020. android:layout\_height="wrap\_content"
021. android:layout\_marginTop="12dp"
022. android:text="@string/username"
023. android:textColor="#556055"
024. android:layout\_below="@id/describe\_view"
025. /&gt;
026. &lt;EditText
027. android:id="@+id/username\_edit"
028. android:layout\_width="90dp"
029. android:layout\_height="40dp"
030. android:layout\_marginTop="4dp"
031. android:layout\_toRightOf="@id/username\_view"
032. android:layout\_below="@id/describe\_view"
033. /&gt;
034. &lt;TextView
035. android:id="@+id/sex\_view"
036. android:layout\_width="wrap\_content"
037. android:layout\_height="wrap\_content"
038. android:layout\_marginTop="12dp"
039. android:text="@string/sex"
040. android:textColor="#556055"
041. android:layout\_below="@id/describe\_view"
042. android:layout\_toRightOf="@id/username\_edit"
043. /&gt;
044. &lt;RadioGroup
045. android:id="@+id/sex\_radiogroup"
046. android:orientation="horizontal"
047. android:layout\_width="wrap\_content"
048. android:layout\_height="wrap\_content"
049. android:layout\_toRightOf="@id/sex\_view"
050. android:layout\_below="@id/describe\_view"
051. &gt;
052. &lt;!--第一个RadioButton--&gt;
053. &lt;RadioButton
054. android:id="@+id/male\_radiobutton"
055. android:layout\_width="wrap\_content"
056. android:layout\_height="wrap\_content"
057. android:text="男"
058. /&gt;
059. &lt;!--第二个RadioButton--&gt;
060. &lt;RadioButton
061. android:id="@+id/woman\_radiobutton"
062. android:layout\_width="wrap\_content"
063. android:layout\_height="wrap\_content"
064. android:text="女"
065. /&gt;
066. &lt;/RadioGroup&gt;
067. &lt;TextView
068. android:id="@+id/age\_view"
069. android:layout\_width="wrap\_content"
070. android:layout\_height="wrap\_content"
071. android:paddingTop="25dp"
072. android:text="@string/age"
073. android:textColor="#556055"
074. android:layout\_below="@id/username\_view"
075. /&gt;
076. &lt;EditText
077. android:id="@+id/brithday\_edit"
078. android:layout\_width="90dp"
079. android:layout\_height="40dp"
080. android:layout\_marginTop="4dp"
081. android:hint="@string/selectdate"
082. android:textSize="13sp"
083. android:gravity="center"
084. android:editable="false"
085. android:layout\_toRightOf="@id/age\_view"
086. android:layout\_below="@id/username\_edit"
087. /&gt;
088. &lt;TextView
089. android:id="@+id/education\_view"
090. android:layout\_width="wrap\_content"
091. android:layout\_height="wrap\_content"
092. android:paddingTop="25dp"
093. android:text="@string/education"
094. android:textColor="#556055"
095. android:layout\_below="@id/sex\_view"
096. android:layout\_toRightOf="@id/brithday\_edit"
097. /&gt;
098. &lt;!--下拉列表控件--&gt;
099. &lt;Spinner
100. android:id="@+id/edu\_spinner"
101. android:layout\_width="108dp"
102. android:layout\_height="38dp"
103. android:layout\_below="@id/sex\_radiogroup"
104. android:layout\_toRightOf="@id/education\_view"
105. /&gt;
106. &lt;TextView
107. android:id="@+id/interest\_view"
108. android:layout\_width="wrap\_content"
109. android:layout\_height="wrap\_content"
110. android:paddingTop="25dp"
111. android:text="@string/interest"
112. android:textColor="#556055"
113. android:layout\_below="@id/age\_view"
114. /&gt;
115. &lt;!--复选框控件--&gt;
116. &lt;CheckBox
117. android:id="@+id/car\_check"
118. android:layout\_width="wrap\_content"
119. android:layout\_height="wrap\_content"
120. android:text="@string/car"
121. android:textColor="#566156"
122. android:layout\_toRightOf="@id/interest\_view"
123. android:layout\_below="@id/brithday\_edit"
124. /&gt;
125. &lt;CheckBox
126. android:id="@+id/sing\_check"
127. android:layout\_width="wrap\_content"
128. android:layout\_height="wrap\_content"
129. android:layout\_marginLeft="11dp"
130. android:text="@string/sing"
131. android:textColor="#566156"
132. android:layout\_toRightOf="@id/car\_check"
133. android:layout\_below="@id/brithday\_edit"
134. /&gt;
135. &lt;CheckBox
136. android:id="@+id/movie\_check"
137. android:layout\_width="wrap\_content"
138. android:layout\_height="wrap\_content"
139. android:layout\_marginLeft="11dp"
140. android:text="@string/movie"
141. android:textColor="#566156"
142. android:layout\_toRightOf="@id/sing\_check"
143. android:layout\_below="@id/brithday\_edit"
144. /&gt;
145. &lt;Button
146. android:id="@+id/submit\_button"
147. android:layout\_width="100dp"
148. android:layout\_height="40dp"
149. android:text="@string/submit"
150. android:gravity="center"
151. android:layout\_below="@id/movie\_check"
152. android:layout\_marginLeft="210dp"
153. android:layout\_marginTop="15dp"
154. /&gt;
155. &lt;/RelativeLayout&gt;

```
<?xml version="1.0" encoding="utf-8"?>
<!-- 相对布局  一个控件相对于另一个控件或者容器的位置。 -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@drawable/bg"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<TextView  
		android:id="@+id/describe_view" 
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="@string/hello"
	    android:textColor="#556055"
	    />
	 <!-- 这个TextView相对于上一个TextView 在 它的下方所以设置属性为layout_below-->
	 <TextView
	 	android:id="@+id/username_view"
	 	android:layout_width="wrap_content"
	 	android:layout_height="wrap_content"
	 	android:layout_marginTop="12dp"
	 	android:text="@string/username"
	 	android:textColor="#556055"
	 	android:layout_below="@id/describe_view"
	 />
	 <EditText
	 	android:id="@+id/username_edit"
	 	android:layout_width="90dp"
	 	android:layout_height="40dp"
	 	android:layout_marginTop="4dp"
	 	android:layout_toRightOf="@id/username_view"
	 	android:layout_below="@id/describe_view"
	 />
	 <TextView
	 	android:id="@+id/sex_view"
	 	android:layout_width="wrap_content"
	 	android:layout_height="wrap_content"
	 	android:layout_marginTop="12dp"
	 	android:text="@string/sex"
	 	android:textColor="#556055"
	 	android:layout_below="@id/describe_view"
	 	android:layout_toRightOf="@id/username_edit"
	 />
	 <RadioGroup
	 	android:id="@+id/sex_radiogroup"
	 	android:orientation="horizontal"
	 	android:layout_width="wrap_content"
	 	android:layout_height="wrap_content"
	 	android:layout_toRightOf="@id/sex_view"
	 	android:layout_below="@id/describe_view"
	 	>
	    <!--第一个RadioButton -->
	    <RadioButton
	      android:id="@+id/male_radiobutton"
	      android:layout_width="wrap_content"
	      android:layout_height="wrap_content"
	      android:text="男"
	    />
	    <!--第二个RadioButton -->
	    <RadioButton
	      android:id="@+id/woman_radiobutton"
	      android:layout_width="wrap_content"
	      android:layout_height="wrap_content"
	      android:text="女"
	    />
	 </RadioGroup>
	 <TextView
	 	android:id="@+id/age_view"
	 	android:layout_width="wrap_content"
	 	android:layout_height="wrap_content"
	 	android:paddingTop="25dp"
	 	android:text="@string/age"
	 	android:textColor="#556055"
	 	android:layout_below="@id/username_view"
	 />
	 	
	 <EditText
	 	android:id="@+id/brithday_edit"
	 	android:layout_width="90dp"
	 	android:layout_height="40dp"
	 	android:layout_marginTop="4dp"
	 	android:hint="@string/selectdate"
	 	android:textSize="13sp"
	 	android:gravity="center"
	 	android:editable="false"
	 	android:layout_toRightOf="@id/age_view"
	 	android:layout_below="@id/username_edit"
	 />
	  <TextView
	 	android:id="@+id/education_view"
	 	android:layout_width="wrap_content"
	 	android:layout_height="wrap_content"
	 	android:paddingTop="25dp"
	 	android:text="@string/education"
	 	android:textColor="#556055"
	 	android:layout_below="@id/sex_view"
	 	android:layout_toRightOf="@id/brithday_edit"
	 />
	 <!-- 下拉列表控件 -->
	 <Spinner
	 	android:id="@+id/edu_spinner"
	 	android:layout_width="108dp"
	 	android:layout_height="38dp"
	 	android:layout_below="@id/sex_radiogroup"
	 	android:layout_toRightOf="@id/education_view"
	 />
	 <TextView
	 	android:id="@+id/interest_view"
	 	android:layout_width="wrap_content"
	 	android:layout_height="wrap_content"
	 	android:paddingTop="25dp"
	 	android:text="@string/interest"
	 	android:textColor="#556055"
	 	android:layout_below="@id/age_view"
	 />
	 <!-- 复选框控件 -->
	 <CheckBox
	 	android:id="@+id/car_check"
	 	android:layout_width="wrap_content"
	 	android:layout_height="wrap_content"
	 	android:text="@string/car"
	 	android:textColor="#566156"
	 	android:layout_toRightOf="@id/interest_view"
	 	android:layout_below="@id/brithday_edit"
	 />
	 <CheckBox
	 	android:id="@+id/sing_check"
	 	android:layout_width="wrap_content"
	 	android:layout_height="wrap_content"
	 	android:layout_marginLeft="11dp"
	 	android:text="@string/sing"
	 	android:textColor="#566156"
	 	android:layout_toRightOf="@id/car_check"
	 	android:layout_below="@id/brithday_edit"
	 />
	 <CheckBox
	 	android:id="@+id/movie_check"
	 	android:layout_width="wrap_content"
	 	android:layout_height="wrap_content"
	 	android:layout_marginLeft="11dp"
	 	android:text="@string/movie"
	 	android:textColor="#566156"
	 	android:layout_toRightOf="@id/sing_check"
	 	android:layout_below="@id/brithday_edit"
	 />
	 <Button
	 	android:id="@+id/submit_button"
	 	android:layout_width="100dp"
	 	android:layout_height="40dp"
	 	android:text="@string/submit"
	 	android:gravity="center"
	 	android:layout_below="@id/movie_check"
	 	android:layout_marginLeft="210dp"
	 	android:layout_marginTop="15dp"
	 />
</RelativeLayout>
```

（3）TableLayout：表格布局

TableLayout布局是LinearLayout的扩展，以行和列的形式组织其子控件。与HTML中得Table相似。每一个TableRow元素代表一行。TableRow中包含几个控件代表几列。尽管使用TableRow来填充TableLayout是最常见的模式，但是该布局中可以放置任何子控件。需要指出的是TableLayout的子控件不能指定android:layout\_width="wrap\_content",它们被强制设定为fill\_parent。但是可以设置高度。还有两个不太好理解的属性的说一下，android：stretchColums此属性指要被拉伸的列。取值可以单个列的索引也可以是一组列的索引值。例如：如果一行有三列。stretchColums="1"这表示拉伸第二列填充剩余空间。android:layout\_column="1"这个属性指定子控件放置在哪一列上。例如

&lt;TextViewandroid:layout\_column="1"android:text="Open..."android:padding="3dip"/&gt;指该控放置在第二列。上图：

![](http://dl.iteye.com/upload/attachment/549875/7464a61f-751b-3bdb-8ae4-3696249a5c2f.jpg)

Xml代码 [![复制代码](http://www.iteye.com/images/icon_copy.gif)](http://www.iteye.com/topic/1114402 "复制代码")[![收藏代码](http://www.iteye.com/images/icon_star.png)![](http://www.iteye.com/images/spinner.gif)]()

01. &lt;?xmlversion="1.0"encoding="utf-8"?&gt;
02. &lt;!--
03. android:stretchColumns="1"是设置TableLayout所有行的第二列为拉伸列。
04. 也就是说如果每行都有三列的话，剩余的空间由第二列补齐
05. --&gt;
06. &lt;!--最外层包裹一个滚动条--&gt;
07. &lt;ScrollView
08. xmlns:android="http://schemas.android.com/apk/res/android"
09. android:layout\_width="fill\_parent"
10. android:layout\_height="wrap\_content"&gt;
11. &lt;TableLayout
12. android:layout\_width="fill\_parent"
13. android:layout\_height="fill\_parent"
14. android:stretchColumns="1"&gt;
15. &lt;TableRow&gt;
16. &lt;View
17. android:layout\_height="80dp"
18. android:layout\_width="250dp"
19. android:background="#D6D6D6"
20. /&gt;
21. &lt;TextView
22. android:layout\_height="80dp"
23. android:text="#D6D6D6"
24. android:gravity="center"/&gt;
25. &lt;/TableRow&gt;
26. &lt;!--此处的View控件充当着一个分割条的作用--&gt;
27. &lt;View
28. android:layout\_height="2dip"
29. android:background="#C4C4C4"/&gt;
30. &lt;TableRow&gt;
31. &lt;View
32. android:layout\_height="80dp"
33. android:layout\_width="250dp"
34. android:background="#292929"
35. /&gt;
36. &lt;TextView
37. android:layout\_height="80dp"
38. android:text="#292929"
39. android:gravity="center"/&gt;
40. &lt;/TableRow&gt;
41. &lt;View
42. android:layout\_height="2dip"
43. android:background="#C4C4C4"/&gt;
44. &lt;TableRow&gt;
45. &lt;View
46. android:layout\_height="80dp"
47. android:layout\_width="250dp"
48. android:background="#8A500E"
49. /&gt;
50. &lt;TextView
51. android:layout\_height="80dp"
52. android:text="#8A500E"
53. android:gravity="center"/&gt;
54. &lt;/TableRow&gt;
55. &lt;View
56. android:layout\_height="2dip"
57. android:background="#C4C4C4"/&gt;
58. &lt;TableRow&gt;
59. &lt;View
60. android:layout\_height="80dp"
61. android:layout\_width="250dp"
62. android:background="#D67C15"
63. /&gt;
64. &lt;TextView
65. android:layout\_height="80dp"
66. android:text="#D67C15"
67. android:gravity="center"/&gt;
68. &lt;/TableRow&gt;
69. &lt;View
70. android:layout\_height="2dip"
71. android:background="#C4C4C4"/&gt;
72. &lt;TableRow&gt;
73. &lt;View
74. android:layout\_height="80dp"
75. android:layout\_width="250dp"
76. android:background="#8A270E"
77. /&gt;
78. &lt;TextView
79. android:layout\_height="80dp"
80. android:text="#8A270E"
81. android:gravity="center"/&gt;
82. &lt;/TableRow&gt;
83. &lt;View
84. android:layout\_height="2dip"
85. android:background="#C4C4C4"/&gt;
86. &lt;TableRow&gt;
87. &lt;View
88. android:layout\_height="80dp"
89. android:layout\_width="250dp"
90. android:background="#D63C16"
91. /&gt;
92. &lt;TextView
93. android:layout\_height="80dp"
94. android:text="#D63C16"
95. android:gravity="center"/&gt;
96. &lt;/TableRow&gt;
97. &lt;/TableLayout&gt;
98. &lt;/ScrollView&gt;

```
<?xml version="1.0" encoding="utf-8"?>
<!--   
android:stretchColumns="1"是设置 TableLayout所有行的第二列为拉伸列。   
也就是说如果每行都有三列的话，剩余的空间由第二列补齐   
-->  
<!-- 最外层包裹一个滚动条 --> 
<ScrollView 
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content">
	<TableLayout 
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:stretchColumns="1">
	    <TableRow>
	        <View
	        	android:layout_height="80dp"
	        	android:layout_width="250dp"
	        	android:background="#D6D6D6"
	        />
	        <TextView 
	        	android:layout_height="80dp"
	        	android:text="#D6D6D6"
				android:gravity="center"/>
	    </TableRow>
	    <!-- 此处的View控件充当着一个分割条的作用 -->
		<View
			android:layout_height="2dip"
	        android:background="#C4C4C4" />
	    <TableRow>
	        <View
	        	android:layout_height="80dp"
	        	android:layout_width="250dp"
	        	android:background="#292929"
	        />
	        <TextView 
	        	android:layout_height="80dp"
	        	android:text="#292929"
				android:gravity="center"/>
	    </TableRow>
		<View
			android:layout_height="2dip"
	        android:background="#C4C4C4" />
	    <TableRow>
	        <View
	        	android:layout_height="80dp"
	        	android:layout_width="250dp"
	        	android:background="#8A500E"
	        />
	        <TextView 
	        	android:layout_height="80dp"
	        	android:text="#8A500E"
				android:gravity="center"/>
	    </TableRow>
		<View
			android:layout_height="2dip"
	        android:background="#C4C4C4" />
	    <TableRow>
	        <View
	        	android:layout_height="80dp"
	        	android:layout_width="250dp"
	        	android:background="#D67C15"
	        />
	        <TextView 
	        	android:layout_height="80dp"
	        	android:text="#D67C15"
				android:gravity="center"/>
	    </TableRow>
		<View
			android:layout_height="2dip"
	        android:background="#C4C4C4" />
	    <TableRow>
	        <View
	        	
	        	android:layout_height="80dp"
	        	android:layout_width="250dp"	
	        	android:background="#8A270E"
	        />
	        <TextView 
	        	android:layout_height="80dp"
	        	android:text="#8A270E"
				android:gravity="center"/>
	    </TableRow>
		<View
			android:layout_height="2dip"
	        android:background="#C4C4C4" />
	    <TableRow>
	        <View
	        	android:layout_height="80dp"
	        	android:layout_width="250dp"
	        	android:background="#D63C16"
	        />
	        <TextView 
	        	android:layout_height="80dp"
	        	android:text="#D63C16"
				android:gravity="center"/>
	    </TableRow>
	</TableLayout>
</ScrollView>
```

（4）FrameLayout：帧布局

最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域，之后你可以在其中填充一个单一对象—比如，一张你要发布的图片。所有的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。但是你可以通过子控件自身控制其位置。后一个子元素将会直接在前一个子元素之上进行覆盖填充，把它们部份或全部挡住(除非后一个子元素是透明的)。此布局通常用于游戏或者处理一些画廊程序。如图：

![](http://dl.iteye.com/upload/attachment/549881/e068d793-443e-3201-85a3-eed58e66d0b5.jpg)

![](http://dl.iteye.com/upload/attachment/549883/62950337-e77c-3d48-8a23-bab3027945be.jpg)

![](http://dl.iteye.com/upload/attachment/549885/8a21656c-a969-3509-874b-c67ca135ce24.jpg)  
&lt;?xml version="1.0" encoding="utf-8"?&gt;

Xml代码 [![复制代码](http://www.iteye.com/images/icon_copy.gif)](http://www.iteye.com/topic/1114402 "复制代码")[![收藏代码](http://www.iteye.com/images/icon_star.png)![](http://www.iteye.com/images/spinner.gif)]()

01. &lt;!--帧布局，所以子控件均显示在屏幕的左上角，层叠式排列。此布局无法控制子控件的大小与位置，
02. 但是子控件自身可以控制其位置大小--&gt;
03. &lt;FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"
04. android:orientation="vertical"
05. android:layout\_width="fill\_parent"
06. android:layout\_height="fill\_parent"
07. android:background="@drawable/bg"
08. &gt;
09. &lt;!--图片显示控件并且在容器的右侧显示--&gt;
10. &lt;ImageView
11. android:id="@+id/one\_imageview"
12. android:src="@drawable/one"
13. android:layout\_width="wrap\_content"
14. android:layout\_height="wrap\_content"
15. android:layout\_gravity="right"
16. /&gt;
17. &lt;!--第二张图片显示在左侧底部--&gt;
18. &lt;ImageView
19. android:id="@+id/two\_imageview"
20. android:src="@drawable/two"
21. android:layout\_width="wrap\_content"
22. android:layout\_height="fill\_parent"
23. android:scaleType="fitEnd"
24. /&gt;
25. &lt;/FrameLayout&gt;

```
<!-- 帧布局，所以子控件均显示在屏幕的左上角，层叠式排列。此布局无法控制子控件的大小与位置，
      但是子控件自身可以控制其位置大小 -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
    >
    <!-- 图片显示控件 并且在容器的右侧显示 -->
	<ImageView 
		android:id="@+id/one_imageview"
		android:src="@drawable/one"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_gravity="right"
		/>
	<!-- 第二张图片显示在左侧底部 -->
	<ImageView 
		android:id="@+id/two_imageview"
		android:src="@drawable/two"
		android:layout_width="wrap_content"
		android:layout_height="fill_parent"
		android:scaleType="fitEnd"
		/>
</FrameLayout>
```

package net.csdn.blog.androidtoast;

Java代码 [![复制代码](http://www.iteye.com/images/icon_copy.gif)](http://www.iteye.com/topic/1114402 "复制代码")[![收藏代码](http://www.iteye.com/images/icon_star.png)![](http://www.iteye.com/images/spinner.gif)]()

01. importandroid.app.Activity;
02. importandroid.os.Bundle;
03. importandroid.view.View;
04. importandroid.widget.ImageView;
05. publicclassMainActivityextendsActivity{
06. ImageViewmOneImageView;
07. ImageViewmTwoImageView;
08. /\*\*Calledwhentheactivityisfirstcreated.\*/
09. @Override
10. publicvoidonCreate(BundlesavedInstanceState){
11. super.onCreate(savedInstanceState);
12. setContentView(R.layout.main);
13. mOneImageView=(ImageView)findViewById(R.id.one\_imageview);
14. mTwoImageView=(ImageView)findViewById(R.id.two\_imageview);
15. //添加点击监听事件
16. mOneImageView.setOnClickListener(newImageView.OnClickListener(){
17. @Override
18. publicvoidonClick(Viewv){
19. //点击one时隐藏自身显示two
20. mTwoImageView.setVisibility(View.VISIBLE);
21. v.setVisibility(View.GONE);
22. }
23. });
24. mTwoImageView.setOnClickListener(newImageView.OnClickListener(){
25. @Override
26. publicvoidonClick(Viewv){
27. mOneImageView.setVisibility(View.VISIBLE);
28. v.setVisibility(View.GONE);
29. }
30. });
31. }
32. }

```
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {
	
	ImageView mOneImageView;
	ImageView mTwoImageView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
         mOneImageView=(ImageView) findViewById(R.id.one_imageview);
         mTwoImageView=(ImageView) findViewById(R.id.two_imageview);
        
         //添加点击监听事件
        mOneImageView.setOnClickListener(new ImageView.OnClickListener(){

			@Override
			public void onClick(View v) {
				//点击one时隐藏自身 显示two
				mTwoImageView.setVisibility(View.VISIBLE);
				v.setVisibility(View.GONE);
			}
        	
        });
        
        mTwoImageView.setOnClickListener(new ImageView.OnClickListener(){

			@Override
			public void onClick(View v) {
				
				mOneImageView.setVisibility(View.VISIBLE);
				v.setVisibility(View.GONE);
			}
        	
        });
    }
}
```

- [![](http://dl.iteye.com/upload/attachment/549862/673cc39b-ab85-32c1-9147-f3c83652df8b-thumb.jpg "点击查看原始大小图片")](http://dl.iteye.com/upload/attachment/549862/673cc39b-ab85-32c1-9147-f3c83652df8b.jpg)
- 大小: 47.1 KB

<!--THE END-->

- [![](http://dl.iteye.com/upload/attachment/549865/9513d52f-a05d-3d9b-9a18-ba1e6e76ae9a-thumb.jpg "点击查看原始大小图片")](http://dl.iteye.com/upload/attachment/549865/9513d52f-a05d-3d9b-9a18-ba1e6e76ae9a.jpg)
- 大小: 33.9 KB

<!--THE END-->

- [![](http://dl.iteye.com/upload/attachment/549867/2b730825-8ae5-35c1-80f3-4e3d1233dcc4-thumb.jpg "点击查看原始大小图片")](http://dl.iteye.com/upload/attachment/549867/2b730825-8ae5-35c1-80f3-4e3d1233dcc4.jpg)
- 大小: 41.4 KB

<!--THE END-->

- [![](http://dl.iteye.com/upload/attachment/549869/ceda3825-3b0d-3ce8-bb98-7f55237bb21e-thumb.jpg "点击查看原始大小图片")](http://dl.iteye.com/upload/attachment/549869/ceda3825-3b0d-3ce8-bb98-7f55237bb21e.jpg)
- 大小: 35.1 KB

<!--THE END-->

- [![](http://dl.iteye.com/upload/attachment/549871/8fe2f1f6-6970-34ed-b36e-49752c89709d-thumb.jpg "点击查看原始大小图片")](http://dl.iteye.com/upload/attachment/549871/8fe2f1f6-6970-34ed-b36e-49752c89709d.jpg)
- 大小: 26.1 KB

<!--THE END-->

- [![](http://dl.iteye.com/upload/attachment/549875/7464a61f-751b-3bdb-8ae4-3696249a5c2f-thumb.jpg "点击查看原始大小图片")](http://dl.iteye.com/upload/attachment/549875/7464a61f-751b-3bdb-8ae4-3696249a5c2f.jpg)
- 大小: 18.4 KB

<!--THE END-->

- [![](http://dl.iteye.com/upload/attachment/549881/e068d793-443e-3201-85a3-eed58e66d0b5-thumb.jpg "点击查看原始大小图片")](http://dl.iteye.com/upload/attachment/549881/e068d793-443e-3201-85a3-eed58e66d0b5.jpg)
- 大小: 40.6 KB

<!--THE END-->

- [![](http://dl.iteye.com/upload/attachment/549883/62950337-e77c-3d48-8a23-bab3027945be-thumb.jpg "点击查看原始大小图片")](http://dl.iteye.com/upload/attachment/549883/62950337-e77c-3d48-8a23-bab3027945be.jpg)
- 大小: 35.2 KB

<!--THE END-->

- [![](http://dl.iteye.com/upload/attachment/549885/8a21656c-a969-3509-874b-c67ca135ce24-thumb.jpg "点击查看原始大小图片")](http://dl.iteye.com/upload/attachment/549885/8a21656c-a969-3509-874b-c67ca135ce24.jpg)
- 大小: 35.6 KB

<!--THE END-->

- [LinearLayout.rar](http://dl.iteye.com/topics/download/f6e59eca-cb69-351c-b2f5-2ed40ef82b4e) (552.6 KB)
- 下载次数: 359

<!--THE END-->

- [RelativeLayout.rar](http://dl.iteye.com/topics/download/6dd40aa9-4669-3767-94b5-db3cfe05c544) (244.1 KB)
- 下载次数: 294

<!--THE END-->

- [TableLayout.rar](http://dl.iteye.com/topics/download/4468dbd8-3a69-32b1-8d40-a0eaa9d2c663) (39.9 KB)
- 下载次数: 286

<!--THE END-->

- [FrameLayout.rar](http://dl.iteye.com/topics/download/090bb50a-e31d-3685-ae31-18c82e1fe590) (585.9 KB)
- 下载次数: 316

<!--THE END-->

- [查看图片附件](http://www.iteye.com/topic/1114402)