5分钟上手之ConstraintLayou使用进阶篇(二)

在上一篇已经把ConstraintLayou 基本使用已经掌握了,有上面基本已经可以解决80%日常布局所需,在掌握本篇你就可以再新布局上面所向披靡了,Ok,让我我们来看看进阶使用教程。

一、OKOK,先来个小菜!!设置图片比例

特别注意:需要设定比例必须是有一个方向上宽或者高固定,另外一个方向是不确定的,否则不起效果
在这里插入图片描述
上图是以宽度固定来调整整体的比例,对应以高位标准也可以的

二、角度定位

在这里插入图片描述
一共有3个属性需要注意:
在这里插入图片描述

<!-- 这个是基准View的ID绑定 -->
app:layout_constraintCircle="@+id/imageView3"
<!-- 这个是以基准View标准,角度偏移量,注意没有单位 -->
app:layout_constraintCircleAngle="120"
<!-- 这个是以基准View标准,2个之前的间隔距离,注意有单位 -->
app:layout_constraintCircleRadius="90dp"

三、Chain(链条)

当遇到多个条目需要平分距离的时候,你就需要用到这个了。
在这里插入图片描述
这里选中我是直接框选的,还可以按住command (mac) 或者 ctrl (win) 加鼠标点击来选中。
链条一种有2种,一个是竖着的,一个是横着的。
在这里插入图片描述
然后每种有3小种形式,如图!就不多介绍了。

四、最后ConstraintLayou动画

虽然大概率用不上,因为需求不设计如此,你也不会自己给自己找麻烦?
但还是详细说下。这里只是做了一个简单的,根据需求来实现吧,下面看下效果

在这里插入图片描述
下面简单说下过程
第一,你需要准备2个xml文件,一个是动画之前,一个是动画之后的效果
下面贴下动画之前xml布局
activity_main.xml

<?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:id="@+id/main_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="215dp"
        android:layout_height="163dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_go"
        app:srcCompat="@mipmap/beautiful_girl_4" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:ellipsize="end"
        android:maxLines="6"
        android:text="I love waking up in the morning and not knowing what’s going to happen, or who I’m going to meet, where I’m going to wind up。The road of life is like a large river,because of the power of the currents,river courses appear unexpectedly where there is no flowing water.(Tagore India)Dare and the world always yields. If it beats you sometimes, dare it again and again and it will succumb."
        app:layout_constraintBottom_toBottomOf="@+id/imageView5"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/imageView5"
        app:layout_constraintTop_toTopOf="@+id/imageView5" />

    <Button
        android:id="@+id/btn_go"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="88dp"
        android:text="走你"
        app:layout_constraintBottom_toBottomOf="@+id/btn_back"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/btn_back"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/btn_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="48dp"
        android:layout_marginTop="8dp"
        android:text="回来"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.181"
        app:layout_constraintStart_toEndOf="@+id/btn_go"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

再贴下动画之后的布局
activity_main_after.xml

<?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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="0dp"
        android:layout_height="250dp"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_go"
        app:srcCompat="@mipmap/beautiful_girl_4" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="I love waking up in the morning and not knowing what’s going to happen, or who I’m going to meet, where I’m going to wind up。The road of life is like a large river,because of the power of the currents,river courses appear unexpectedly where there is no flowing water.(Tagore India)Dare and the world always yields. If it beats you sometimes, dare it again and again and it will succumb."
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView5" />

    <Button
        android:id="@+id/btn_go"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="88dp"
        android:text="走你"
        app:layout_constraintBottom_toBottomOf="@+id/btn_back"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/btn_back"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/btn_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="48dp"
        android:layout_marginTop="8dp"
        android:text="回来"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.181"
        app:layout_constraintStart_toEndOf="@+id/btn_go"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

看下怎么玩的

public class MainActivity extends AppCompatActivity {

    private ConstraintLayout mMainLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mMainLayout = findViewById(R.id.main_view);
        //创建2个约束的集合
        final ConstraintSet constraintSet1 = new ConstraintSet();
        final ConstraintSet constraintSet2 = new ConstraintSet();
        //复制各自布局的约束
        constraintSet1.clone(this, R.layout.activity_main);
        constraintSet2.clone(this, R.layout.activity_main_after);

        findViewById(R.id.btn_go).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //通过TransitionManager来执行过渡动画
                TransitionManager.beginDelayedTransition(mMainLayout);
                constraintSet2.applyTo(mMainLayout);
            }
        });

        findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TransitionManager.beginDelayedTransition(mMainLayout);
                constraintSet1.applyTo(mMainLayout);
            }
        });

    }
}

好了,就是这么简单。

到这基本就已经介绍完所有的ConstraintLayou在日常会用的到的了,学会这个已经可以完成日常开发的99%了,如果中见有遗漏或者错误,请留言,我会及时更正的。

最后下篇会介绍 ConstraintLayou 一些好用技巧和开发中可能会遇到的坑,敬请期待吧。。。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 1. 安装TypeScript:使用 npm 命令 "npm install -g typescript" 安装 TypeScript。 2. 创建TypeScript文件:创建一个名为 "main.ts" 的文件,在其中编写 TypeScript 代码。 3. 编译TypeScript文件:使用命令 "tsc main.ts" 编译 TypeScript 文件,生成同名的 JavaScript 文件。 4. 运行JavaScript文件:使用命令 "node main.js" 运行编译生成的 JavaScript 文件。 5. 添加类型注解:在变量前加上类型注解,如 "let message: string = 'Hello World'"。 6. 添加类型检查:使用 "tsc --noEmit" 命令只进行类型检查而不生成 JavaScript 文件。 这些步骤可以帮助你在5分钟内快速上手 TypeScript。 ### 回答2: TypeScript是一种静态类型的编程语言,它是JavaScript的超集。要快速上手TypeScript,你可以按照以下步骤进行: 1. 安装TypeScript:首先,你需要在你的计算机上安装TypeScript。你可以使用Node.js包管理器(npm)或者使用TypeScript官方提供的安装包进行安装。 2. 创建TypeScript文件:在你的项目目录下,创建一个以`.ts`扩展名结尾的TypeScript文件。例如,你可以创建一个名为`example.ts`的文件。 3. 编写TypeScript代码:在刚才创建的TypeScript文件中,你可以使用JavaScript的语法以及TypeScript的类型注解来编写代码。TypeScript提供了一些新的语法和特性,例如类、接口、泛型等。 4. 编译TypeScript代码:在终端中,进入到你的项目目录,并使用TypeScript编译器(tsc)将TypeScript代码编译成JavaScript代码。你可以使用以下命令进行编译: ``` tsc example.ts ``` 5. 运行JavaScript代码:在编译成功后,你将得到一个与TypeScript文件同名的JavaScript文件(`example.js`)。你可以使用Node.js或者在浏览器中运行该JavaScript文件来查看结果。 以上就是使用TypeScript快速上手的基本步骤。当你熟悉了TypeScript的基本语法和特性后,你可以进一步学习TypeScript的高级特性,如模块化、异步编程等。还可以结合一些TypeScript的开发工具,如编辑器插件、调试工具等,来提升你的开发效率。 ### 回答3: 要快速上手TypeScript,你可以按照以下步骤进行操作: 1. 安装TypeScript:首先,你需要在你的计算机上安装TypeScript编译器。你可以通过npm(Node Package Manager)来安装它,使用下面的命令: ``` npm install -g typescript ``` 2. 创建一个TypeScript文件:在你的项目中,创建一个后缀名为`.ts`的TypeScript文件。 3. 确定编译选项:在你的项目根目录下创建一个`tsconfig.json`文件,用于配置TypeScript编译器的选项。你可以根据需要选择不同的选项,比如目标版本、模块系统等。 4. 编写TypeScript代码:在TypeScript文件中编写你的代码。TypeScript是一种强类型的编程语言,它提供了类、接口、枚举等丰富的语言特性。 5. 编译TypeScript代码:使用以下命令将你的TypeScript代码编译为JavaScript代码: ``` tsc ``` 6. 运行JavaScript代码:在编译成功后,你将得到一个后缀名为`.js`的JavaScript文件。你可以使用任何支持JavaScript的环境(如浏览器、Node.js等)来运行这些代码。 7. 运行时类型检查:TypeScript还提供了运行时类型检查的功能。你可以使用像`typeof`、`instanceof`等运算符来进行类型检查,确保程序的运行过程中类型的正确性。 通过按照上述步骤进行操作,你就可以快速上手TypeScript,并开始使用它来开发你的项目了。当然,为了更好地掌握TypeScript的更高级特性和最佳实践,你可能需要进一步学习和实践。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值