Android的约束布局,表格布局及线性布局(对学习帮助不大,非详细解说)

Android的约束布局,表格布局及线性布局


一、约束布局(ConstraintLayout)的创建及使用,及个人的相关实验说明

有关于此布局的创建和使用,推荐看一看ConstraintLayout的创建和使用
看完之后开始拖拽Textview组件进入布局,输入每个组件的内容,然后调整背景颜色,成品如图:
在这里插入图片描述
每一个组件调整位置,建议使用右边的Attributes工具栏中的:
在这里插入图片描述
笔者在这里不一一举例,创建了布局之后可以慢慢调整慢慢体会这个布局。
布局调整完毕之后,代码如下(代码会在调整过程中自动生成):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:background="@android:color/black">

    <TextView
        android:id="@+id/textView"
        android:layout_width="144dp"
        android:layout_height="77dp"
        android:background="@android:color/holo_red_dark"
        android:gravity="center"
        android:text="@string/red"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="150dp"
        android:layout_height="77dp"

        android:background="@android:color/holo_orange_dark"
        android:gravity="center"
        android:text="@string/orange"
        app:layout_constraintEnd_toStartOf="@+id/textView3"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="144dp"
        android:layout_height="77dp"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="@string/yellow"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="126dp"
        android:layout_height="95dp"
        android:layout_marginStart="134dp"
        android:layout_marginLeft="134dp"
        android:layout_marginTop="134dp"
        android:background="@android:color/holo_green_light"
        android:gravity="center"
        android:text="@string/green"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="97dp"
        android:layout_height="95dp"
        android:layout_marginTop="57dp"
        android:background="@android:color/holo_blue_dark"
        android:gravity="center"
        android:text="@string/blue"
        app:layout_constraintEnd_toStartOf="@+id/textView6"
        app:layout_constraintStart_toEndOf="@+id/textView4"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="126dp"
        android:layout_height="95dp"
        android:layout_marginTop="134dp"
        android:layout_marginEnd="134dp"
        android:layout_marginRight="134dp"
        android:background="@android:color/holo_purple"
        android:gravity="center"
        android:text="@string/purple"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="0dp"
        android:layout_height="80dp"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:text="@string/violef"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

二、表格布局 (Tablelayout)

表格布局也不难,也是创建和使用,创建就按下图所示创建就行(右键layout->new):
在这里插入图片描述
②默认是线性布局(LinearLayout)改成Tablelayout
在这里插入图片描述
关于使用,粗略使用就是表格行(TableRow),还有表格的一些属性,可以在这个作者写的博客里简单了解一下:
表格布局的属性及应用


总而言之我们最后用表格布局做了一个菜单栏,它长这样:
在这里插入图片描述
自动生成的代码是这样的:

<?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"
    android:background="@android:color/background_dark"
    android:stretchColumns="1">

    <TableRow>

        <TextView
            android:padding="3dp"
            android:text="open..."
            android:textColor="@android:color/background_light" />

        <TextView
            android:gravity="right"
            android:padding="3dp"
            android:text="crtl-o"
            android:textColor="@android:color/background_light" />
    </TableRow>

    <TableRow>

        <TextView
            android:padding="3dp"
            android:text="save..."
            android:textColor="@android:color/background_light" />

        <TextView
            android:gravity="right"
            android:padding="3dp"
            android:text="ctrl-s"
            android:textColor="@android:color/background_light" />

    </TableRow>

    <TableRow>

        <TextView
            android:padding="3dp"
            android:text="save as..."
            android:textColor="@android:color/background_light" />

        <TextView
            android:gravity="right"
            android:padding="3dp"
            android:text="crtl-shift-s"
            android:textColor="@android:color/background_light" />
    </TableRow>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/colorAccent" />

    <TableRow>

        <TextView
            android:padding="3dp"
            android:text="X Import"
            android:textColor="@android:color/background_light" />
    </TableRow>

    <TableRow>

        <TextView
            android:padding="3dp"
            android:text="X Export..."
            android:textColor="@android:color/background_light" />

        <TextView
            android:gravity="right"
            android:padding="3dp"
            android:text="crtl-E"
            android:textColor="@android:color/background_light" />
    </TableRow>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/colorAccent" />

    <TableRow>

        <TextView
            android:padding="3dp"
            android:text="Quit"
            android:textColor="@android:color/background_light" />

    </TableRow>
</TableLayout>

三、线性布局

线性布局的创建类似于表格布局,这里不多赘述。
我也不知道怎么说,有些东西都是做的时候慢慢领悟的,比如线性布局的嵌套,你观察下面代码,可以看到一层线性布局里面套了4个线性布局,再使用垂直排布android:orientation="vertical"的属性,才可以使4个线性布局展开。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:gravity="right"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="RtlHardcoded">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <Button
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:text="@string/bn1"
        tools:ignore="ButtonStyle" />
    <Button
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:text="@string/bn2"
        tools:ignore="ButtonStyle" />
    <Button
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:text="@string/bn3"
        tools:ignore="ButtonStyle" />
    <Button
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:text="@string/bn4"
        tools:ignore="ButtonStyle" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:text="@string/bn5"
            tools:ignore="ButtonStyle" />
        <Button
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:text="@string/bn6"
            tools:ignore="ButtonStyle" />
        <Button
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:text="@string/bn7"
            tools:ignore="ButtonStyle" />
        <Button
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:text="@string/bn8"
            tools:ignore="ButtonStyle" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:text="@string/bn9"
            tools:ignore="ButtonStyle" />
        <Button
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:text="@string/bn10"
            tools:ignore="ButtonStyle" />
        <Button
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:text="@string/bn11"
            tools:ignore="ButtonStyle" />
        <Button
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:text="@string/bn12"
            tools:ignore="ButtonStyle" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:text="@string/bn13"
            tools:ignore="ButtonStyle" />
        <Button
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:text="@string/bn14"
            tools:ignore="ButtonStyle" />
        <Button
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:text="@string/bn15"
            tools:ignore="ButtonStyle" />
        <Button
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:text="@string/bn16"
            tools:ignore="ButtonStyle" />
    </LinearLayout>

</LinearLayout>

最后成果如图:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
深度学习是一种机器学习技术,它的核心原理是通过构建多层神经网络,从数据中学习并提取出高级别的特征表达,以实现分类、识别、回归等任务。以下是深度学习详细解释: 1. 神经网络 神经网络是深度学习的核心组成部分。它由多个神经元(也称为节点)组成,每个神经元接收来自前一层神经元的输入,并输出给下一层神经元。每个神经元的输出经过一个线性激活函数进行处理,以产生线性的特征表示。整个神经网络通常由多个层组成,每个层都包含多个神经元。 2. 反向传播 反向传播是深度学习中的一种优化算法,它的目的是通过调整神经网络中的参数,使得网络的输出尽可能接近真实值。反向传播算法通过计算损失函数的梯度来更新网络中的参数,使得损失函数值最小化。这个过程是迭代进行的,通常需要多次迭代才能达到最优值。 3. 损失函数 损失函数是深度学习中用来衡量模型预测与真实值之间差异的函数。常见的损失函数包括均方误差、交叉熵等。深度学习的优化目标是最小化损失函数,使得模型的预测尽可能接近真实值。 4. 正则化 正则化是一种用于控制模型复杂度的技术。由于深度学习模型通常包含大量的参数,容易出现过拟合现象。正则化通过引入惩罚项来限制模型参数的大小,从而减少模型的复杂度。 5. 梯度消失 深度学习中的梯度消失问题是指,在反向传播算法中,当梯度逐层传播时,梯度值会逐渐变小,导致低层神经元的参数更新常缓慢,甚至不更新。为了解决这个问题,研究者们提出了很多方法,如使用ReLU等激活函数、批标准化等技术。 总之,深度学习的原理是通过构建多层神经网络,使用反向传播算法不断更新网络参数,使得模型可以从数据中学习高级别的特征表达,并实现各种机器学习任务。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值