Android|布局及标签属性

六大布局:线性布局,相对布局,绝对布局,表格布局,帧布局,网格布局。

线性布局(LinearLayout):
  <LinerLayout>作为根标签。线性布局内控件垂直或者水平排列。通过orientation属性来指定。取值:horizontal(水平)默认值,vertical(垂直)。

  栅格化排列:通过layout_heightlayout_widthlayout_height属性联合控制。

  示例:水平方向3个button平分宽度。指定layout_height都相同,并且layout_width要为0dp。

效果图:
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.Main4Activity">
    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="50dp"
        android:text="我占1格"
        android:textSize="15sp"/>
    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="50dp"
        android:text="我占1格"
        android:textSize="15sp"/>
    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="50dp"
        android:text="我占1格"
        android:textSize="15sp"/>
</LinearLayout>

解释:
  上面示例中我们可以看到一行中3个button的weight加在一起等于3,就说明把宽度平均分成3份,每个button的weight等于1,1/3所以每个button各占一份。当然我们也可以指定让第一个按钮占1格,第二个按钮占2格,第三个按钮占3格。

效果图:
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.Main4Activity">
    <Button
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:text="占1格"
        android:textSize="15sp"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="2"
        android:text="占2格"
        android:textSize="15sp"/>
    <Button
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="3"
        android:text="占3格"
        android:textSize="15sp"/>
</LinearLayout>

相对布局(RelativeLayout):
  <RelativeLayout>作为根标签。相对于 父控件 或者 指定 id 控件 布局。

  布局内控件的常用属性:
    居中:
    layout_centerVertical:指定为true时垂直居中。
    layout_centerHorizontal:指定为true时水平居中。

    相对于 父控件:
    layout_alignParentTop:和父控件顶部对齐。
    layout_alignParentRight:和父控件右侧对齐。
    layout_alignParentBottom:和父控件底部对齐。
    layout_alignParentLeft:和父控件左侧对齐。

    相对于 指定 id 控件:
    layout_above:跟随到指定控件上方。
    layout_below:跟随到指定控件下方。
    layout_toRightOf:跟随到指定控件右侧。
    layout_toLeftOf:跟随到指定控件左侧。
示例:
效果图:
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".activity.Main2Activity">
    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="按钮1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/btn1"
        android:text="按钮2"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_toRightOf="@+id/btn1"
        android:text="按钮3"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_above="@+id/btn1"
        android:text="按钮4"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/btn1"
        layout
        android:text="按钮5"/>
</RelativeLayout>

效果图:
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".activity.Main2Activity">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="按钮1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="按钮2"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="按钮3"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="按钮3"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮3"/>
	<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="按钮3"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="按钮3"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮3"/>
</RelativeLayout>

绝对布局(AbsoluteLayout):

  <AbsoluteLayout>作为根标签。控件通过 layout_xlayout_y来指定其位置。

注:AbsoluteLayout is deprecated.


表格布局(TableLayout):
  <TableLayout>作为根标签。在<TableLayout>标签内使用<TableRow>标签来组织控件。控件对lay_weight属性生效。
效果图:
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    tools:cotext=".MainActivity">
    <TableRow>
        <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"/>
        <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"/>
        <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"/>
        <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"/>
        <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"/>
    </TableRow>
</TableLayout>
余下的两个布局会后续补充。
发布了46 篇原创文章 · 获赞 1 · 访问量 2498
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章