美文网首页
ConstraintLayout解析

ConstraintLayout解析

作者: 云木杉 | 来源:发表于2019-08-21 16:10 被阅读0次

学习

由于RecyclerView的滑动卡顿,促使我不得已去研究卡顿问题,查了很多资料,讲说ViewGroup嵌套层次太多的原因几率比较大,相比较而言也没有那么难,所以就学习一下,且记录一下。

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

    <TextView
        android:id="@+id/banner"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#765"
        android:gravity="center"
        android:text="Banner"
        app:layout_constraintDimensionRatio="H,16:6"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/tv"
        android:layout_width="140dp"
        android:layout_height="86dp"
        android:background="#fd3"
        android:gravity="center"
        android:text="left instance"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/banner"/>

    <TextView
        android:id="@+id/tv2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="12dp"
        android:text="马云:一年交税170多亿马云:一年交税170多亿马云:一年交税170多亿"
        android:textColor="#000000"
        android:textSize="16dp"
        app:layout_constraintLeft_toRightOf="@+id/tv"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/banner"/>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="12dp"
        android:text="八分钟前"
        app:layout_constraintBottom_toBottomOf="@id/tv"
        app:layout_constraintLeft_toRightOf="@+id/tv"/>


    <TextView
        android:id="@+id/tv_bar1"
        android:layout_width="0dp"
        android:layout_height="42dp"
        android:background="#f67"
        android:gravity="center"
        android:text="bar1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/tv_bar2"/>

    <TextView
        android:id="@+id/tv_bar2"
        android:layout_width="0dp"
        android:layout_height="42dp"
        android:background="#a67"
        android:gravity="center"
        android:text="bar1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toRightOf="@+id/tv_bar1"
        app:layout_constraintRight_toLeftOf="@+id/tv_bar3"/>

    <TextView
        android:id="@+id/tv_bar3"
        android:layout_width="0dp"
        android:layout_height="42dp"
        android:background="#767"
        android:gravity="center"
        android:text="bar1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintLeft_toRightOf="@+id/tv_bar2"
        app:layout_constraintRight_toRightOf="parent"/>

    <TextView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="#612"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.9"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.9"/>


    <android.support.constraint.Guideline
        android:id="@+id/guideline_h"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.8"/>


    <android.support.constraint.Guideline
        android:id="@+id/guideline_w"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.8"/>

    <TextView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="#612"
        app:layout_constraintLeft_toRightOf="@id/guideline_w"
        app:layout_constraintTop_toBottomOf="@id/guideline_h"/>


</android.support.constraint.ConstraintLayout>

常用的几个方法

  • 当前View的右侧与tv的左侧对齐
    layout_constraintRight_toLeftOf="@id/tv"

  • 当前View的右侧与tv的右侧对齐
    layout_constraintRight_toRightOf="@id/tv"

  • 当前View的上方与tv的上方对齐
    layout_constraintTop_toTopOf="@id/tv"

  • 当前View的上方与tv的下方对齐
    layout_constraintTop_toBottomOf="@id/tv"

  • 当前View的下方与tv的上方对齐
    layout_constraintBottom_toTopOf="@id/tv"

  • 当前View的下方与tv的下方对齐
    layout_constraintBottom_toBottomOf="@id/tv"

  • 当前View
    layout_constraintBaseline_toBaselineOf="@id/tv"

  • 宽与高的比例
    app:layout_constraintDimensionRatio="H,16:6"

  • 横向权重
    app:layout_constraintHorizontal_weight="1"

  • 悬浮窗 上下拉的比例
    layout_constraintHorizontal_bias="0.9"
    layout_constraintVertical_bias="0.9"

相对定位

  • 居中时 偏差
    app:layout_constraintHorizontal_bias="0.4"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"

  • 居中显示
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"/>

  • 约束优化器
    app:layout_optimizationLevel

参考文档
 * 参考线
 * https://developer.android.google.cn/reference/android/support/constraint/Guideline
 * 分组控制隐藏/展示
 * https://developer.android.google.cn/reference/android/support/constraint/Group
 * 组视图边缘线
 * https://developer.android.google.cn/reference/android/support/constraint/Barrier

相关文章

网友评论

      本文标题:ConstraintLayout解析

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