ConstraintLayout基本使用

2019-03-24  本文已影响0人  Bernardo_Silva
 <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
app:layout_constraintHorizontal_weight
layout_constraintHorizontal_chainStyle
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
<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
<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" />
上一篇 下一篇

猜你喜欢

热点阅读