Android基础知识回顾——Android布局

Android的常用布局

  • 布局是一种可用于放置很多控件的容器,它可以按照一定的规律调整内部控件的位置,从而编写出精美的界面。下面就来详细介绍一下Android的几种布局

线性布局

  • 线性布局是一种非常常用的布局,正如它的名字描述的一样,它会将包含的控件在线性方向上依次排列,我们可以通过android:orientation属性来控制它排列的方向,如果指定的是vertical,控件会在垂直方向上排列,如果指定的是horizontal,则控件在水平方向上排列,我们可以写一段代码演示一下
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.example.yang.test04.MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3" />

</LinearLayout>
  • 我们在LinearLayout中添加了3个Button,指定排列方向为vertical,现在运行下程序
    这里写图片描述
  • 然后我们修改下LinearLayout的排列方式,将vertical改成horizontal,再运行下程序
    这里写图片描述
  • 可以看到,按钮的排列方式已经变成水平的了,这里需要注意一点,如果LinearLayout的排列方式是horizontal,那么内部的控件就不能将宽度指定为match_parent,这样会导致单独一个控件将整个水平方向占满。同理,如果排列方式是vertical,那么内部控件的高度不能指定为match_parent

相对布局

  • 相对布局也是一种常用的布局,和线性布局的排列方式不同,相对布局是通过相对定位的方式让控件出现在布局的任何位置
  • 我们来看一段代码
<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"
    android:orientation="horizontal"
    tools:context="com.example.yang.test04.MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn1"
        android:text="Button 2" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn1"
        android:layout_toRightOf="@id/btn2"
        android:text="Button 3" />

</RelativeLayout>
  • 运行这段程序
    这里写图片描述
  • 这段代码有几个属性,其中android:layout_below属性作用是使该控件处于另一个控件的下方,而android:layout_toRightOf属性作用是使该控件位于另一个控件的右边,相对布局就是这样通过各种属性来排列布局的位置

帧布局

  • 帧布局相对于前面两种布局简单了许多,因此它的应用场景也比较少,这种布局所有的控件都会默认摆放在布局的左上角。
<FrameLayout 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"
    android:orientation="horizontal"
    tools:context="com.example.yang.test04.MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView TextView" />

</FrameLayout>
  • 运行结果
    这里写图片描述
  • 可以看到Button按钮压在了文字的上面,当然我们也可以使用其他的属性调整控件的对齐方式

表格布局

  • 表格布局适用于需要N行N列的布局格式比如九宫格,一个TableLayout中由许多TableRow组成,一个TableRow就是代表一行,如果向TableLayout中直接添加控件,则直接占据一行,如果向TableRow中添加控件,则每增加一个控件TableRow就增加一列
<TableLayout 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"
    android:orientation="horizontal"
    tools:context="com.example.yang.test04.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView 1" />

    <TableRow>

        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView 2" />
    </TableRow>

</TableLayout>
  • 运行结果
    这里写图片描述
  • 可以看到,TextView 1 单独占据了一行,而 Button 1与TextView 2 则在同一行
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值