[关闭]
@stepbystep 2015-01-27T21:37:21.000000Z 字数 22891 阅读 14447

Android UI框架和常用布局

Android

说明: 本节主要讲解了Android的UI框架View 和ViewGroup等, 同时讲解一些常用的布局,如LinearLayout,RelativeLayout等


课前复习
控件点击事件注册的4中方法:

  1. // Res/layout/xxx.xml
  2. // <Button
  3. // onClick="click"/>
  4. // 在Activity中写一个如下的方法: 函数名与之对应
  5. Public void click(View view){
  6. }
  1. // button 是查找后赋值的控件
  2. button.setOnClickListener(new OnClickListener(){
  3. // 重载函数
  4. Public void onClick(View view){
  5. }
  6. });
  1. // 在Activity类中再自定义一个MyOnClickListener类
  2. public class MainActivity extends Activity{
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. //---其他代码
  7. // button 是查找后赋值的控件
  8. button.setOnClickListener(new MyOnClickListener();
  9. }
  10. // 自定义内部类
  11. Private final class MyOnClickListener implements OnClickListener{
  12. Public void onClick(View view){
  13. }
  14. }
  15. }
  1. Public class MainActivity extends Activity implements OnClickListener{
  2. @Override
  3. Public void onClick(View view){
  4. ///
  5. }
  6. }

Android UI 框架

Android UI 框架的特点

View和ViewGroup

Android的UI 开发使用嵌套结构,在ViewGroup中嵌套ViewGroup,每一层有任意数目的View。如下图:
此处输入图片的描述

需要注意的是嵌套次数最好不要超过10层,否则会降低效率,上图是3层

View和ViewGroup的结构图
深红色<-蓝色<-粉色<-青色<-土黄色
此处输入图片的描述

常用的UI布局

布局定义方式

在layout目录下新建一个xml文件,按照布局格式布局。
xml中每个元素的是View或者ViewGroup的子孙类的对象。他就像一颗树一样,根节点只有一个ViewGroup,叶节点都是View对象,分支节点都是ViewGroup


常用布局属性

常用布局实验并讲解

首先新建LayoutDemo工程,并新建MainActivity和LayoutActivity。
工程文件结构最后大致如下图:

其中MainActivity为主界面,主要代码如下:

  1. package com.example.shiyanlou.layoutdemo;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. public class MainActivity extends Activity implements View.OnClickListener{
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. //注册点击事件
  12. findViewById(R.id.btn_linear).setOnClickListener(this);
  13. findViewById(R.id.btn_relative).setOnClickListener(this);
  14. findViewById(R.id.btn_table).setOnClickListener(this);
  15. findViewById(R.id.btn_frame).setOnClickListener(this);
  16. findViewById(R.id.btn_absolute).setOnClickListener(this);
  17. }
  18. @Override
  19. public void onClick(View v) {
  20. Intent intent = new Intent();
  21. intent.setClass(this, LayoutActivity.class);
  22. String layout_id = "layout_id";
  23. /**
  24. * 此处因为只需要展示每个布局文件,用不着新建多个Activity,只需要在LayoutActivity中的onCreate函数导航
  25. * 布局文件的时候通过intent.getIntent().getIntExtra("layout_id")的方法获取布局文件对应的id号。
  26. * 即可设置不同的布局文件
  27. */
  28. switch (v.getId()){
  29. case R.id.btn_linear:
  30. intent.putExtra(layout_id, R.layout.linear_layout);
  31. break;
  32. case R.id.btn_relative:
  33. intent.putExtra(layout_id, R.layout.relative_layout);
  34. break;
  35. case R.id.btn_table:
  36. intent.putExtra(layout_id, R.layout.table_layout);
  37. break;
  38. case R.id.btn_frame:
  39. intent.putExtra(layout_id, R.layout.frame_layout);
  40. break;
  41. case R.id.btn_absolute:
  42. intent.putExtra(layout_id, R.layout.absolute_layout);
  43. break;
  44. }
  45. startActivity(intent);
  46. }
  47. }

MainActivity的布局文件res\layout\activity_main.xml如下

  1. <!--这里用到了线性布局, 利用layout_weight="1"使得每个按钮均匀显示在手机中-->
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. android:paddingBottom="20dp"
  8. >
  9. <!--应用了style属性,因为这5个按钮除了显示的内容和id号不一样以外,其余属性基本上一样
  10. 此时就可以利用style属性,在res/values/styles.xml文件中添加一个style标签,加上属性内容
  11. 再在控件的属性中用style="@style/**"的方式,引用style
  12. 这样做的好处是当需要改变属性的时候,只要改变style文件中的内容,布局结果就会发生变化-->
  13. <Button
  14. android:id="@+id/btn_linear"
  15. style="@style/blue_btn_style"
  16. android:text="LinearLayout"
  17. />
  18. <Button
  19. android:id="@+id/btn_relative"
  20. style="@style/blue_btn_style"
  21. android:text="RelativeLayout"
  22. />
  23. <Button
  24. android:id="@+id/btn_table"
  25. style="@style/blue_btn_style"
  26. android:text="TableLayout"
  27. />
  28. <Button
  29. android:id="@+id/btn_frame"
  30. style="@style/blue_btn_style"
  31. android:text="FrameLayout"
  32. />
  33. <Button
  34. android:id="@+id/btn_absolute"
  35. style="@style/blue_btn_style"
  36. android:text="AbsoluteLayout"
  37. />
  38. </LinearLayout>

其中style="@style/blue_btn_style"引用的style文件内容如下,设置了Button的样式
res\values\styles.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <!-- Base application theme. -->
  4. <style name="AppTheme" parent="android:Theme.Holo.Light">
  5. <!-- Customize your theme here. -->
  6. </style>
  7. <!--蓝色按钮的背景style,将各种属性集一身,减少布局文件中重复的内容-->
  8. <style name="blue_btn_style">
  9. <item name="android:textSize">18sp</item>
  10. <item name="android:textColor">#fff</item>
  11. <item name="android:layout_height">match_parent</item>
  12. <item name="android:layout_width">match_parent</item>
  13. <!--设置了background为@drawable/selector_btn_blue
  14. 表示背景对应这样的一个drawable文件,实现了背景选择器-->
  15. <item name="android:background">@drawable/selector_btn_blue</item>
  16. <item name="android:layout_marginLeft">20dp</item>
  17. <item name="android:layout_marginRight">20dp</item>
  18. <item name="android:layout_marginTop">20dp</item>
  19. <item name="android:layout_weight">1</item>
  20. </style>
  21. </resources>

背景选择器的内容如下 res\drawable\selector_btn_blue.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <!--selector 背景选择器, 每一个item对应一种状态,从上至下扫描,如果满足则选择该item作为背景
  4. 主要用来处理按下的效果,获取获取焦掉与离开焦点的效果
  5. 正常情况下 state_pressed="false" 且 state_focused="false",此时应选择最后一个item(此item无特殊要求)
  6. 当控件被按下或者获取焦点的时候,就会匹配到第一个item,颜色就会变深,从而得到了按下的效果。
  7. 当然每个item可以有其他的属性,这里的radius就是圆角的意思。
  8. -->
  9. <item android:state_pressed="true"><shape>
  10. <corners android:radius="5dp"/>
  11. <solid android:color="#2988e4"></solid>
  12. </shape></item>
  13. <item android:state_focused="true"><shape>
  14. <corners android:radius="5dp"/>
  15. <solid android:color="#2988e4"></solid>
  16. </shape></item>
  17. <item><shape>
  18. <corners android:radius="5dp"/>
  19. <solid android:color="#4AA9EE"></solid>
  20. </shape></item>
  21. </selector>

进入界面效果如下:

点击按钮的按下效果如下: 可以看到颜色明显加深了

LayoutActivity为点击按钮进入的布局界面

  1. package com.example.shiyanlou.layoutdemo;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.Window;
  5. public class LayoutActivity extends Activity {
  6. private int layout_id;
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. // 根据Intent传递的数据,得到导航的布局id号。
  11. layout_id = getIntent().getIntExtra("layout_id", R.layout.linear_layout);
  12. // 设置全屏,需要在setContentView之前调用
  13. requestWindowFeature(Window.FEATURE_NO_TITLE);
  14. // 将layout_id设置为当前布局文件的id号
  15. setContentView(layout_id);
  16. }
  17. }

线性布局

线性布局知识点

线性布局实验

此处输入图片的描述

底部按钮按下效果如下: 可以看到颜色变成了深红色

res\layout\linear_layout.xml文件主要内容如下:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical"
  5. >
  6. <!--内层再使用两个线性布局,分别表示上层的水平方式,和下半部分的垂直方式-->
  7. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:layout_weight="2"
  11. android:padding="10dp"
  12. android:orientation="horizontal">
  13. <!--wrap_content和layout_weight一起使用时,控件所占比例跟layout_weight的值成正比-->
  14. <!--android:text="第\n一\n列" \n在这里用于换行显示内容-->
  15. <TextView
  16. android:layout_width="wrap_content"
  17. android:layout_height="match_parent"
  18. android:background="#FFDAB9"
  19. android:gravity="center_horizontal"
  20. android:textColor="#2a5caa"
  21. android:text="第\n一\n列"
  22. android:layout_weight="1"/>
  23. <TextView
  24. android:layout_width="wrap_content"
  25. android:layout_height="match_parent"
  26. android:background="#FF82AB"
  27. android:gravity="center_vertical|right"
  28. android:textColor="#2a5caa"
  29. android:text="第\n二\n列"
  30. android:layout_weight="2"/>
  31. <TextView
  32. android:layout_width="wrap_content"
  33. android:layout_height="match_parent"
  34. android:background="#EEEE00"
  35. android:gravity="bottom|center_horizontal"
  36. android:textColor="#2a5caa"
  37. android:text="第\n三\n列"
  38. android:layout_weight="3"/>
  39. <TextView
  40. android:layout_width="wrap_content"
  41. android:layout_height="match_parent"
  42. android:background="#ADFF2F"
  43. android:gravity="center"
  44. android:textColor="#2a5caa"
  45. android:text="第\n四\n列"
  46. android:layout_weight="4"/>
  47. </LinearLayout>
  48. <!--下面的所有Button使用了android:layout_weight="1"属性,
  49. 使得可以在下半部分均匀显示-->
  50. <LinearLayout
  51. android:layout_width="match_parent"
  52. android:layout_height="match_parent"
  53. android:layout_weight="3"
  54. android:paddingLeft="10dp"
  55. android:paddingRight="10dp"
  56. android:paddingBottom="10dp"
  57. android:orientation="vertical">
  58. <!--使用了背景选择器selector_btn_red-->
  59. <Button
  60. android:layout_width="match_parent"
  61. android:layout_height="match_parent"
  62. android:layout_weight="1"
  63. android:textColor="#fff"
  64. android:background="@drawable/selector_btn_red"
  65. android:text="第一行"/>
  66. <Button
  67. android:layout_marginTop="10dp"
  68. android:layout_width="match_parent"
  69. android:layout_height="match_parent"
  70. android:layout_weight="1"
  71. android:textColor="#fff"
  72. android:background="@drawable/selector_btn_red"
  73. android:text="第二行"/>
  74. <Button
  75. android:layout_marginTop="10dp"
  76. android:layout_width="match_parent"
  77. android:layout_height="match_parent"
  78. android:layout_weight="1"
  79. android:textColor="#fff"
  80. android:background="@drawable/selector_btn_red"
  81. android:text="第三行"/>
  82. </LinearLayout>
  83. </LinearLayout>

这里又用到了背景选择器,直接在控件的backgroud设置,注意,引用背景选择器,需要用@drawable方式。红色背景选择器如下,几乎同蓝色selector_btn_blue.xml一样。只不过换了颜色
res/drawable/selector_btn_red.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_pressed="true"><shape>
  4. <corners android:radius="5dp"/>
  5. <solid android:color="#FF1120"></solid>
  6. </shape></item>
  7. <item android:state_focused="true"><shape>
  8. <corners android:radius="5dp"/>
  9. <solid android:color="#FF1120"></solid>
  10. </shape></item>
  11. <item><shape>
  12. <corners android:radius="5dp"/>
  13. <solid android:color="#FF6B6B"></solid>
  14. </shape></item>
  15. </selector>

相对布局

相对布局知识点

相对布局四眼
本实验中相对布局以英雄联盟的英雄介绍为目的,制作了一个简单的布局界面。
用到了如下的图片资源,可以点击右键另存为后作为实验。


布局效果如下:

布局文件res\layout\relative_layout.xml文件如下

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#FAEBD7"
  6. >
  7. <!--内层相对布局,默认暂居屏幕顶端-->
  8. <RelativeLayout
  9. android:id="@+id/top_info"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:background="#ACE383">
  13. <ImageView
  14. android:id="@+id/txt_img"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:src="@drawable/manwang"
  18. />
  19. <!--该控件以左边的ImageView为标识
  20. android:layout_toRightOf="@id/txt_img"表示居其右-->
  21. <TextView
  22. android:id="@+id/txt_name"
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:text="蛮族之王"
  26. android:textColor="#436EEE"
  27. android:textSize="22sp"
  28. android:layout_toRightOf="@id/txt_img"
  29. android:layout_marginLeft="10dp"
  30. android:layout_marginTop="5dp"/>
  31. <!--该控件以上边的TextView为标识
  32. android:layout_below="@id/txt_name"表示位其下
  33. android:layout_alignLeft="@id/txt_name"表示左边界相同-->
  34. <LinearLayout
  35. android:layout_below="@id/txt_name"
  36. android:layout_alignLeft="@id/txt_name"
  37. android:layout_marginTop="5dp"
  38. android:layout_width="match_parent"
  39. android:layout_height="wrap_content">
  40. <TextView
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:text="近战"
  44. android:padding="3dp"
  45. android:textSize="12sp"
  46. android:textColor="#fff"
  47. android:background="#FF7A01"
  48. />
  49. <TextView
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:text="上单"
  53. android:padding="3dp"
  54. android:textSize="12sp"
  55. android:textColor="#fff"
  56. android:layout_marginLeft="5dp"
  57. android:background="#FF7A01"
  58. />
  59. <TextView
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:text="打野"
  63. android:padding="3dp"
  64. android:textSize="12sp"
  65. android:textColor="#fff"
  66. android:layout_marginLeft="5dp"
  67. android:background="#FF7A01"
  68. />
  69. </LinearLayout>
  70. <!--android:layout_alignLeft="@id/txt_name"
  71. android:layout_alignBottom="@id/txt_img"
  72. 表示了该控件首先跟顶部的TextView沿着同一个左边界
  73. 同时也与左边的图片沿着同一个底边界-->
  74. <TextView
  75. android:layout_width="match_parent"
  76. android:layout_height="wrap_content"
  77. android:textColor="#8B3A3A"
  78. android:textSize="14sp"
  79. android:layout_alignLeft="@id/txt_name"
  80. android:layout_alignBottom="@id/txt_img"
  81. android:layout_marginBottom="5dp"
  82. android:text="蛮族之王,别名蛮子,泰达米尔,五秒男,蛮王。灵活性强,可以自由穿越地形切入战场,后期能力非常强."
  83. />
  84. </RelativeLayout>
  85. <!--android:layout_alignParentBottom="true"属性说明了该控件居于父控件的底部-->
  86. <Button
  87. android:id="@+id/btn_focus_vedio"
  88. android:layout_width="match_parent"
  89. android:layout_height="wrap_content"
  90. android:layout_weight="1"
  91. android:textColor="#fff"
  92. android:background="#B22222"
  93. android:layout_alignParentBottom="true"
  94. android:text="英雄视频"/>
  95. <!--android:layout_above="@+id/btn_focus_vedio"属性说明了该控件居于Button的上方
  96. 注意,此处需要先布局Button,再根据其id布局下面的内容,尽管Button显示在最底部-->
  97. <RelativeLayout
  98. android:id="@+id/layout_q_info"
  99. android:layout_width="match_parent"
  100. android:layout_height="wrap_content"
  101. android:layout_above="@+id/btn_focus_vedio"
  102. android:padding="10dp">
  103. <!--android:layout_centerVertical="true"
  104. 表示了该控件始终位于父控件的垂直中部-->
  105. <ImageView
  106. android:id="@+id/img_q"
  107. android:layout_width="80dp"
  108. android:layout_height="80dp"
  109. android:layout_centerVertical="true"
  110. android:src="@drawable/manwang_q"
  111. />
  112. <TextView
  113. android:id="@+id/txt_q_name"
  114. android:layout_width="match_parent"
  115. android:layout_height="wrap_content"
  116. android:layout_toRightOf="@+id/img_q"
  117. android:layout_marginLeft="10dp"
  118. android:textColor="#9B30FF"
  119. android:textSize="18sp"
  120. android:text="嗜血杀戮【快捷键Q】"/>
  121. <TextView
  122. android:id="@+id/txt_q_info"
  123. android:layout_width="match_parent"
  124. android:layout_height="wrap_content"
  125. android:text="泰达米尔对战斗极度饥渴,他受伤程度越高,攻击力越强。他能通过释放嗜血杀戮消耗怒气并治疗自己。"
  126. android:layout_alignLeft="@+id/txt_q_name"
  127. android:layout_below="@+id/txt_q_name"
  128. android:textSize="12sp"
  129. android:textColor="#595959"
  130. android:layout_marginTop="5dp"
  131. />
  132. <RelativeLayout
  133. android:layout_width="match_parent"
  134. android:layout_height="wrap_content"
  135. android:layout_alignLeft="@+id/txt_q_name"
  136. android:layout_below="@+id/txt_q_info"
  137. android:layout_marginTop="15dp"
  138. >
  139. <TextView
  140. android:id="@+id/txt_first"
  141. android:layout_width="wrap_content"
  142. android:layout_height="wrap_content"
  143. android:text="冷却"
  144. android:textSize="12sp"
  145. android:textColor="#FF7A01"
  146. />
  147. <TextView
  148. android:layout_width="wrap_content"
  149. android:layout_height="wrap_content"
  150. android:text="12/12/12/12/12"
  151. android:textSize="12sp"
  152. android:textColor="#595959"
  153. android:layout_toRightOf="@+id/txt_first"
  154. android:layout_alignTop="@+id/txt_first"
  155. android:layout_marginLeft="10dp"
  156. />
  157. <TextView
  158. android:id="@+id/txt_second"
  159. android:layout_width="wrap_content"
  160. android:layout_height="wrap_content"
  161. android:text="射程"
  162. android:layout_below="@+id/txt_first"
  163. android:layout_marginTop="10dp"
  164. android:textSize="12sp"
  165. android:textColor="#FF7A01"
  166. />
  167. <TextView
  168. android:layout_width="wrap_content"
  169. android:layout_height="wrap_content"
  170. android:text="318"
  171. android:textSize="12sp"
  172. android:textColor="#595959"
  173. android:layout_toRightOf="@+id/txt_second"
  174. android:layout_alignTop="@+id/txt_second"
  175. android:layout_marginLeft="10dp"
  176. />
  177. <TextView
  178. android:id="@+id/txt_third"
  179. android:layout_width="wrap_content"
  180. android:layout_height="wrap_content"
  181. android:text="效果"
  182. android:layout_below="@+id/txt_second"
  183. android:layout_marginTop="10dp"
  184. android:textSize="12sp"
  185. android:textColor="#FF7A01"
  186. />
  187. <TextView
  188. android:layout_width="wrap_content"
  189. android:layout_height="wrap_content"
  190. android:text="被动:泰达米尔嗜血成性,获得5/10/15/20/25攻击力,每损失1%生命值额外增加0.15/0.2/0.25/0.3/0.35攻击力。泰达米尔消耗怒气,回复30/40/50/60/70(+0.3AP)。"
  191. android:textSize="12sp"
  192. android:textColor="#595959"
  193. android:layout_toRightOf="@+id/txt_third"
  194. android:layout_alignTop="@+id/txt_third"
  195. android:layout_marginLeft="10dp"
  196. />
  197. </RelativeLayout>
  198. </RelativeLayout>
  199. <!--此空间最后布局,
  200. android:layout_below="@id/top_info"
  201. android:layout_above="@id/layout_q_info"
  202. 两个属性使得该ImageView显示在两个id对应的控件之间-->
  203. <ImageView
  204. android:layout_width="match_parent"
  205. android:layout_height="wrap_content"
  206. android:scaleType="centerInside"
  207. android:layout_below="@id/top_info"
  208. android:layout_above="@id/layout_q_info"
  209. android:src="@drawable/manwang_chuzhuang"/>
  210. </RelativeLayout>

表格布局

说明,本表格布局的内容主要参考了http://blog.sina.com.cn/s/blog_63c66eb60100u29p.html网址的内容,故在此说明。
表格布局知识点
Tablelayout简介

Tablelayout类以行和列的形式对控件进行管理,每一行为一个TableRow对象,或一个View控件。 当为TableRow对象时,可在TableRow下添加子控件,默认情况下,每个子控件占据一列。当为View时,该View将独占一行。

TableLayout行列数的确定

TableLayout的行数由开发人员直接指定,即有多少个TableRow对象(或View控件),就有多少行。 TableLayout的列数等于含有最多子控件的TableRow的列数。如第一TableRow含2个子控件,第二个TableRow含3个,第三个TableRow含4个,那么该TableLayout的列数为4.

TableLayout可设置的属性详解
TableLayout可设置的属性包括全局属性及单元格属性。
全局属性也即列属性,有以下3个参数

说明:列可以同时具备stretchColumns及shrinkColumns属性,若此,那么当该列的内容N多时,将“多行”显示其内容。(这里不是真正的多行,而是系统根据需要自动调节该行的layout_height)

单元格属性,有以下2个参数
- android:layout_column 指定该单元格在第几列显示
- android:layout_span 指定该单元格占据的列数(未指定时,为1)
说明:一个控件也可以同时具备这两个特性。

表格布局实验内容
布局效果如图:

res/layout/table_layout.xml文件如下

  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. android:padding="3dip"
  7. >
  8. <!-- 第1个TableLayout,用于描述表中的列属性。第0列可伸展,第1列可收缩,第2列被隐藏-->
  9. <TextView
  10. android:text="表1:全局设置:列属性设置"
  11. android:layout_height="wrap_content"
  12. android:layout_width="wrap_content"
  13. android:textSize="15sp"
  14. android:background="#7f00ffff"/>
  15. <TableLayout
  16. android:id="@+id/table1"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:stretchColumns="0"
  20. android:shrinkColumns="1"
  21. android:collapseColumns="2"
  22. android:padding="3dip">
  23. <TableRow>
  24. <Button android:text="该列可伸展"/>
  25. <Button android:text="该列可收缩"/>
  26. <Button android:text="我被隐藏了"/>
  27. </TableRow>
  28. <TableRow>
  29. <TextView android:text="我向行方向伸展,我可以很长 "/>
  30. <TextView android:text="我向列方向收缩,我可以很深 "/>
  31. </TableRow>
  32. </TableLayout>
  33. <!-- 第2个TableLayout,用于描述表中单元格的属性,包括:android:layout_column 及android:layout_span-->
  34. <TextView
  35. android:text="表2:单元格设置:指定单元格属性设置"
  36. android:layout_height="wrap_content"
  37. android:layout_width="wrap_content"
  38. android:textSize="15sp"
  39. android:background="#7f00ffff"/>
  40. <TableLayout
  41. android:id="@+id/table2"
  42. android:layout_width="fill_parent"
  43. android:layout_height="wrap_content"
  44. android:padding="3dip">
  45. <TableRow>
  46. <Button android:text="第0列"/>
  47. <Button android:text="第1列"/>
  48. <Button android:text="第2列"/>
  49. </TableRow>
  50. <TableRow>
  51. <TextView android:text="我被指定在第1列"
  52. android:layout_column="1"/>
  53. </TableRow>
  54. <TableRow>
  55. <TextView
  56. android:text="我跨1到2列,不信你看!"
  57. android:layout_column="1"
  58. android:layout_span="2"
  59. />
  60. </TableRow>
  61. </TableLayout>
  62. <!-- 第3个TableLayout,使用可伸展特性布局-->
  63. <TextView
  64. android:text="表3:应用一,非均匀布局"
  65. android:layout_height="wrap_content"
  66. android:layout_width="wrap_content"
  67. android:textSize="15sp"
  68. android:background="#7f00ffff"/>
  69. <TableLayout
  70. android:id="@+id/table3"
  71. android:layout_width="fill_parent"
  72. android:layout_height="wrap_content"
  73. android:stretchColumns="*"
  74. android:padding="3dip"
  75. >
  76. <TableRow>
  77. <Button android:text="一" ></Button>
  78. <Button android:text="两字"></Button>
  79. <Button android:text="三个字" ></Button>
  80. </TableRow>
  81. </TableLayout>
  82. <!-- 第4个TableLayout,使用可伸展特性,并指定每个控件宽度一致,如1dip-->
  83. <TextView
  84. android:text="表4:应用二,均匀布局"
  85. android:layout_height="wrap_content"
  86. android:layout_width="wrap_content"
  87. android:textSize="15sp"
  88. android:background="#7f00ffff"/>
  89. <TableLayout
  90. android:id="@+id/table4"
  91. android:layout_width="fill_parent"
  92. android:layout_height="wrap_content"
  93. android:stretchColumns="*"
  94. android:padding="3dip"
  95. >
  96. <TableRow>
  97. <Button android:text="一" android:layout_width="1dip"></Button>
  98. <Button android:text="两字" android:layout_width="1dip"></Button>
  99. <Button android:text="三个字" android:layout_width="1dip"></Button>
  100. </TableRow>
  101. </TableLayout>
  102. </LinearLayout>

帧布局实验

效果如下:

res/layout/frame_layout.xml文件内容如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <!--FrameLayout中,常用于控件遮掩,比如看视频的时候,按暂停键,
  7. 弹出的控件就浮在播放内容的中间部位,此例中,控件在代码最后面的,展示在最前面-->
  8. <TextView
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:background="#FFE7BA"
  12. android:text="第零层"
  13. android:textSize="20sp"
  14. android:gravity="center_horizontal"
  15. />
  16. <TextView
  17. android:layout_width="300dp"
  18. android:layout_height="300dp"
  19. android:layout_gravity="center"
  20. android:background="#FFF68F"
  21. android:text="第一层"
  22. android:textSize="20sp"
  23. android:gravity="center_horizontal"
  24. />
  25. <!--可以看到第二层遮盖了第一层和第零层,却被第三层遮盖-->
  26. <TextView
  27. android:layout_width="200dp"
  28. android:layout_height="200dp"
  29. android:layout_gravity="center_vertical|right"
  30. android:background="#FFBBFF"
  31. android:text="第二层"
  32. android:textSize="20sp"
  33. android:gravity="center_horizontal"
  34. />
  35. <TextView
  36. android:layout_width="100dp"
  37. android:layout_height="100dp"
  38. android:layout_gravity="center"
  39. android:background="#FF4040"
  40. android:text="第三层"
  41. android:textSize="20sp"
  42. android:gravity="center_horizontal"
  43. />
  44. </FrameLayout>

绝对布局实验

效果如下:

res/layout/absolute_layout.xml文件内容如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#F0F8FF" >
  6. <!--绝对布局基本上不会用,因为对于设备的兼容性不好,
  7. 两部分辨率不同的手机,展示结果可能差别很大-->
  8. <!-- 定义一个文本框,使用绝对定位 -->
  9. <TextView
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_x="20dip"
  13. android:layout_y="20dip"
  14. android:text="用户名:" />
  15. <!-- 定义一个文本编辑框,使用绝对定位 -->
  16. <EditText
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_x="80dip"
  20. android:layout_y="15dip"
  21. android:width="200px" />
  22. <!-- 定义一个文本框,使用绝对定位 -->
  23. <TextView
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_x="20dip"
  27. android:layout_y="80dip"
  28. android:text="密 码:" />
  29. <!-- 定义一个文本编辑框,使用绝对定位 -->
  30. <EditText
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:layout_x="80dip"
  34. android:layout_y="75dip"
  35. android:password="true"
  36. android:width="200px" />
  37. <!-- 定义一个按钮,使用绝对定位 -->
  38. <Button
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:layout_x="130dip"
  42. android:layout_y="135dip"
  43. android:text="登 录" />
  44. </AbsoluteLayout>

课后实验

了解View和ViewGroup的继承关系

属性5大布局的特有属性,并练习一些常见的布局,思考怎么实现

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注