最近碰到一个面试题,按照下图,由Button和EditText组成的界面下厨布局代码,解决这题目需要使用android:layout_weight的知识。

关于android:layout_weight属性的一个面试题_第1张图片" width="438" height="729" style="border:1px solid black;">

首先分析上图所示的界面可以看成一下3个部分。

  1. 界面顶端的3个按钮。
  2. 界面中间的EditText。
  3. 界面底端的1个按钮。
  • 其中第1部分和第3部分分别在界面顶端和底端显示,而第2部分的EditText充满了剩余的空间。
  • 如果想让一个组件充满整个屏幕,需要将android:layout_width和android:layout_height都设成fill_parent或者match_parent。我们可以将第1部分看成一个整体,android:layout_height的值设成wrap_content,第3部分的android:layout_height的值也设成wrap_content,那么第2部分的<EditText>标签的android:layout_weight属性值要设为一个大于0的浮点数(例如1)。这样EditText就会充满整个剩余空间。否则EditText会占用第3部分的Button空间,这样界面底端的按钮就会显示不出来。
  • 现在来看第1部分的3个按钮。这3个按钮分钟占了水平宽度的1/5、3/5、1/5。这种按比例摆放的组件一般都需要设置标签的android:layout_weight属性。按重要程度可将3个Button的android:layout_weight属性从左到右依次设为2、1、2。
具体的实现布局代码如下所示:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <!-- 顶端的3个按钮布局 -->    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <!-- 该按钮占1/5宽度 -->        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="2"            android:text="1/5" />                <!-- 该按钮占3/5宽度 -->        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="3/5" />                <!-- 该按钮占1/5宽度 -->        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="2"            android:text="1/5" />    </LinearLayout>        <!-- 充满剩余空间的EditText组件 -->    <EditText        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_weight="1"        android:text="EditText(充满上方和下方按钮之间的整个屏幕\n博客地址:http://blog.csdn.net/ouyang_peng)" />    <!-- 屏幕底端的按钮 -->    <Button android:layout_width="match_parent"        android:layout_height="wrap_content" android:text="按钮"/></LinearLayout>


可以通过另外一篇实例文章来了解android:layout_weight的属性,链接如下:

《我的Android进阶之旅------>Android利用温度传感器实现带动画效果的电子温度计》

PS:地址为:http://blog.csdn.net/ouyang_peng/article/details/48790289



关于android:layout_weight的更多介绍可以参考一下资源:
  1. http://www.cnblogs.com/angeldevil/archive/2012/04/08/2437747.html
  2. http://blog.csdn.net/softmanfly/article/details/7636510
  3. http://developer.android.com/guide/topics/ui/layout/linear.html#Weight
  4. http://www.cnblogs.com/draem0507/archive/2013/05/11/3073508.html
  5. Android 对Layout_weight属性完全解析以及使用ListView来实现表格

==================================================================================================

作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!

转载请保留原文地址:http://blog.csdn.net/ouyang_peng

==================================================================================================


更多相关文章

  1. Android最佳性能实践(四)——布局优化技巧
  2. 转:一个Demo学完Android中所有的服务
  3. [Android初级]android单元测试之Activity单独测试
  4. Android(安卓)中实现在界面上右划返回上一个界面
  5. Android搜索框输入内容点击键盘的搜索按钮进行搜索
  6. Android中ImageView控件的ScaleType属性使用详解
  7. Android(安卓)MVVM架构模式 详解和综合运用(一)
  8. android仿腾讯安全管家首页抽屉效果
  9. Android(安卓)MVC模式实例(一)

随机推荐

  1. Android(安卓)app 与 ActivityManagerSer
  2. [Android] 你真的了解Activity吗?
  3. react-native开发实例之code-push更新(and
  4. IOS类似android的view.gone/view.visable
  5. Android(安卓)AOP注解Annotation详解(一)
  6. Android代码覆盖率初探—问题已解决!
  7. 最简单的Android更新组件~
  8. SDK Platform Android(安卓)无法更新?
  9. Android(安卓)Studio 管理打包版本 起步
  10. 在Android中将子View的坐标转换为父View