你好,游客 登录 注册 搜索
背景:
阅读新闻

Android Training - 创建一个简单的用户界面

[日期:2012-08-02] 来源:Linux社区  作者:lixiang0522 [字体:大 中 小]

weight的默认值是0,所以如果你仅仅指定大于0的weight值给一个视图,那么这个视图会填充其他所有视图占用必须的空间后剩下的所以空间 。因此,不设置按钮的weight值,设置EditText的weight值为1,让它占用剩下的空间。
<EditText
        Android:layout_weight="1"
        ... />


为了提高布局性能,当你指定weight值的时候,你需要修改width为0(0dp),因为使用"wrap_content"的话,系统会去计算它的宽度,而这个计算出来的宽度在最后是没有意义的,因为weight值需要另外计算宽度以便填充剩下的空间。
<EditText android:id="@+id/edit_message"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/edit_message" />


下面这个图是显示的是你给EditText元素分配了weight值后的效果。

下面是整个布局文件:

  1. <LinearLayout 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:orientation="horizontal" >  
  6.   
  7.     <EditText android:id="@+id/edit_message"  
  8.         android:layout_width="0dp"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_weight="1"  
  11.         android:hint="@string/edit_message" />  
  12.       
  13.     <Button android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="@string/button_send" />  
  16.   
  17. </LinearLayout>  

这个布局已经被默认建立好的Activity类调用了,所以你已经可以运行这个程序并查看结果了:

在eclipse中的工具栏中点击Run图标。

命令行的话,改变目录到项目的跟目录,执行:

ant debug

adb install bin/MyFirstApp-debug.apk

下一个课程将要学习响应按钮点击事件,读取文本域内容,开启另外一个activity,等等。

linux
相关资讯       Android程序设计  Android Training 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款