美文网首页
ConstraintLayout基本使用

ConstraintLayout基本使用

作者: Bernardo_Silva | 来源:发表于2019-03-24 16:06 被阅读0次
  • 先通过一个简单的例子来看使用方式,主要用到的相对位置
 <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:background="#11ffff00"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:gravity="center"
        android:id="@+id/banner"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:background="#259"
        app:layout_constraintDimensionRatio="H,10:3"
        android:layout_width="0dp"
        android:layout_height="0dp" />

    <TextView
        app:layout_constraintTop_toBottomOf="@id/banner"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:id="@+id/tv1"
        android:background="#2d4"
        android:layout_width="140dp"
        android:layout_height="85dp" />
    
    <TextView
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        app:layout_constraintTop_toTopOf="@id/tv1"
        android:id="@+id/tv2"
        android:textSize="15sp"
        app:layout_constraintLeft_toRightOf="@id/tv1"
        app:layout_constraintRight_toRightOf="parent"
        android:text="几个凯撒的管理会计师的理科高考了坚实的离开公交卡了十多个对方的说法"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />

    <TextView
        android:textSize="12sp"
        android:layout_marginRight="10dp"
        android:id="@+id/tv3"
        app:layout_constraintLeft_toLeftOf="@id/tv2"
        app:layout_constraintBottom_toBottomOf="@id/tv1"
        app:layout_constraintRight_toRightOf="parent"
        android:text="3个月前"
        android:layout_width="0dp"
        android:layout_height="15dp" />

</android.support.constraint.ConstraintLayout>

效果图如下:


代码实现的效果.jpg
  • 可以使用以下属性替代LinearLayout的weight
app:layout_constraintHorizontal_weight
  • 可以使用不同的链类型,默认为spread,另外两种为spread_inside和packed
layout_constraintHorizontal_chainStyle
  • 各种链类型的效果见官网的图,比较清晰


    chains-styles.png
  • 可以通过下面两个属性设置拉力偏差
layout_constraintHorizontal_bias
layout_constraintVertical_bias

例如以下的设置,表示上下的拉力为2比8,即上面的间隙为80%,下面的间隙为20%

app:layout_constraintVertical_bias="0.8"
  • 可以使用圆形约束定位
layout_constraintCircle : references another widget id
layout_constraintCircleRadius : the distance to the other widget center
layout_constraintCircleAngle : which angle the widget should be at (in degrees, from 0 to 360)

主要是使用这个三个属性,可以设置一个View相对另外一个View的圆心的角度和距离,看图


circle2.png
  • 强制约束的使用
app:layout_constrainedWidth=”true”
app:layout_constrainedHeight=”true”
  • 设置相对于父布局的百分比
layout_constraintWidth_percent
layout_constraintHeight_percent

-设置宽高比

layout_constraintDimensionRatio
  • Barrier的使用
<android.support.constraint.Barrier
              android:id="@+id/barrier"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              app:barrierDirection="end"
              app:constraint_referenced_ids="button1,button2" />

第一个属性barrierDirection用来指定相对于constraint_referenced_ids所包含的控件的位置,第二个属性constraint_referenced_ids用来设置包含的控件的ID,效果看图


barrier-end.png
  • 使用Group来隐藏一组控件
<android.support.constraint.Group
              android:id="@+id/group"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:visibility="visible"
              app:constraint_referenced_ids="button4,button9" />

相关文章

网友评论

      本文标题:ConstraintLayout基本使用

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