四种基本布局

一、LinearLayout


LinearLayout 又称作线性布局,这个布局会将它所包含的控件在线性方向上依次排列。通过 android:orientation 属性指定排列方向是 vertical(垂直)或 horizontal(水平),默认 horizontal。

  • android:layout_weight

允许我们使用比例的方式来指定控件的大小。注意** android:layout_weight 设为 0 **比较规范。


二、RelativeLayout


RelativeLayout 又称作相对布局,它可以通过相对定位的方式让控件出现在布局的任何位置。

android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_above="@id/button3"
android:layout_below="@id/button3"
android:layout_toLeftOf="@id/button3"
android:layout_toRightOf="@id/button3"

三、FrameLayout


FrameLayout 这种布局没有任何的定位方式,所有的控件都会摆放在布局的左上角


四、TableLayout


TableLayout 允许我们使用表格的方式来排列控件。

<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 account" />
    </TableRow>
    <TableRow>
        <TextView
            android:layout_height="wrap_content"
            android:text="Password:" />
        <EditText
             android:id="@+id/password"
            android:layout_height="wrap_content"
            android:inputType="textPassword" />
    </TableRow>
    <TableRow>
        <Button
             android:id="@+id/login"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:text="Login" />
    </TableRow>
</TableLayout>
  • 使用 android:layout_span="2" 让登录按钮占据两列的空间。
  • 在 TableRow 中无法指定控件的宽度。这时使用 android:stretchColumns,它允许将 TableLayout 中的某一列进行拉伸,以达到自动适应屏幕宽度的作用。android:stretchColumns="1"
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容