ConstraintLayout 所有属性

本文详细介绍了Android ConstraintLayout的使用方法及高级特性,包括基础约束、Barrier组件应用、百分比布局等,并提供了具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 :同left_toRightOf
  • layout_constraintStart_toStartOf :同left_toLeftOf
  • layout_constraintEnd_toStartOf :同right_toLeftOf
  • layout_constraintEnd_toEndOf :同right_toRightOf

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"

H 表明约束高度,所以结果就是 Button 的宽度和父容器宽度一样,而高度值符合 16:9 的比率。


当一个控件设为wrap_content时,再添加约束尺寸是不 

起效果的。如需生效,需要设置如下属性为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_constraintGuide_begin=""
    app:layout_constraintGuide_end=""
    app:layout_constraintGuide_percent=""

表示此控件在布局中Y轴的绝对坐标点

app:layout_constraintHorizontal_chainStyle=""
app:layout_constraintHorizontal_weight=""
app:layout_constraintVertical_bias=""
app:layout_constraintVertical_weight=""
app:layout_constraintVertical_chainStyle=""
app:layout_constraintTop_creator=""
水平占比
三种格式 :三个平分在中间,三个平分在左边,三个都在中间

表示此控件在布局中的的垂直方向上的偏移百分百

下面几个属性是 UI 编辑器所使用的,用了辅助拖拽布局的,在实际使用过程中,可以不用关心这些属性。

  • 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"



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值