基本布局
作用:放置多个控件的容器,可以嵌套多层布局,完成复杂界面的实现。
LinearLayout 线性布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> //控件按垂直方向排列,取“horizontal”水平方向依次排列。默认是按照水平方向排列。
</LinearLayout>
注意:控件水平方向放置,不能指定宽度为match_parent。
同理,垂直放置,不能指定高度为match_parent。
<TextView
android:id="@+id/input_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Typesomething"
/>
<Button
android:id="@+id/send"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Send" />
android:layout_weight="1"//水平方向上平分宽度
也可以修改为其他值,系统会统计界面否认全部值,并根据比例分配界面空间。
RelativeLayout相对布局
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="151dp"
android:layout_toLeftOf="@+id/button3"
android:text="Button1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/button3"
android:text="Button2" />
FrameLayout:所有的控件都会被放置在左上角。
TableLayout 用表格方式排列控件。
TableRow 表示行,里面有几个控件就有几列
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:stretchColumns="1"//拉伸第二列
<TableRow >
<TextView
android:layout_height="wrap_content"
android:text="Account:"/>
<EditText
android:id="@+id/account"
android:layout_height="wrap_content"
android:hint="Input your accunt"
/>
</TableRow>