ConstraintLayout 所有属性

匿名 (未验证) 提交于 2019-12-03 00:25:02

1、基础值(以下是经常用到的值,很有规律)
  • layout_constraintLeft_toLeftOf :当前View的右侧和另一个View的右侧位置对齐,与RelativeLayout的alignLeft属性相似
  • ***layout_constraintLeft_toRightOf ***:当前view的左侧会在另一个View的右侧位置 与RelativeLayout的toRightOf属性相似
  • layout_constraintRight_toLeftOf :当前view的右侧会在另一个View的左侧位置 与RelativeLayout的toLeftOf属性相似
  • layout_constraintRight_toRightOf :当前View的右侧和另一个View的右侧位置对其,与RelativeLayout的alignRight属性相似
  • layout_constraintTop_toTopOf :头部对齐,与alignTop相似
  • layout_constraintTop_toBottomOf :当前View在另一个View的下侧 与below相似
  • layout_constraintBottom_toTopOf :当前View在另一个View的上方 与above相似
  • layout_constraintBottom_toBottomOf :底部对齐,与alignBottom属性相似
  • layout_constraintBaseline_toBaselineOf :文字底部对齐,与alignBaseLine属性相似
  • layout_constraintStart_toEndOf
  • layout_constraintStart_toStartOf
  • layout_constraintEnd_toStartOf
  • layout_constraintEnd_toEndOf

Margins属性:同RelativeLayout属性

  • android:layout_marginStart
  • android:layout_marginEnd
  • android:layout_marginLeft
  • android:layout_marginTop
  • android:layout_marginRight
  • android:layout_marginBottom
那就正好说到下面了。

对 Barrier 可以使用的属性有:

  • barrierDirection:设置 Barrier 所创建的位置。可设置的有:bottom、end、left、right、start、top。
  • constraint_referenced_ids:设置 Barrier 引用的控件。可设置多个,设置的方式是:id, id。(无需加 @id/)
  • barrierAllowsGoneWidgets:默认为 true,即当 Barrier 引用的控件被 GONE 掉时,则 Barrier 默认的创建行为是在已 GONE 掉控件的已解析位置上进行创建。如果设置为 false,则不会将 GONE 掉的控件考虑在内。
<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"     android:padding="16dp"     tools:context="com.github.airsaid.constraintlayoutdemo.MainActivity">      <TextView         android:id="@+id/title"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Title"         android:textSize="16sp"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent" />      <TextView         android:id="@+id/desc"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginTop="10dp"         android:text="This is a descriptive text."         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toBottomOf="@id/title" />      <android.support.constraint.Barrier         android:id="@+id/barrier"         android:layout_width="match_parent"         android:layout_height="match_parent"         app:barrierDirection="end"         app:constraint_referenced_ids="title, desc" />      <TextView         android:id="@+id/content"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_marginStart="10dp"         android:text="This is a piece of content that is very long and long very long and long ..."         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toEndOf="@id/barrier"         app:layout_constraintTop_toTopOf="parent" />   </android.support.constraint.ConstraintLayout>
效果一:

效果二:

二:重要的属性

app:layout_constraintHorizontal_bias="0.5"

横向占比


app:layout_constraintDimensionRatio="1:1" 

app:layout_constraintDimensionRatio="H,16:9"



起效果的。如需生效,需要设置如下属性为true:(1.1新增))

app:layout_constrainedWidth=”true|false”

app:layout_constrainedHeight=”true|false”

app:layout_constraintHeight_default="" app:layout_constraintHeight_max="" app:layout_constraintHeight_min="" app:layout_constraintHeight_percent=""
app:layout_constraintWidth_min="" app:layout_constraintWidth_max="" app:layout_constraintWidth_default=""

可以设置控件相对于父布局尺寸的百分比,0-1之间。需要注意:

  • 控件的width和height要被设置为0dp
  • 默认宽高属性要被设置为percent(1.1版本之后将无需设置,自动识别)

    app:layout_constraintWidth_default="percent" app:layout_constraintHeight_default="percent" 
 <Button         android:layout_width="0dp"         android:layout_height="0dp"         app:layout_constraintWidth_percent="0.3"         app:layout_constraintHeight_percent="0.2"         />

设置按钮的宽度为父布局宽度的30%,高度为父布局高度的20%。

app:layout_constraintHorizontal_chainStyle="" app:layout_constraintHorizontal_weight="" app:layout_constraintVertical_bias=""
app:layout_constraintVertical_weight="" app:layout_constraintVertical_chainStyle="" app:layout_constraintTop_creator=""

  • layout_optimizationLevel
  • layout_editor_absoluteX
  • layout_editor_absoluteY
  • layout_constraintBaseline_creator
  • layout_constraintTop_creator
  • layout_constraintRight_creator
  • layout_constraintLeft_creator
  • layout_constraintBottom_creator

app:constraintSet="@string/app_name"



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!