美文网首页
关于ConstraintLayout的小Tips

关于ConstraintLayout的小Tips

作者: 冷师傅_ | 来源:发表于2019-07-20 17:59 被阅读0次

    最近在研究MotionLayout的动画,发现了一个有意思的动画,在研究的过程中发现了一个关于ConstraintLayout定位的小技巧。

    准备知识

    • 实现View1的一边贴紧View2某一边居中**

      image.png
    • 代码实现

    <?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"
                                                       android:orientation="vertical"
                                                       android:layout_width="match_parent"
                                                       android:layout_height="match_parent">
    
        <View android:layout_width="0dp" android:layout_height="100dp"
              android:layout_marginTop="100dp"
              android:background="@color/colorPrimary"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintLeft_toLeftOf="parent"
              android:id="@+id/view"
        />
    
        <View android:layout_width="100dp" android:layout_height="100dp"
              android:id="@+id/View2"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent"
              android:background="@android:color/holo_red_dark"
              app:layout_constraintTop_toBottomOf="@+id/view"
              app:layout_constraintBottom_toBottomOf="@+id/view"
              android:textSize="20sp"
              android:text="hahahahahah"
        />
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    小技巧

    • View2高度变为0
    • 代码实现,上边的布局代码基本不变,View2宽高为0即可
      image.png

    总结

    这个小Tips在我们实现MotionLayout动画的时候应该会用的上。

    相关文章

      网友评论

          本文标题:关于ConstraintLayout的小Tips

          本文链接:https://www.haomeiwen.com/subject/acvulctx.html