ConstraintLayout 简单实用

ConstraintLayout(约束布局)是在2016的Google I/O大会上发布的。

  • 支持Android 2.3(api 9 +)
  • 单独的依赖包
  • ConstraintLayout的实现基于食火鸟算法(Cassowary Algorithm),它是一个高效的约束解决方案。
  • 优势:可以解决布局过度嵌套,效率低下,还可以实现复杂动画。
image.png
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <!--layout_constraintXX_toYYOf  xx 代表当前组件 YY代表对方组件  -->
    <!--layout_constraintDimensionRatio  比列 -->
    <!-- android:layout_width="0dp"   当文本信息过长的时候,文本信息会覆盖图片组件,设置为0dp
    就可以解决了 -->
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.03"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_launcher_background" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginStart="16dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/imageView"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="HELLO___))))))))))))))))))))))))))))) )))666))" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginStart="16dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/imageView"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        tools:text="XXXXXXXYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYX" />
</android.support.constraint.ConstraintLayout>

注意: 提出一个问题,如果我想让第二行的文本信息和图片底部对齐怎么实现呢?欢迎大家回复我。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。