您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页四种基本布局

四种基本布局

来源:二三娱乐

一、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 
    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"

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务