内容均为google官方教程的学习笔记
教程地址:http://developer.android.com/intl/zh-cn/training/basics/firstapp/building-ui.html#LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<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>
实际运行效果
要点:
1 对view可以指定宽度和长度为wrap_content, 即view可以根据其content的长宽来动态适应. 比如上图的text里的输入, 如果换行, 也会撑大text的高度.
2 线性布局中可以用weight属性来分配和填充布局的剩余空间, 如果只有一个view设置了weight, 那就会填充满剩余空间, 需要注意的是设置weight后需要指定宽度为0, 即不再需要固定宽度.