Android 的几种布局方式及实践

fmms 13年前
     <div id="cnblogs_post_body">     <p>我们对Android应用程序运行原理及布局文件可谓有了比较深刻的认识和理解,并且用“Hello World!”程序来实践证明了。在继续深入<a href="/misc/goto?guid=4959518081901249118" target="_blank">Android开发之旅</a>之前,有必要解决前两篇中没有介绍的遗留问题:View的几种布局显示方法,以后就不会在针对布局方面做过多的介绍。View的布局显示方式有下面几种:<b>线性布局</b>(Linear Layout)、<b>相对布局</b>(Relative Layout)、<b>表格布局</b>(Table Layout)、<b>网格视图</b>(Grid View)、<b>标签布局</b>(Tab Layout)、<b>列表视图</b>(List View)、<b>绝对布局</b>(AbsoluteLayout)。本文虽然是介绍View的布局方式,但不仅仅是这样,其中涉及了很多小的知识点,绝对能给你带来Android大餐!</p>     <p>本文的主要内容就是分别介绍以上视图的七种布局显示方式效果及实现,大纲如下:</p>     <ul>      <li>1、View布局概述 </li>      <li>2、线性布局(Linear Layout)       <ul>        <li>2.1、Tips:<span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_weight</span>=<span style="color:#0000ff;">"1"</span> </li>       </ul> </li>      <li>3、相对布局(Relative Layout) </li>      <li>4、表格布局(Table Layout) </li>      <li>5、列表视图(List View)       <ul>        <li>5.1、一个小的改进 </li>        <li>5.2、补充说明 </li>       </ul> </li>      <li>6、网格视图(Grid View)</li>      <li>7 、绝对布局()</li>      <li>8、标签布局(Tab Layout) </li>     </ul>     <h4><b>1、view的布局显示概述</b></h4>     <p>通过前面的学习我们知道:在一个Android应用程序中,用户界面通过<span style="color:#ff8040;">View</span>和<span style="color:#ff8040;">ViewGroup</span>对象构建。Android中有很多种View和ViewGroup,他们都继承自<span style="color:#ff8040;">View</span>类。View对象是Android平台上表示用户界面的基本单元。</p>     <p>View的布局显示方式直接影响用户界面,View的布局方式是指一组View元素如何布局,准确的说是一个ViewGroup中包含的一些View怎么样布局。<a href="/misc/goto?guid=4959518081986583239" target="_blank">ViewGroup</a>类是布局(layout)和视图容器(View container)的基类,此类也定义了<code><a href="/misc/goto?guid=4959518082066370791"><span style="font-family:Georgia;">ViewGroup.LayoutParams</span></a>类,它作为布局参数的基类,此类告诉父视图其中的子视图想如何显示。例如,XML布局文件中名为<span style="color:#ff8040;"><b>layout_<i>something</i></b></span>的属性(参加<a href="/misc/goto?guid=4959518082143904539" target="_blank">上篇的4.2节</a>)。我们要介绍的<span style="font-family:Georgia;">View</span>的布局方式的类,都是直接或间接继承自<a href="/misc/goto?guid=4959518081986583239" target="_blank">ViewGroup</a>类,如下图所示:</code></p>     <p style="text-align:center;"><code><a href="https://simg.open-open.com/show/a4eae778c62ec09dea75237c545e92c3.jpg"><img title="ViewGroup的继承" border="0" alt="ViewGroup的继承" src="https://simg.open-open.com/show/277558d3be987f3f59aa50c549944d4c.jpg" width="555" height="354" /></a> </code></p>     <p align="center"><code>图1、继承自ViewGroup的一些布局类</code></p>     <p><code></code>其实,所有的布局方式都可以归类为ViewGroup的5个类别,即ViewGroup的5个直接子类。其它的一些布局都扩展自这5个类。下面分小节分别介绍View的七种布局显示方式。</p>     <h4><b>2、线性布局(Linear Layout)</b></h4>     <p><b>线性布局</b>:是一个ViewGroup以线性方向显示它的子视图(view)元素,即<b>垂直地</b>或<b>水平地</b>。之前我们的Hello World!程序中view的布局方式就是线性布局的,一定不陌生!如下所示<span style="color:#ff8040;">res/layour/main.xml</span>: <br /> <br /> <span style="color:#0000ff;"><?</span>xml version="1.0" encoding="utf-8"<span style="color:#0000ff;">?></span> <br /> <b><span style="color:#0000ff;"><</span><span style="color:#800000;">LinearLayout</span></b> <span style="color:#ff0000;">xmlns</span>:<span style="color:#ff0000;">android</span>=<span style="color:#0000ff;">"http://schemas.android.com/apk/res/android"</span> <br />               <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"fill_parent"</span> <br />               <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"fill_parent"</span> <br />               <b><span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">orientation</span>=<span style="color:#0000ff;">"horizontal"</span><span style="color:#0000ff;">><!-- have an eye on ! --></span></b> <br />     <span style="color:#0000ff;"><</span><span style="color:#800000;">Button</span> <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">id</span>=<span style="color:#0000ff;">"@+id/button1"</span> <br />             <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />             <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"wrap_content"</span>            <br />             <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">text</span>=<span style="color:#0000ff;">"Hello, I am a Button1"</span> <br />             <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_weight</span>=<span style="color:#0000ff;">"1"</span> <br />             <span style="color:#0000ff;">/></span> <br />     <span style="color:#0000ff;"><</span><span style="color:#800000;">Button</span> <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">id</span>=<span style="color:#0000ff;">"@+id/button2"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">text</span>=<span style="color:#0000ff;">"Hello, I am a Button2"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_weight</span>=<span style="color:#0000ff;">"1"</span> <br />     <span style="color:#0000ff;">/></span> <br />     <span style="color:#0000ff;"><</span><span style="color:#800000;">Button</span> <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">id</span>=<span style="color:#0000ff;">"@+id/button3"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">text</span>=<span style="color:#0000ff;">"Hello, I am a Button3"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_weight</span>=<span style="color:#0000ff;">"1"</span> <br />     <span style="color:#0000ff;">/></span> <br />     <span style="color:#0000ff;"><</span><span style="color:#800000;">Button</span> <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">id</span>=<span style="color:#0000ff;">"@+id/button4"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">text</span>=<span style="color:#0000ff;">"Hello, I am a Button4"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_weight</span>=<span style="color:#0000ff;">"1"</span> <br />     <span style="color:#0000ff;">/></span> <br />     <span style="color:#0000ff;"><</span><span style="color:#800000;">Button</span> <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">id</span>=<span style="color:#0000ff;">"@+id/button5"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">text</span>=<span style="color:#0000ff;">"Hello, I am a Button5"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_weight</span>=<span style="color:#0000ff;">"1"</span> <br />     <span style="color:#0000ff;">/></span> <br /> <b><span style="color:#0000ff;"></</span><span style="color:#800000;">LinearLayout</span><span style="color:#0000ff;">></span></b> </p>     <p>从上面可以看出根LinearLayout视图组(ViewGroup)包含5个Button,它的子元素是以线性方式(horizontal,水平的)布局,运行效果如下图所示:</p>     <p align="center"><a href="https://simg.open-open.com/show/c77c25c66d7da3c9ded83d7a7e3032b1.png"><img title="linearlayout1" border="0" alt="linearlayout1" src="https://simg.open-open.com/show/aded303f4d8abadde5669994c4f345fd.png" width="381" height="195" /></a></p>     <p align="center">图2、线性布局(水平或者说是横向)</p>     <p> </p>     <p> </p>     <p>如果你在<b><span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">orientation</span>=<span style="color:#0000ff;">"horizontal"</span></b>设置为<span style="color:#0000ff;"><b>vertical</b></span>,则是是垂直或者说是纵向的,如下图所示:</p>     <p align="center"><a href="https://simg.open-open.com/show/1c8f6f679561cbb703cd3bc7be7e8f2d.png"><img title="linearlayout2" border="0" alt="linearlayout2" src="https://simg.open-open.com/show/75d8ebede24637d1b5b48e0ff5bb00eb.png" width="364" height="545" /></a></p>     <p align="center"> </p>     <p align="center">图3、线性布局(垂直或者说是纵向)</p>     <h4><b>2.1、Tips:<span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_weight</span>=<span style="color:#0000ff;">"1"</span></b> </h4>     <p><b> </b>这个属性很关键,如果你没有显示设置它,它默认为0。把上面布局文件(<i>水平显示的那个</i>)中的这个属性都去掉,运行会得出如下结果:</p>     <p align="center"><a href="https://simg.open-open.com/show/3ec7edfaab85af45806b1c23fe60469a.png"><img title="linearlayout3" border="0" alt="linearlayout3" src="https://simg.open-open.com/show/af6a4140d0f4639e08dc8d7535dc7059.png" width="377" height="136" /></a></p>     <p align="center">图4、layout_weight属性 </p>     <blockquote>      <p><b>没有了这个属性,我们本来定义的5个Button运行后却只显示了2个Button,为什么呢??</b></p>      <p><span style="color:#0000ff;">"</span>weight<span style="color:#0000ff;">"</span>顾名思义是<b>权重</b>的意思,layout_weight 用于给一个线性布局中的诸多视图的重要程度赋值。所有的视图都有一个layout_weight值,默认为零,意思是需要显示多大的视图就占据多大的屏幕空间。这就不难解释为什么会造成上面的情况了:Button1~Button5都设置了layout_height和layout_width属性为wrap_content即包住文字内容,他们都没有设置layout_weight 属性,即默认为0.,这样Button1和Button2根据需要的内容占据了整个屏幕,别的就显示不了啦!</p>      <p>若赋一个高于零的值,则将父视图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight值以及该值在当前屏幕布局的整体layout_weight值和在其它视图屏幕布局的layout_weight值中所占的比率而定。举个例子:比如说我们在 水平方向上有一个文本标签和两个文本编辑元素。该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个文本编辑元素其中第一个的layout_weight值设置为1,而第二个的设置为2,则剩余空间的三分之二分给第一个,三分之一分给第二个(<b>数值越小,重要度越高</b>)。  </p>     </blockquote>     <h4><b>3、相对布局(Relative Layout)</b></h4>     <p><b>相对布局</b>:是一个ViewGroup以相对位置显示它的子视图(view)元素,一个视图可以指定相对于它的兄弟视图的位置(例如在给定视图的左边或者下面)或相对于<code><a href="/misc/goto?guid=4959518082275604178"><span style="font-family:Georgia;">RelativeLayout</span></a>的特定区域的位置(例如底部对齐,或中间偏左)。</code></p>     <p><code>相对布局是设计用户界面的有力工具,因为它消除了嵌套视图组。如果你发现你使用了多个嵌套的<code><a href="/misc/goto?guid=4959518082364504439"><span style="font-family:Georgia;">LinearLayout</span></a></code>视图组后,你可以考虑使用一个<code><a href="/misc/goto?guid=4959518082275604178"><span style="font-family:Georgia;">RelativeLayout</span></a></code>视图组了。看下面的<span style="color:#ff8040;">res/layour/main.xml</span>:</code> <br /> <br /> <span style="color:#0000ff;"><?</span>xml version="1.0" encoding="utf-8"<span style="color:#0000ff;">?></span> <br /> <b><span style="color:#0000ff;"><</span><span style="color:#800000;">RelativeLayout</span></b> <span style="color:#ff0000;">xmlns</span>:<span style="color:#ff0000;">android</span>=<span style="color:#0000ff;">"http://schemas.android.com/apk/res/android"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"fill_parent"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"fill_parent"</span><span style="color:#0000ff;">></span> <br />     <span style="color:#0000ff;"><</span><span style="color:#800000;">TextView</span> <br />         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">id</span>=<span style="color:#0000ff;">"@+id/label"</span> <br />         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"fill_parent"</span> <br />         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">text</span>=<span style="color:#0000ff;">"Type here:"</span><span style="color:#0000ff;">/></span> <br />     <span style="color:#0000ff;"><</span><span style="color:#800000;">EditText</span> <br />         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">id</span>=<span style="color:#0000ff;">"@+id/entry"</span> <br />         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"fill_parent"</span> <br />         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">background</span>=<span style="color:#0000ff;">"@android:drawable/editbox_background"</span> <br />         <b><span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_below</span>=<span style="color:#0000ff;">"@id/label"</span><span style="color:#0000ff;">/><!-- have an eye on ! --></span> <br /> </b>    <span style="color:#0000ff;"><</span><span style="color:#800000;">Button</span> <br />         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">id</span>=<span style="color:#0000ff;">"@+id/ok"</span> <br />         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />         <b><span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_below</span>=<span style="color:#0000ff;">"@id/entry"  <!-- have an eye on ! --></span></b> <br />        <b><span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_alignParentRight</span>=<span style="color:#0000ff;">"true" <!-- have an eye on ! --></span> <br /> </b>        <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_marginLeft</span>=<span style="color:#0000ff;">"10dip"</span> <br />         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">text</span>=<span style="color:#0000ff;">"OK"</span> <span style="color:#0000ff;">/></span> <br />     <span style="color:#0000ff;"><</span><span style="color:#800000;">Button</span> <br />         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"wrap_content"</span> <br /> <b>        <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_toLeftOf</span>=<span style="color:#0000ff;">"@id/ok" <!-- have an eye on ! --></span> <br />         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_alignTop</span>=<span style="color:#0000ff;">"@id/ok" <!-- have an eye on ! --></span></b> <br />         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">text</span>=<span style="color:#0000ff;">"Cancel"</span> <span style="color:#0000ff;">/></span> <br /> <b><span style="color:#0000ff;"></</span><span style="color:#800000;">RelativeLayout</span><span style="color:#0000ff;">></span> <br /> </b><br /> <span style="font-family:Courier New;">从上面的布局文件我们知道,<b><span style="color:#800000;"><span style="font-family:Georgia;">RelativeLayout</span></span></b>视图组包含一个TextView、一个EditView、两个Button,注意标记了<span style="font-family:Georgia;color:#0000ff;"><!-- have an eye on ! -->(请注意运行代码的时候,请把这些注释去掉,否则会运行出错,上面加上是为了更加醒目!)</span>的属性,在使用<b>相对布局</b>方式中就是使用这些类似的属性来定位视图到你想要的位置,它们的值是你参照的视图的<b>id</b>。这些属性的意思很简单,就是英文单词的直译,就不多做介绍了。运行之后,得如下结果:</span></p>     <p align="center"><a href="https://simg.open-open.com/show/1cc5541f9e21b8e9f4f7b8b555e4e1e7.png"><img title="relativelayout" border="0" alt="relativelayout" src="https://simg.open-open.com/show/0e93c1be006b7febb52db2d0b2668bfe.png" width="382" height="217" /></a></p>     <p align="center">图5、相对布局</p>     <h4><b>4、 表格布局(Table Layout)</b></h4>     <p><b>表格布局</b>:是一个ViewGroup以表格显示它的子视图(view)元素,即行和列标识一个视图的位置。其实Android的表格布局跟HTML中的表格布局非常类似,<code><a href="/misc/goto?guid=4959518082464605810">TableRow</a></code> 就像HTML表格的<<code>tr>标记。</code> </p>     <p><b>用表格布局需要知道以下几点</b>:</p>     <ul>      <li><a href="/misc/goto?guid=4959518082535364336">android:shrinkColumns</a>,对应的方法:<a href="/misc/goto?guid=4959518082624006451">setShrinkAllColumns(boolean)</a>,作用:设置表格的列是否收缩(列编号从0开始,下同),多列用逗号隔开(下同),如<span style="color:#0000ff;">android:shrinkColumns="0,1,2"</span>,即表格的第1、2、3列的内容是收缩的以适合屏幕,不会挤出屏幕。 </li>      <li><a href="/misc/goto?guid=4959518082693539181">android:collapseColumns</a>,对应的方法:<a href="/misc/goto?guid=4959518082778149019">setColumnCollapsed(int,boolean)</a>,作用:设置表格的列是否隐藏 </li>      <li><a href="/misc/goto?guid=4959518082863717446">android:stretchColumns</a>,对应的方法:<a href="/misc/goto?guid=4959518082957394893">setStretchAllColumns(boolean)</a>,作用:设置表格的列是否拉伸 </li>     </ul>     <p><code>看下面的<span style="color:#ff8040;">res/layour/main.xml</span>:</code> <br /> <br /> <span style="color:#0000ff;"><?</span>xml version="1.0" encoding="utf-8"<span style="color:#0000ff;">?></span> <br /> <b><span style="color:#0000ff;"><</span><span style="color:#800000;">TableLayout</span></b> <span style="color:#ff0000;">xmlns</span>:<span style="color:#ff0000;">android</span>=<span style="color:#0000ff;">"http://schemas.android.com/apk/res/android"</span> <br />               <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"fill_parent"</span> <br />               <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"fill_parent"</span> <br />               <b><span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">shrinkColumns</span>=<span style="color:#0000ff;">"0,1,2"</span><span style="color:#0000ff;">><!-- have an eye on ! --></span></b> <br />   <b>  <span style="color:#0000ff;"><</span><span style="color:#800000;">TableRow</span><span style="color:#0000ff;">><!-- row1 --></span> <br /> </b>    <span style="color:#0000ff;"><</span><span style="color:#800000;">Button</span> <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">id</span>=<span style="color:#0000ff;">"@+id/button1"</span> <br />             <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />             <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"wrap_content"</span>            <br />             <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">text</span>=<span style="color:#0000ff;">"Hello, I am a Button1"</span> <br />             <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_column</span>=<span style="color:#0000ff;">"0"</span> <br />             <span style="color:#0000ff;">/></span> <br />        <span style="color:#0000ff;"><</span><span style="color:#800000;">Button</span> <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">id</span>=<span style="color:#0000ff;">"@+id/button2"</span> <br />      <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />      <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />      <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">text</span>=<span style="color:#0000ff;">"Hello, I am a Button2"</span> <br />      <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_column</span>=<span style="color:#0000ff;">"1"</span> <br />      <span style="color:#0000ff;">/></span> <br />      <span style="color:#0000ff;"></</span><span style="color:#800000;">TableRow</span><span style="color:#0000ff;">></span> <br />     <b><span style="color:#0000ff;"><</span><span style="color:#800000;">TableRow</span><span style="color:#0000ff;">><span style="color:#0000ff;"><!-- row2 --></span></span></b> <br />     <span style="color:#0000ff;"><</span><span style="color:#800000;">Button</span> <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">id</span>=<span style="color:#0000ff;">"@+id/button3"</span> <br />             <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />             <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"wrap_content"</span>            <br />             <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">text</span>=<span style="color:#0000ff;">"Hello, I am a Button3"</span> <br />             <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_column</span>=<span style="color:#0000ff;">"1"</span> <br />             <span style="color:#0000ff;">/></span> <br /> <span style="color:#0000ff;"><</span><span style="color:#800000;">Button</span> <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">id</span>=<span style="color:#0000ff;">"@+id/button4"</span> <br />      <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />      <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />      <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">text</span>=<span style="color:#0000ff;">"Hello, I am a Button4"</span> <br />      <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_column</span>=<span style="color:#0000ff;">"1"</span> <br />      <span style="color:#0000ff;">/></span> <br /> <span style="color:#0000ff;"></</span><span style="color:#800000;">TableRow</span><span style="color:#0000ff;">></span> <br /> <span style="color:#0000ff;"><</span><span style="color:#800000;">TableRow</span><span style="color:#0000ff;">></span>    <br />      <span style="color:#0000ff;"><</span><span style="color:#800000;">Button</span> <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">id</span>=<span style="color:#0000ff;">"@+id/button5"</span> <br />      <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />      <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"wrap_content"</span> <br />      <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">text</span>=<span style="color:#0000ff;">"Hello, I am a Button5"</span> <br />      <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_column</span>=<span style="color:#0000ff;">"2"</span> <br />      <span style="color:#0000ff;">/></span> <br /> <span style="color:#0000ff;"></</span><span style="color:#800000;">TableRow</span><span style="color:#0000ff;">></span> <br /> <span style="color:#0000ff;"></</span><span style="color:#800000;">TableLayout</span><span style="color:#0000ff;">></span> <br /> <br /> 运行之后可以得出下面的结果:</p>     <p align="center"><a href="https://simg.open-open.com/show/c31efd7226ac751c12aef48a9000374c.png"><img title="tablelayout" border="0" alt="tablelayout" src="https://simg.open-open.com/show/ce68a0710f4ddf3ac85616c89c9bd96e.png" width="394" height="266" /></a></p>     <p align="center">图6、表格布局</p>     <h4><b>5、列表视图(List View)</b></h4>     <p><b>列表布局</b>:是一个ViewGroup以列表显示它的子视图(view)元素,列表是可滚动的列表。列表元素通过<code><a href="/misc/goto?guid=4959518083039044634">ListAdapter</a>自动插入到列表。</code></p>     <p><code><a href="/misc/goto?guid=4959518083039044634"><b>ListAdapter</b></a></code>:扩展自<code><a href="/misc/goto?guid=4959518083129268593">Adapter</a></code>,它是<code><a href="/misc/goto?guid=4959518083219231201">ListView</a></code>和数据列表之间的桥梁。ListView可以显示任何包装在ListAdapter中的数据。该类提供两个公有类型的抽象方法:</p>     <ol>      <li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">abstract</span> <span style="color:#0000ff;">boolean</span>  areAllItemsEnabled () :表示ListAdapter中的所有元素是否可激活的?如果返回真,即所有的元素是可选择的即可点击的。 </li>      <li><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">abstract</span> <span style="color:#0000ff;">boolean</span>  isEnabled (<span style="color:#0000ff;">int</span> position) :判断指定位置的元素是否可激活的? </li>     </ol>     <p>下面通过一个例子来,创建一个可滚动的列表,并从一个字符串数组读取列表元素。当一个元素被选择时,显示该元素在列表中的位置的消息。</p>     <p>1)、首先,将<span style="color:#ff8040;">res/layour/main.xml</span>的内容置为如下: <br /> <br /> <span style="color:#0000ff;"><?</span>xml version="1.0" encoding="utf-8"<span style="color:#0000ff;">?></span> <br /> <span style="color:#0000ff;"><</span><span style="color:#800000;">TextView</span> <span style="color:#ff0000;">xmlns</span>:<span style="color:#ff0000;">android</span>=<span style="color:#0000ff;">"http://schemas.android.com/apk/res/android"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"fill_parent"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"fill_parent"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">padding</span>=<span style="color:#0000ff;">"10dp"</span> <br />     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">textSize</span>=<span style="color:#0000ff;">"16sp"</span> <span style="color:#0000ff;">></span> <br /> <span style="color:#0000ff;"></</span><span style="color:#800000;">TextView</span><span style="color:#0000ff;">></span></p>     <p>这样就定义了元素在列表中的布局。 </p>     <p>2)、<span style="color:#ff8040;">src/skynet.com.cnblogs.www/HelloWorld.java</span>文件的代码如下:</p>     <div>      <pre><span style="color:#0000ff;">package</span> skynet.com.cnblogs.www;<span style="color:#0000ff;">import</span> android.app.ListActivity;<span style="color:#0000ff;">import</span> android.os.Bundle;<span style="color:#0000ff;">import</span> android.view.View;<span style="color:#0000ff;">import</span> android.widget.AdapterView;<span style="color:#0000ff;">import</span> android.widget.ArrayAdapter;<span style="color:#0000ff;">import</span> android.widget.ListView;<span style="color:#0000ff;">import</span> android.widget.TextView;<span style="color:#0000ff;">import</span> android.widget.Toast;<span style="color:#0000ff;">import</span> android.widget.AdapterView.OnItemClickListener;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> HelloWorld <span style="color:#0000ff;">extends</span> <b>ListActivity</b>  {</pre>      <pre>    //注意这里Helloworld类不是扩展自Acitvity,而是扩展自ListAcitivty     <span style="color:#008000;">/** Called when the activity is first created. */</span>     @Override     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> onCreate(Bundle savedInstanceState) {         <span style="color:#0000ff;">super</span>.onCreate(savedInstanceState);         <b>setListAdapter(<span style="color:#0000ff;">new</span> ArrayAdapter<String>(<span style="color:#0000ff;">this</span>, R.layout.main, COUNTRIES));</b>         ListView lv = getListView();         lv.setTextFilterEnabled(<span style="color:#0000ff;">true</span>);          lv.setOnItemClickListener(<span style="color:#0000ff;">new</span> OnItemClickListener() {           <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> onItemClick(AdapterView<?> parent, View view,               <span style="color:#0000ff;">int</span> position, <span style="color:#0000ff;">long</span> id) {             <span style="color:#008000;">// When clicked, show a toast with the TextView text</span>             Toast.makeText(getApplicationContext(), ((TextView) view).getText(),                 Toast.LENGTH_SHORT).show();           }         });     }     <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">final</span> String[] COUNTRIES = <span style="color:#0000ff;">new</span> String[] {      "<span style="color:#8b0000;">1</span>", "<span style="color:#8b0000;">2</span>", "<span style="color:#8b0000;">3</span>", "<span style="color:#8b0000;">4</span>", "<span style="color:#8b0000;">5</span>",      "<span style="color:#8b0000;">6</span>", "<span style="color:#8b0000;">7</span>", "<span style="color:#8b0000;">8</span>", "<span style="color:#8b0000;">9</span>", "<span style="color:#8b0000;">10</span>",      "<span style="color:#8b0000;">11</span>", "<span style="color:#8b0000;">12</span>", "<span style="color:#8b0000;">13</span>", "<span style="color:#8b0000;">14</span>", "<span style="color:#8b0000;">15</span>",      "<span style="color:#8b0000;">16</span>", "<span style="color:#8b0000;">17</span>", "<span style="color:#8b0000;">18</span>", "<span style="color:#8b0000;">19</span>", "<span style="color:#8b0000;">20</span>",      "<span style="color:#8b0000;">21</span>", "<span style="color:#8b0000;">22</span>", "<span style="color:#8b0000;">23</span>", "<span style="color:#8b0000;">24</span>"    }; }</pre>     </div>     <p><b>Note</b>:<span style="color:#ff8040;">onCreate()</span>函数中并不像往常一样通过<span style="color:#ff8040;">setContentView()</span>为活动(Activity)加载布局文件,替代的是通过<code><a href="/misc/goto?guid=4959518083289629871"><span style="font-family:Georgia;">setListAdapter(ListAdapter)</span></a>自动添加一个ListView填充整个屏幕的ListActivity。在此文件中这个方法以一个ArrayAdapter为参数:<b>setListAdapter(<span style="color:#0000ff;">new</span> ArrayAdapter<String>(<span style="color:#0000ff;">this</span>, R.layout.main, COUNTRIES))</b>,这个ArrayAdapter管理填入ListView中的列表元素。ArrayAdapter的构造函数的参数为:this(表示应用程序的上下文context)、表示ListViewde布局文件(这里是R.layout.main)、插入ListView的List对象对数组(这里是COUNTRES)。</code></p>     <p><code><a href="/misc/goto?guid=4959518083372125453"><span style="font-family:Georgia;">setOnItemClickListener(OnItemClickListener)</span></a>定义了每个元素的点击(on-click)的监听器,当ListView中的元素被点击时,<code><a href="/misc/goto?guid=4959518083450967882"><span style="font-family:Georgia;">onItemClick()</span></a>方法被调用,在这里是即一个<code><a href="/misc/goto?guid=4959518083538081150"><span style="font-family:Georgia;">Toast</span></a></code>消息——每个元素的位置将显示。</code></code></p>     <p>3)、运行应用程序得如下结果(点击1之后,在下面显示了1):</p>     <p style="text-align:center;"><a href="https://simg.open-open.com/show/cf2221503cad1994b1a89246f2fb1d93.png"><img title="listview" border="0" alt="listview" src="https://simg.open-open.com/show/207a558fbb8d244c420806eed6bfead8.png" width="376" height="574" /></a></p>     <p align="center">图7、列表布局</p>     <p align="left"><b>NOTE:</b>如果你改了HelloWorld <span style="color:#0000ff;">extends</span> <b>ListActivity</b> 而不是<b>Activity</b>之后,运行程序是提示:“Conversion to Dalvik format failed with error 1”。可以这么解决:解决办法是 Project > Clean... > Clean project selected below > Ok</p>     <h4><b>5.1、一个小的改进</b></h4>     <p>上面我们是把要填充到ListView中的元素硬编码到HelloWorld.java文件中,这样就缺乏灵活性!也不符合推荐的<b>应用程序的界面</b>与<b>控制它行为的代码</b>更好地分离的准则!</p>     <p>其实我们可以把要填充到ListView的元素写到<span style="font-family:Georgia;"><code><span style="color:#ff8040;">res/values/strings.xml</span></code>文件中的<code><string-array></code>元素中,然后再源码中动态地读取。这样strings.xml的内容类似下面:</span></p>     <div>      <pre><span style="color:#0000ff;"><?</span>xml version="1.0" encoding="utf-8"<span style="color:#0000ff;">?></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">resources</span><span style="color:#0000ff;">></span>     <span style="color:#0000ff;"><</span><span style="color:#800000;">string</span>-<span style="color:#ff0000;">array</span> <span style="color:#ff0000;">name</span>=<span style="color:#0000ff;">"countries_array"</span><span style="color:#0000ff;">></span>         <span style="color:#0000ff;"><</span><span style="color:#800000;">item</span><span style="color:#0000ff;">></span>1<span style="color:#0000ff;"></</span><span style="color:#800000;">item</span><span style="color:#0000ff;">></span>         <span style="color:#0000ff;"><</span><span style="color:#800000;">item</span><span style="color:#0000ff;">></span>2<span style="color:#0000ff;"></</span><span style="color:#800000;">item</span><span style="color:#0000ff;">></span>         <span style="color:#0000ff;"><</span><span style="color:#800000;">item</span><span style="color:#0000ff;">></span>3<span style="color:#0000ff;"></</span><span style="color:#800000;">item</span><span style="color:#0000ff;">></span>         <span style="color:#0000ff;"><</span><span style="color:#800000;">item</span><span style="color:#0000ff;">></span>4<span style="color:#0000ff;"></</span><span style="color:#800000;">item</span><span style="color:#0000ff;">></span>         <span style="color:#0000ff;"><</span><span style="color:#800000;">item</span><span style="color:#0000ff;">></span>5<span style="color:#0000ff;"></</span><span style="color:#800000;">item</span><span style="color:#0000ff;">></span>         <span style="color:#0000ff;"><</span><span style="color:#800000;">item</span><span style="color:#0000ff;">></span>6<span style="color:#0000ff;"></</span><span style="color:#800000;">item</span><span style="color:#0000ff;">></span>         <span style="color:#0000ff;"><</span><span style="color:#800000;">item</span><span style="color:#0000ff;">></span>7<span style="color:#0000ff;"></</span><span style="color:#800000;">item</span><span style="color:#0000ff;">></span>     <span style="color:#0000ff;"></</span><span style="color:#800000;">string</span>-array<span style="color:#0000ff;">></span> <span style="color:#0000ff;"></</span><span style="color:#800000;">resources</span><span style="color:#0000ff;">></span></pre>     </div>     <p>然而HelloWorld.java文件中的<span style="color:#ff8040;">onCreate()</span>函数,则这样动态访问这个数组及填充到ListVies: <br /> <br /> String[] countries = getResources().getStringArray(R.array.countries_array); <br /> setListAdapter(<span style="color:#0000ff;">new</span> ArrayAdapter<String>(<span style="color:#0000ff;">this</span>, R.layout.list_item, countries)); </p>     <h4><b>5.2、补充说明</b></h4>     <p>首先总结一下列表布局的关键部分:</p>     <ul>      <li>布局文件中定义ListView </li>      <li>Adapter用来将数据填充到ListView </li>      <li>要填充到ListView的数据,这些数据可以字符串、图片、控件等等 </li>     </ul>     <p>其中Adapter是ListView和数据源之间的桥梁,根据数据源的不同Adapter可以分为三类:</p>     <ul>      <li>String[]: ArrayAdapter </li>      <li>List<Map<String,?>>: SimpleAdapter </li>      <li>数据库Cursor: SimpleCursorAdapter </li>     </ul>     <p>使用ArrayAdapter(数组适配器)顾名思义,需要把数据放入一个数组以便显示,上面的例子就是这样的;SimpleAdapter能定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等;SimpleCursorAdapter是和数据库有关的东西。篇幅有限后面两种就不举例实践了。你可以参考<a href="/misc/goto?guid=4959517930155700593">android ListView详解</a>or<a href="/misc/goto?guid=4959518083652993088">ArrayAdapter ,SimpleAdapter ,SimpleCursorAdapter 区别</a>。</p>     <h4>6<b>、网格视图(Grid View)</b></h4>     <p><b>网格布局</b>:是一个ViewGroup以网格显示它的子视图(view)元素,即二维的、滚动的网格。网格元素通过<code><a href="/misc/goto?guid=4959518083039044634">ListAdapter</a>自动插入到网格。</code>ListAdapter跟上面的列表布局是一样的,这里就不重复累述了。</p>     <p>下面也通过一个例子来,创建一个显示图片缩略图的网格。当一个元素被选择时,显示该元素在列表中的位置的消息。</p>     <p>1)、首先,将上面实践截取的图片放入<code><span style="color:#ff8040;">res/drawable/</span></code></p>     <p>2)、<span style="color:#ff8040;">res/layour/main.xml</span>的内容置为如下:这个GridView填满整个屏幕,而且它的属性都很好理解,按英文单词的意思就对了。</p>     <div>      <pre><span style="color:#0000ff;"><?</span>xml version="1.0" encoding="utf-8"<span style="color:#0000ff;">?></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">GridView</span> <span style="color:#ff0000;">xmlns</span>:<span style="color:#ff0000;">android</span>=<span style="color:#0000ff;">"http://schemas.android.com/apk/res/android"</span>      <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">id</span>=<span style="color:#0000ff;">"@+id/gridview"</span>     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"fill_parent"</span>      <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"fill_parent"</span>     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">columnWidth</span>=<span style="color:#0000ff;">"90dp"</span>     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">numColumns</span>=<span style="color:#0000ff;">"auto_fit"</span>     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">verticalSpacing</span>=<span style="color:#0000ff;">"10dp"</span>     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">horizontalSpacing</span>=<span style="color:#0000ff;">"10dp"</span>     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">stretchMode</span>=<span style="color:#0000ff;">"columnWidth"</span>     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">gravity</span>=<span style="color:#0000ff;">"center"</span> <span style="color:#0000ff;">/></span></pre>     </div>     <p>3)、然后,HelloWorld.java文件中<span style="color:#ff8040;">onCreate()</span>函数如下:</p>     <div>      <pre>    <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> onCreate(Bundle savedInstanceState) {         <span style="color:#0000ff;">super</span>.onCreate(savedInstanceState);         setContentView(R.layout.main);          GridView gridview = (GridView) findViewById(R.id.gridview);         gridview.setAdapter(<span style="color:#0000ff;">new</span> ImageAdapter(<span style="color:#0000ff;">this</span>));          gridview.setOnItemClickListener(<span style="color:#0000ff;">new</span> OnItemClickListener() {             <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> onItemClick(AdapterView<?> parent, View v, <span style="color:#0000ff;">int</span> position, <span style="color:#0000ff;">long</span> id) {                 Toast.makeText(HelloWorld.<span style="color:#0000ff;">this</span>, "<span style="color:#8b0000;"> </span>" + position, Toast.LENGTH_SHORT).show();             }         });     } </pre>     </div>     <p> </p>     <p><span style="color:#ff8040;">onCreate()</span>函数跟通常一样,首先调用超类的<span style="color:#ff8040;">onCreate()</span>函数函数,然后通过<span style="color:#ff8040;">setContentView()</span>为活动(Activity)加载布局文件。紧接着是,通过GridView的id获取布局文件中的gridview,然后调用它的<code><a href="/misc/goto?guid=4959518083289629871"><span style="font-family:Georgia;">setListAdapter(ListAdapter)</span></a></code>函数填充它,它的参数是一个我们自定义的ImageAdapter。后面的工作跟列表布局中一样,为监听网格中的元素被点击的事件而做的工作。</p>     <p>4)、实现我们自定义ImageAdapter,新添加一个类文件,它的代码如下:</p>     <div>      <pre><span style="color:#0000ff;">package</span> skynet.com.cnblogs.www;<span style="color:#0000ff;">import</span> android.content.Context;<span style="color:#0000ff;">import</span> android.view.View;<span style="color:#0000ff;">import</span> android.view.ViewGroup;<span style="color:#0000ff;">import</span> android.widget.BaseAdapter;<span style="color:#0000ff;">import</span> android.widget.GridView;<span style="color:#0000ff;">import</span> android.widget.ImageView;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> ImageAdapter <span style="color:#0000ff;">extends</span> BaseAdapter {     <span style="color:#0000ff;">private</span> Context mContext;      <span style="color:#0000ff;">public</span> ImageAdapter(Context c) {         mContext = c;     }      <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">int</span> getCount() {         <span style="color:#0000ff;">return</span> mThumbIds.length;     }      <span style="color:#0000ff;">public</span> Object getItem(<span style="color:#0000ff;">int</span> position) {         <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">null</span>;     }      <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">long</span> getItemId(<span style="color:#0000ff;">int</span> position) {         <span style="color:#0000ff;">return</span> 0;     }      <span style="color:#008000;">// create a new ImageView for each item referenced by the Adapter</span>     <span style="color:#0000ff;">public</span> View getView(<span style="color:#0000ff;">int</span> position, View convertView, ViewGroup parent) {         ImageView imageView;         <span style="color:#0000ff;">if</span> (convertView == <span style="color:#0000ff;">null</span>) {  <span style="color:#008000;">// if it's not recycled, initialize some attributes</span>             imageView = <span style="color:#0000ff;">new</span> ImageView(mContext);             imageView.setLayoutParams(<span style="color:#0000ff;">new</span> GridView.LayoutParams(85, 85));             imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);             imageView.setPadding(8, 8, 8, 8);         } <span style="color:#0000ff;">else</span> {             imageView = (ImageView) convertView;         }          imageView.setImageResource(mThumbIds[position]);         <span style="color:#0000ff;">return</span> imageView;     }      <span style="color:#008000;">// references to our images</span>     <span style="color:#0000ff;">private</span> Integer[] mThumbIds = {             R.drawable.linearlayout1, R.drawable.linearlayout2,             R.drawable.linearlayout3, R.drawable.listview,             R.drawable.relativelayout, R.drawable.tablelayout     }; }</pre>     </div>     <p> </p>     <p> </p>     <p> </p>     <p>ImageAdapter类扩展自<code><a href="/misc/goto?guid=4959518083750150068"><span style="font-family:Georgia;">BaseAdapter</span></a>,所以首先得实现它所要求必须实现的方法。构造函数和getcount()函数很好理解,而getItem(int)应该返回实际对象在适配器中的特定位置,但是这里我们不需要。类似地,getItemId(int)应该返回元素的行号,但是这里也不需要。</code></p>     <p><code>这里重点要介绍的是getView()方法,它为每个要添加到ImageAdapter的图片都创建了一个新的View。当调用这个方法时,一个View是循环再用的,因此要确认对象是否为空。如果是空的话,一个<code><a href="/misc/goto?guid=4959518083830279125">ImageView</a></code>就被实例化且配置想要的显示属性:</code></p>     <ul>      <li><code><a href="/misc/goto?guid=4959518083911462826">setLayoutParams(ViewGroup.LayoutParams)</a></code>:设置View的高度和宽度,这确保不管drawable中图片的大小,每个图片都被重新设置大小且剪裁以适应这些尺寸。 </li>      <li><code><a href="/misc/goto?guid=4959518083994298141">setScaleType(ImageView.ScaleType)</a></code>:声明图片应该向中心剪裁(如果需要的话)。 </li>      <li><code><a href="/misc/goto?guid=4959518084074682262">setPadding(int, int, int, int)</a></code>:定义补距,如果图片有不同的横纵比,小的补距将导致更多的剪裁以适合设置的ImageView的高度和宽度。 </li>     </ul>     <p>如果View传到getView()不是空的,则本地的ImageView初始化时将循环再用View对象。在getView()方法末尾,position整数传入setImageResource()方法以从mThumbIds数组中选择图片。</p>     <p>运行程序会得到如下结果(点击第一张图片之后):</p>     <p><a href="https://simg.open-open.com/show/10efb821f8daba0b13cda637d644d0aa.png"><img title="gridviewlayout" border="0" alt="gridviewlayout" src="https://simg.open-open.com/show/3ae50a9113ec48ea1df20a26ad1fcbef.png" width="381" height="473" /></a> </p>     <p align="center">图8、网格布局</p>     <h4><b>7、<b>绝对布局</b>(AbsoluteLayout)</b></h4>     <p><b>绝对布局</b>:是一个ViewGroup以绝对方式显示它的子视图(view)元素,即以坐标的方式来定位在屏幕上位置。</p>     <p>这种布局方式很好理解,在布局文件或编程地设置View的坐标,从而绝对地定位。如下所示布局文件:</p>     <div>      <pre><b><span style="color:#0000ff;"><</span><span style="color:#800000;">AbsoluteLayout</span></b> <span style="color:#ff0000;">xmlns</span>:<span style="color:#ff0000;">android</span>=<span style="color:#0000ff;">"http://schemas.android.com/apk/res/android"</span>     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">id</span>=<span style="color:#0000ff;">"@+id/AbsoluteLayout01"</span>     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"fill_parent"</span>     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"fill_parent"</span>     <span style="color:#0000ff;">></span>    <span style="color:#0000ff;"><</span><span style="color:#800000;">TextView</span> <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">id</span>=<span style="color:#0000ff;">"@+id/txtIntro"</span>      <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">text</span>=<span style="color:#0000ff;">"绝对布局"</span>      <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"fill_parent"</span>      <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"wrap_content"</span> <b>     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_x</span>=<span style="color:#0000ff;">"20dip"<!-- have an eye on ! --></span>      <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_y</span>=<span style="color:#0000ff;">"20dip"</span><span style="color:#0000ff;">><!-- have an eye on ! --></span></b>    <span style="color:#0000ff;"></</span><span style="color:#800000;">TextView</span><span style="color:#0000ff;">></span> <b><span style="color:#0000ff;"></</span><span style="color:#800000;">AbsoluteLayout</span><span style="color:#0000ff;">></span></b></pre>     </div>     <p> </p>     <p>简单吧,这里不在深入了!</p>     <h4><b>8、标签布局(Tab Layout)</b></h4>     <p><b>标签布局</b>:是一个ViewGroup以标签的方式显示它的子视图(view)元素,就像在Firefox中的一个窗口中显示多个网页一样。</p>     <p>为了狂创建一个标签UI(tabbed UI),需要使用到<code><a href="/misc/goto?guid=4959518084163214283">TabHost</a></code>和<code><a href="/misc/goto?guid=4959518084245400716">TabWidget</a>。<code><a href="/misc/goto?guid=4959518084163214283">TabHost</a></code>必须是布局的根节点,它包含为了显示标签的<code><a href="/misc/goto?guid=4959518084245400716">TabWidget</a>和显示标签内容的<code><a href="/misc/goto?guid=4959518084350091968">FrameLayout</a></code>。</code></code></p>     <p><code><code>可以有两种方式实现标签内容:使用标签在同一个活动中交换视图、使用标签在完全隔离的活动之间改变。根据你的需要,选择不同的方式,但是如果每个标签提供不同的用户活动,为每个标签选择隔离的活动,因此你可以更好地以分离的组管理应用程序,而不是一个巨大的应用程序和布局。</code></code><code><code>下面还有一个例子来创建一个标签UI,每个标签使用隔离的活动。</code></code></p>     <p>1)、在项目中建立三个隔离的Activity类:ArtistisActivity、AlbumActivity、SongActivity。它们每个表示一个分隔的标签。每个通过TextView显示简单的一个消息,例如:</p>     <div>      <pre><span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> ArtistsActivity <span style="color:#0000ff;">extends</span> Activity {     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> onCreate(Bundle savedInstanceState) {         <span style="color:#0000ff;">super</span>.onCreate(savedInstanceState);          TextView textview = <span style="color:#0000ff;">new</span> TextView(<span style="color:#0000ff;">this</span>);         textview.setText("<span style="color:#8b0000;">This is the Artists tab</span>");         setContentView(textview);     } }</pre>     </div>     <p> </p>     <p>其它两个类也类似。</p>     <p>2)、设置每个标签的图标,每个图标应该有两个版本:一个是选中时的,一个是未选中时的。通常的设计建议是,选中的图标应该是深色(灰色),未选中的图标是浅色(白色)。</p>     <p>现在创建一个<a href="/misc/goto?guid=4959518084425108017">state-list drawable</a>指定哪个图标表示标签的状态:将图片放到res/drawable目录下并创建一个新的XML文件命名为<code>ic_tab_artists.xml</code>,内容如下:</p>     <div>      <pre><span style="color:#0000ff;"><?</span>xml version="1.0" encoding="utf-8"<span style="color:#0000ff;">?></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">selector</span> <span style="color:#ff0000;">xmlns</span>:<span style="color:#ff0000;">android</span>=<span style="color:#0000ff;">"http://schemas.android.com/apk/res/android"</span><span style="color:#0000ff;">></span>     <span style="color:#008000;"><!-- When selected, use grey --></span>     <span style="color:#0000ff;"><</span><span style="color:#800000;">item</span> <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">drawable</span>=<span style="color:#0000ff;">"@drawable/ic_tab_artists_grey"</span>           <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">state_selected</span>=<span style="color:#0000ff;">"true"</span> <span style="color:#0000ff;">/></span>     <span style="color:#008000;"><!-- When not selected, use white--></span>     <span style="color:#0000ff;"><</span><span style="color:#800000;">item</span> <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">drawable</span>=<span style="color:#0000ff;">"@drawable/ic_tab_artists_white"</span> <span style="color:#0000ff;">/></span> <span style="color:#0000ff;"></</span><span style="color:#800000;">selector</span><span style="color:#0000ff;">></span></pre>     </div>     <p> </p>     <p>3)、<span style="color:#ff8040;">res/layour/main.xml</span>的内容置为如下:</p>     <div>      <pre><span style="color:#0000ff;"><?</span>xml version="1.0" encoding="utf-8"<span style="color:#0000ff;">?></span> <span style="color:#0000ff;"><</span><span style="color:#800000;">TabHost</span> <span style="color:#ff0000;">xmlns</span>:<span style="color:#ff0000;">android</span>=<span style="color:#0000ff;">"http://schemas.android.com/apk/res/android"</span>     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">id</span>=<span style="color:#0000ff;">"@android:id/tabhost"</span>     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"fill_parent"</span>     <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"fill_parent"</span><span style="color:#0000ff;">></span>     <span style="color:#0000ff;"><</span><span style="color:#800000;">LinearLayout</span>         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">orientation</span>=<span style="color:#0000ff;">"vertical"</span>         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"fill_parent"</span>         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"fill_parent"</span>         <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">padding</span>=<span style="color:#0000ff;">"5dp"</span><span style="color:#0000ff;">></span>         <span style="color:#0000ff;"><</span><span style="color:#800000;">TabWidget</span>             <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">id</span>=<span style="color:#0000ff;">"@android:id/tabs"</span>             <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"fill_parent"</span>             <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"wrap_content"</span> <span style="color:#0000ff;">/></span>         <span style="color:#0000ff;"><</span><span style="color:#800000;">FrameLayout</span>             <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">id</span>=<span style="color:#0000ff;">"@android:id/tabcontent"</span>             <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_width</span>=<span style="color:#0000ff;">"fill_parent"</span>             <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">layout_height</span>=<span style="color:#0000ff;">"fill_parent"</span>             <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">padding</span>=<span style="color:#0000ff;">"5dp"</span> <span style="color:#0000ff;">/></span>     <span style="color:#0000ff;"></</span><span style="color:#800000;">LinearLayout</span><span style="color:#0000ff;">></span> <span style="color:#0000ff;"></</span><span style="color:#800000;">TabHost</span><span style="color:#0000ff;">></span></pre>     </div>     <p> </p>     <p>这个布局将显示标签和提供上面创建的活动之间的导航。<code><a href="/misc/goto?guid=4959518084163214283">TabHost</a></code><code>要求包含一个<a href="/misc/goto?guid=4959518084245400716">TabWidget</a>和一个<code><a href="/misc/goto?guid=4959518084350091968">FrameLayout</a></code>。<a href="/misc/goto?guid=4959518084245400716">TabWidget</a>和<code><a href="/misc/goto?guid=4959518084350091968">FrameLayout</a></code><code><a href="/misc/goto?guid=4959518084163214283">TabHost</a></code>以线性垂直地显示。</code></p>     <p>4)、HelloWorld.java文件源码如下:</p>     <div>      <pre><span style="color:#0000ff;">package</span> skynet.com.cnblogs.www;<span style="color:#0000ff;">import</span> android.widget.TabHost;<span style="color:#0000ff;">import</span> android.app.TabActivity;<span style="color:#0000ff;">import</span> android.content.Intent;<span style="color:#0000ff;">import</span> android.content.res.Resources;<span style="color:#0000ff;">import</span> android.os.Bundle;<span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> HelloWorld <span style="color:#0000ff;">extends</span> TabActivity{     <span style="color:#008000;">/** Called when the activity is first created. */</span>     @Override     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> onCreate(Bundle savedInstanceState) {         <span style="color:#0000ff;">super</span>.onCreate(savedInstanceState);         setContentView(R.layout.main);          Resources res = getResources(); <span style="color:#008000;">// Resource object to get Drawables</span>         TabHost tabHost = getTabHost();  <span style="color:#008000;">// The activity TabHost</span>         TabHost.TabSpec spec;  <span style="color:#008000;">// Resusable TabSpec for each tab</span>         Intent intent;  <span style="color:#008000;">// Reusable Intent for each tab</span>         <span style="color:#008000;">// Create an Intent to launch an Activity for the tab (to be reused)</span>         intent = <span style="color:#0000ff;">new</span> Intent().setClass(<span style="color:#0000ff;">this</span>, ArtistsActivity.<span style="color:#0000ff;">class</span>);          <span style="color:#008000;">// Initialize a TabSpec for each tab and add it to the TabHost</span>         spec = tabHost.newTabSpec("<span style="color:#8b0000;">artists</span>").setIndicator("<span style="color:#8b0000;">Artists</span>",                           res.getDrawable(R.drawable.ic_tab_artists))                       .setContent(intent);         tabHost.addTab(spec);          <span style="color:#008000;">// Do the same for the other tabs</span>         intent = <span style="color:#0000ff;">new</span> Intent().setClass(<span style="color:#0000ff;">this</span>, AlbumsActivity.<span style="color:#0000ff;">class</span>);         spec = tabHost.newTabSpec("<span style="color:#8b0000;">albums</span>").setIndicator("<span style="color:#8b0000;">Albums</span>",                           res.getDrawable(R.drawable.ic_tab_artists))                       .setContent(intent);         tabHost.addTab(spec);          intent = <span style="color:#0000ff;">new</span> Intent().setClass(<span style="color:#0000ff;">this</span>, SongsActivity.<span style="color:#0000ff;">class</span>);         spec = tabHost.newTabSpec("<span style="color:#8b0000;">songs</span>").setIndicator("<span style="color:#8b0000;">Songs</span>",                           res.getDrawable(R.drawable.ic_tab_artists))                       .setContent(intent);         tabHost.addTab(spec);          tabHost.setCurrentTab(2);     } }</pre>     </div>     <p> </p>     <p> </p>     <p> </p>     <p>设置每个标签的文字和图标,并分配每个标签一个活动(这里为了方便三个标签都有相同的图标)。TabHost的引用第一次通过<code><a href="/misc/goto?guid=4959518084564944862">getTabHost()</a>获取。然后,为每个标签,创建<code><a href="/misc/goto?guid=4959518084647509676">TabHost.TabSpec</a>定义标签的属性。<code><a href="/misc/goto?guid=4959518084732774387">newTabSpec(String)</a></code>方法创建一个新的<code><a href="/misc/goto?guid=4959518084647509676">TabHost.TabSpec</a></code>以给定的字符串标识标签。调用<code><a href="/misc/goto?guid=4959518084647509676">TabHost.TabSpec</a></code>, <code><a href="/misc/goto?guid=4959518084835255361">setIndicator(CharSequence, Drawable)</a></code>为每个标签设置文字和图标,调用<code><a href="/misc/goto?guid=4959518084916576535">setContent(Intent)</a></code>指定<code><a href="/misc/goto?guid=4959518084996618985">Intent</a></code>去打开合适的活动。每个<code><a href="/misc/goto?guid=4959518084647509676">TabHost.TabSpec</a></code>通过调用<code><a href="/misc/goto?guid=4959518085084015878">addTab(TabHost.TabSpec)</a>添加到TabHost。</code></code></code></p>     <p><code><code><code>最后,<code><a href="/misc/goto?guid=4959518085171942126">setCurrentTab(int)</a></code>设置打开默认显示的标签,通过索引标签的位置。</code></code></code></p>     <p>5)、打开Android的清单文件<span style="color:#ff8040;">AndroidManifest.xml</span>,添加<code><span style="color:#ff8040;">NoTitleBar</span>主题到HelloWorld的<code><span style="color:#ff8040;"><activity></span>标记。这将移除默认应用程序的标题和顶端布局,给标签腾出位置。<code><span style="color:#ff8040;"><activity></span></code>标记应该像这样:</code></code></p>     <div>      <pre>        <span style="color:#0000ff;"><</span><span style="color:#800000;">activity</span> <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">name</span>=<span style="color:#0000ff;">".HelloWorld"</span>                   <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">label</span>=<span style="color:#0000ff;">"@string/app_name"</span>                   <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">theme</span>=<span style="color:#0000ff;">"@android:style/Theme.NoTitleBar"</span><span style="color:#0000ff;">></span></pre>     </div>     <p> </p>     <p><code><code>你运行这个程序能够得到什么结果呢?请自行检查。不过我在这里告诉你很有可能会运行不了,报“<span style="color:#ff0000;">java.lang.NullPointerException</span>”错!我想运行这个例子的很多人都会有这个问题,不信你试试!</code></code></p>     <p><code><code>PS:其实这也算是Android的一个bug,而且这个bug在2.2中还没有解决,这个问题全球N多人都碰到了,并在<a title="http://code.google.com/p/android/issues" href="/misc/goto?guid=4959518085251909093">http://code.google.com/p/android/issues</a>中挂号了,相关问题的编号有不止一个。</code></code></p>     <p><code><code></code></code></p>     <p><code><code>接着往下看……</code></code></p>     <p><code><code>如果你看了我这篇文章,你一定会是个幸运儿!经过我艰苦的调试+找资料,我找到了解决方法:</code></code></p>     <p><code><code>在清单文件<span style="color:#ff8040;">AndroidManifest.xml</span>,添加下面三个Activity:</code></code> <br /> <br /> <span style="color:#0000ff;"><</span><span style="color:#800000;">activity</span> <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">name</span>=<span style="color:#0000ff;">".AlbumsActivity"</span>  <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">label</span>=<span style="color:#0000ff;">"@string/app_name"</span><span style="color:#0000ff;">></span><span style="color:#0000ff;"></</span><span style="color:#800000;">activity</span><span style="color:#0000ff;">></span> <br /> <span style="color:#0000ff;"><</span><span style="color:#800000;">activity</span> <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">name</span>=<span style="color:#0000ff;">".ArtistsActivity"</span> <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">label</span>=<span style="color:#0000ff;">"@string/app_name"</span><span style="color:#0000ff;">></span><span style="color:#0000ff;"></</span><span style="color:#800000;">activity</span><span style="color:#0000ff;">></span> <br /> <span style="color:#0000ff;"><</span><span style="color:#800000;">activity</span> <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">name</span>=<span style="color:#0000ff;">".SongsActivity"</span>  <span style="color:#ff0000;">android</span>:<span style="color:#ff0000;">label</span>=<span style="color:#0000ff;">"@string/app_name"</span><span style="color:#0000ff;">></span><span style="color:#0000ff;"></</span><span style="color:#800000;">activity</span><span style="color:#0000ff;">></span> </p>     <p>现在运行可以看到如下结果:</p>     <p align="center"><a href="https://simg.open-open.com/show/87f2bf7ba5aa08bbc3b1c0426f86f635.png"><img title="tablayout" border="0" alt="tablayout" src="https://simg.open-open.com/show/66fa593f3cc828c4843092afdc3985c4.png" width="377" height="219" /></a><br /> 图9、标签布局 </p>    </div>    <div id="MySignature">     <p> </p>     <div id="SkynetSignature">      <p>作者:吴秦<br /> 出处:http://www.cnblogs.com/skynet/</p>     </div>    </div>