3.2关于布局的那些事~

来源:互联网 发布:会声会影软件百度云 编辑:程序博客网 时间:2024/06/07 00:38

1.LinearLayout

android:orientation="horizontal"意味着要让LinearLayout中的控件在水平方向上依次排列,

        ="vertical"意味着在垂直方向上依次排列。
android:layout_gravity="center_vertical“指定控件在布局中的对齐方式为垂直方向上居中。

                                       =“top”若android:orientation="horizontal",则意味着在垂直方向上居顶部。

                                       =”bottom“意味着在垂直方向上居底部。

android:layout_weight允许我们使用比例的方式来指定控件的大小。

   系统会先把 LinearLayout 下所有控件指定的 layout_weight 值相加, 得到一个总值,然后每个控件所占大小的比例就是用该控件的layout_weight值除以刚才算出的总值。

如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    ><EditText    android:id="@+id/input_message"    android:layout_width="0dp"    android:layout_height="wrap_content"    android:layout_weight="1"/>    <Button        android:id="@+id/send"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"/></LinearLayout>
Button控件和EditText控件的layout_weight值相加为2,故EditText和Button水平方向上分别占一半。

另,若某一控件a的没有指定layout_weight属性,则其他有layout_weight属性的控件b,c分享除了a剩余的空间。如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    ><EditText    android:id="@+id/input_message"    android:layout_width="0dp"    android:layout_height="wrap_content"    android:layout_weight="1"/>    <Button        android:id="@+id/send"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"/>    <Button        android:id="@+id/test"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="test"/></LinearLayout>
得到的界面就是:


2.RelativeLayout

相对于父布局

//左上

android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"

//右上

android:layout_alignParentRight="true"
android:layout_alignParentTop="true"

//中间

android:layout_centerInParent="true"

//左下

android:layout_alignParentBottom="true"114
android:layout_alignParentLeft="true"

//左下

android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"


相对于控件

2.1.

android:layout_above属性可以让一个控件位于另一个控件的上方,

android:layout_below表示让一个控件位于另一个控件的下方

android:layout_toLeftOf表示让一个控件位于另一个控件的左侧,

android:layout_toRightOf表示让一个控件位于另一个控件的右侧

如:

<Button    android:id="@+id/button5"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_above="@id/button4"    android:layout_toLeftOf="@id/button4"    android:text="left_to_button4"    />
button5就在button4的左上方

2.2

android:layout_alignRight表示一个控件的右边缘与另一个空间的右边缘对齐

<Button    android:id="@+id/button6"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_alignLeft="@+id/button5"    android:text="I'm button6"    />
button6与button5的左边缘对齐

3.FrameLayout

所有的控件都会摆放在布局的左上角,即各个控件都是重叠在一起的~

4.TableLayout

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

<TableRow>标签确定了一行,标签内的控件组成各列

android:layout_span="2"表示该控件占2列

android:stretchColumns="1"表示第如果表格不能占满整个屏幕,就将第二列进行拉伸,=“0”就将第一列进行拉伸。

<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:id="@+id/account"            android:layout_height="wrap_content"            android:text="account"            />        <EditText            android:id="@+id/edit_account"            android:layout_height="wrap_content"            android:hint="put in your account"            />    </TableRow>    <TableRow>        <TextView            android:id="@+id/password"            android:layout_height="wrap_content"            android:text="password"            />        <EditText            android:id="@+id/edit_password"            android:layout_height="wrap_content"            android:hint="put in your password"            />    </TableRow>    <TableRow>        <Button            android:id="@+id/button7"            android:layout_height="wrap_content"            android:layout_span="2"            android:text="submit"            />    </TableRow></TableLayout>





0 0
原创粉丝点击