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值后的效果。

下面是整个布局文件:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal" >
- <EditText android:id="@+id/edit_message"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:hint="@string/edit_message" />
- <Button android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/button_send" />
- </LinearLayout>
这个布局已经被默认建立好的Activity类调用了,所以你已经可以运行这个程序并查看结果了:
在eclipse中的工具栏中点击Run图标。
命令行的话,改变目录到项目的跟目录,执行:
ant debug
adb install bin/MyFirstApp-debug.apk
下一个课程将要学习响应按钮点击事件,读取文本域内容,开启另外一个activity,等等。
