【Android 如何置底一个View(附 前置声明layout布局文件中的资源ID)】

简介:



  PS:初学Android,很多术语可能不甚严谨,希望大家积极指出,bill才好及时改正,以免误人子弟。

今天在考虑一个RelativeLayout布局,整个屏幕分为两个部分,上部分是一个ListView,下部分是两个横排的Button。欲实现这两个Button始终置底,ListViewButton的上方占满剩余的空间。

Button置底这个方法还算简单,直接将两个Button包裹于一个LinearLayout,然后设置这个LinearLayout的属性android:layout_alignParentBottomtrue即可。

效果如下:

 

 

 

XML代码如下:

 


 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent"> 
  5.  
  6.     <!-- Button 置底 --> 
  7.     <LinearLayout android:id="@+id/test_bottom_buttons" 
  8.         android:layout_width="fill_parent" android:layout_height="wrap_content" 
  9.         android:orientation="horizontal" android:layout_alignParentBottom="true"> 
  10.  
  11.         <Button android:layout_width="wrap_content" 
  12.             android:layout_height="wrap_content" android:text="确定"></Button> 
  13.  
  14.         <Button android:layout_width="wrap_content" 
  15.             android:layout_height="wrap_content" android:text="取消"></Button> 
  16.  
  17.     </LinearLayout> 
  18. </RelativeLayout> 

接下来就是要把剩余的空间用一个ListView进行填充了。

最开始bill臆断地认为,只要在包裹ButtonsLinearLayout代码上方加上一个ListViewOK了,这是我最开始错误的xml布局文件:

 


 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent"> 
  5.  
  6.     <ListView android:id="@+id/lv_test" android:layout_width="fill_parent" 
  7.         android:layout_height="fill_parent"> 
  8.     </ListView> 
  9.  
  10.     <!-- Button 置底 --> 
  11.     <LinearLayout android:id="@+id/test_bottom_buttons" 
  12.         android:layout_width="fill_parent" android:layout_height="wrap_content" 
  13.         android:orientation="horizontal" android:layout_alignParentBottom="true"> 
  14.  
  15.         <Button android:layout_width="wrap_content" 
  16.             android:layout_height="wrap_content" android:text="确定"></Button> 
  17.  
  18.         <Button android:layout_width="wrap_content" 
  19.             android:layout_height="wrap_content" android:text="取消"></Button> 
  20.  
  21.     </LinearLayout> 
  22.      
  23. </RelativeLayout> 

 

写完后悲剧地发现Button倒是置底了,但是ListView却并没有在Button上方,而是充斥了整个屏幕。如下图所示:

 

可以看到,ListViewItem 8列表项穿过了Buttons

 

 

相对布局是最灵活、最巧妙的“手工活儿”,出现这种情况,只能怪bill自己疏忽大意,稍后我注意到这样一条属性android:layout_above="[viewid]",表示相对布局中的当前view处在viewid所指代view的上方。这可算是救星啊,迅速改正ListView的布局代码如下:

 


 
 
  1. <ListView android:id="@+id/lv_test" android:layout_width="fill_parent" 
  2. android:layout_height="fill_parent"
  3. android:layout_above="@id/test_bottom_buttons"> 
  4. </ListView> 

 

这样理论上就表示ListView是位于Buttons的上方,原本以为这样就大功告成了,没想到却得到一个莫名其妙的编译错误:

 

error: Error: No resource found that matches the given name (at 'layout_above' with value 

 '@id/test_bottom_buttons').

 

说是在android:layout_above这条属性语句中,找不到@id/test_bottom_buttons所指的资源,但是我上面的xml文件明明添加了这个ID的啊(<LinearLayout android:id="@+id/test_bottom_buttons"),怎么会找不到?

后来仔细一想,既然我认为“test_bottom_buttons这个ID资源肯定存在”是一个事实,而编译提示“找不到该ID所指的资源”也是一个事实,两个完全对立的事实不可能同时存在,那么总有一个“事实”是伪造的,而编译器99.99%是不会撒谎的。也就是说我自己把自己骗了,test_bottom_buttons这个ID资源并不存在。

但是这个ID的的确确已经被我写进XML布局文件里了啊?!

突然,我得到了一个灵感,我们知道,C++里两个类相互嵌套会产生编译错误,提示找不到其中一个类的声明。这里会不会出现了类似于这种情况的错误呢?

于是我把ListViewXML代码写在了Buttons的后面:

 


 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent"> 
  5.  
  6.     <!-- Button 置底 --> 
  7.     <LinearLayout android:id="@+id/test_bottom_buttons" 
  8.         android:layout_width="fill_parent" android:layout_height="wrap_content" 
  9.         android:orientation="horizontal" android:layout_alignParentBottom="true"> 
  10.  
  11.         <Button android:layout_width="wrap_content" 
  12.             android:layout_height="wrap_content" android:text="确定"></Button> 
  13.  
  14.         <Button android:layout_width="wrap_content" 
  15.             android:layout_height="wrap_content" android:text="取消"></Button> 
  16.  
  17.     </LinearLayout> 
  18.  
  19.     <ListView android:id="@+id/lv_test" android:layout_width="fill_parent" 
  20.         android:layout_height="fill_parent" android:layout_above="@id/test_bottom_buttons"> 
  21.     </ListView> 
  22.  
  23. </RelativeLayout> 

不出所料,编译通过并正常显示如下:

 

Buttons已经置底,且ListView位于Buttons上方

 

 

这就说明,之前的错误是由于在xml编译期,ListView写在了LinearLayout的前面,编译ListView时,LinearLayoutID @+id/test_bottom_buttons 尚未添加到系统中,而ListView就提前使用了这条ID资源 android:layout_above="@id/test_bottom_buttons因此编译到此处时,由于找不到ID资源而导致编译错误并停止编译,于是就出现了上述“莫名其妙”的错误。解决办法之一是,严格遵循“先声明后使用”的原则办事。

问题虽然解决了,但是却解决得并不完美。我们一般习惯按照布局的排版顺序依次列写我们的xml代码,但是像上述这样的xml并没有按照布局的顺序编写,而是为了解决找不到ID这个问题,把本应该写在前面的ListView后置了。总觉得很别扭,有没有什么方法既能解决找不到ID的问题,又能按照实际布局的顺序编写XML布局代码呢?答案是肯定的。

就如同C++里解决两个相互嵌套类的方案——前置声明一样,布局文件中也有所谓的“前置声明”。

既然问题在于该ID的使用出现在声明之前,那么我们就试图把ID的声明提前。

这里用到了另一个xml配置文件,bill暂且将其命名为myids,在myids.xml中编写如下内容:

 


 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <!-- 手动声明资源ID,类似于C++中的前置声明 --> 
  4.     <item type="id" name="test_bottom_buttons"></item> 
  5. </resources> 

 

这样便手动向系统添加了一个ID test_bottom_buttons 系统会根据这条xml代码在R类中生成对应的int型资源。

我们现在就可以按照实际布局的顺序将xml代码改写如下了

 


 
 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent"> 
  5.  
  6.   <ListView android:id="@+id/lv_test" android:layout_width="fill_parent" 
  7.     android:layout_height="fill_parent" android:layout_above="@id/test_bottom_buttons"> 
  8.   </ListView> 
  9.    
  10.     <!-- Button 置底 --> 
  11.     <LinearLayout android:id="@id/test_bottom_buttons" 
  12.         android:layout_width="fill_parent" android:layout_height="wrap_content" 
  13.         android:orientation="horizontal" android:layout_alignParentBottom="true"> 
  14.  
  15.         <Button android:layout_width="wrap_content" 
  16.             android:layout_height="wrap_content" android:text="确定"></Button> 
  17.  
  18.         <Button android:layout_width="wrap_content" 
  19.             android:layout_height="wrap_content" android:text="取消"></Button> 
  20.  
  21.     </LinearLayout> 
  22.  
  23. </RelativeLayout> 

 

效果如下:

 

 

Buttons置底,且ListView位于其上方

 

 

ADDED(billhoo - 2013-3-18)

感谢网友gogo901的回复,他提到要实现上述行为还有更加简单的方式:

QUOTE[

              可以不换顺序

              List中定义:android:layout_above="@+id/test_bottom_buttons

              Button中定义:android:id="@id/test_bottom_buttons"

]

 

这下可以收工吃东西了~

写了这么多,bill知道,自己只是从表面实验了一下android资源文件编译期的一些特性。但是资源文件的编译顺序究竟是怎么样的?这个过程真的就像是C++的编译吗?如果是,那么其编译器是什么?如果不是,那么这个过程又叫做什么呢?

我想,这些谜题应该会随着自己学习的深入而一一解开吧。

 





     本文转自Bill_Hoo 51CTO博客,原文链接:http://blog.51cto.com/billhoo/764788,如需转载请自行联系原作者





相关文章
|
1月前
|
移动开发 安全 Java
Android历史版本与APK文件结构
通过以上内容,您可以全面了解Android的历史版本及其主要特性,同时掌握APK文件的结构和各部分的作用。这些知识对于理解Android应用的开发和发布过程非常重要,也有助于在实际开发中进行高效的应用管理和优化。希望这些内容对您的学习和工作有所帮助。
187 83
|
5月前
|
ARouter Android开发
Android不同module布局文件重名被覆盖
Android不同module布局文件重名被覆盖
|
5月前
|
数据可视化 Android开发 开发者
安卓应用开发中的自定义View组件
【10月更文挑战第5天】在安卓应用开发中,自定义View组件是提升用户交互体验的利器。本篇将深入探讨如何从零开始创建自定义View,包括设计理念、实现步骤以及性能优化技巧,帮助开发者打造流畅且富有创意的用户界面。
175 0
|
4月前
|
XML 前端开发 Android开发
Android:UI:Drawable:View/ImageView与Drawable
通过本文的介绍,我们详细探讨了Android中Drawable、View和ImageView的使用方法及其相互关系。Drawable作为图像和图形的抽象表示,提供了丰富的子类和自定义能力,使得开发者能够灵活地实现各种UI效果。View和ImageView则通过使用Drawable实现了各种图像和图形的显示需求。希望本文能为您在Android开发中使用Drawable提供有价值的参考和指导。
76 2
|
4月前
|
Android开发 开发者 UED
安卓开发中自定义View的实现与性能优化
【10月更文挑战第28天】在安卓开发领域,自定义View是提升应用界面独特性和用户体验的重要手段。本文将深入探讨如何高效地创建和管理自定义View,以及如何通过代码和性能调优来确保流畅的交互体验。我们将一起学习自定义View的生命周期、绘图基础和事件处理,进而探索内存和布局优化技巧,最终实现既美观又高效的安卓界面。
78 5
|
5月前
|
缓存 数据处理 Android开发
在 Android 中使用 RxJava 更新 View
【10月更文挑战第20天】使用 RxJava 来更新 View 可以提供更优雅、更高效的解决方案。通过合理地运用操作符和订阅机制,我们能够轻松地处理异步数据并在主线程中进行 View 的更新。在实际应用中,需要根据具体情况进行灵活运用,并注意相关的注意事项和性能优化,以确保应用的稳定性和流畅性。可以通过不断的实践和探索,进一步掌握在 Android 中使用 RxJava 更新 View 的技巧和方法,为开发高质量的 Android 应用提供有力支持。
|
5月前
|
缓存 调度 Android开发
Android 在子线程更新 View
【10月更文挑战第21天】在 Android 开发中,虽然不能直接在子线程更新 View,但通过使用 Handler、AsyncTask 或 RxJava 等方法,可以实现子线程操作并在主线程更新 View 的目的。在实际应用中,需要根据具体情况选择合适的方法,并注意相关的注意事项和性能优化,以确保应用的稳定性和流畅性。可以通过不断的实践和探索,进一步掌握在子线程更新 View 的技巧和方法,为开发高质量的 Android 应用提供支持。
90 2
|
5月前
|
XML 前端开发 Android开发
Android面试高频知识点(3) 详解Android View的绘制流程
Android面试高频知识点(3) 详解Android View的绘制流程
Android面试高频知识点(3) 详解Android View的绘制流程
|
5月前
|
XML 前端开发 Android开发
Android面试高频知识点(3) 详解Android View的绘制流程
Android面试高频知识点(3) 详解Android View的绘制流程
47 2
|
XML 前端开发 Android开发
android 前端常用布局文件升级总结(二)
android 前端常用布局文件升级总结(二)
101 0

热门文章

最新文章