草庐IT

ConstraintLayout约束布局使用全解

许进进呀 2023-03-28 原文

文章目录

  • ​​目的​​
  • ​​居中​​
  • ​​居中于⽗容器​​
  • ​​居中于控件中⼼​​
  • ​​⽔平⽅向居中​​
  • ​​垂直⽅向居中​​
  • ​​居中于控件的边​​
  • ​​填充​​
  • ​​权重​​
  • ​​⽂字基准线对⻬​​
  • ​​圆形定位​​
  • ​​特殊属性​​
  • ​​约束限制​​
  • ​​偏向​​
  • ​​约束链​​
  • ​​宽⾼⽐​​
  • ​​百分⽐布局​​
  • ​​辅助控件**GuideLine**​​
  • ​​Group​​
  • ​​Layer​​
  • ​​Barrier​​
  • ​​ConstraintHelper​​
  • ​​Placeholder​​
  • ​​**Flow**​​
  • ​​自定义一个简单linearLayout​​
  • ​​ConstraintSet​​
  • ​​布局扁平化更加容易做过渡动画​​

目的

为何:布局扁平化,减少布局层级,即提高性能;

约束布局 默认需要一个垂直方向和水平方向的约束;

约束布局中的宽高的0dp含义:充满约束

宽高:match_constraint:充满父容器约束

居中

居中于⽗容器

app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"

居中于控件中⼼

⽔平⽅向居中

app:layout_constraintStart_toStartOf="@id/view"
app:layout_constraintEnd_toEndOf="@id/view"

垂直⽅向居中

app:layout_constraintTop_toTopOf="@id/view"
app:layout_constraintBottom_toBottomOf="@id/view"

居中于控件的边

控件垂直居中于 view 的「下边」

app:layout_constraintTop_toBottomOf="@id/view"
app:layout_constraintBottom_toBottomOf="@id/view"

填充

⽔平⽅向填充⽗容器(通过 ​​match_constraint​​ )

app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_width="0dp"
备注:在早期版本中 match_parent 没有效果。

权重

为⽔平⽅向的控件设置权重,⼤⼩为 2:1:1 。

<!-- (view-1) -->
android:layout_width="0dp"
app:layout_constraintHorizontal_weight="2"

<!-- (view-2) -->
android:layout_width="0dp"
app:layout_constraintHorizontal_weight="1"

<!-- (view-3) -->
android:layout_width="0dp"
app:layout_constraintHorizontal_weight="1"

⽂字基准线对⻬

app:layout_constraintBaseline_toBaselineOf

圆形定位

通过「圆⼼」「⻆度」「半径」设置圆形定位

app:layout_constraintCircle="@id/view"
app:layout_constraintCircleAngle="90"
app:layout_constraintCircleRadius="180dp"

特殊属性

约束限制

限制控件⼤⼩不会超过约束范围。

app:layout_constrainedWidth="true"
app:layout_constrainedHeight="true

偏向

控制控件在垂直⽅向的 30%的位置

app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.3"
除了配合百分⽐定位,还有⽤于配合有时在「约束限制」的条件下不需要居中效果

的情况

垂直⽅向居顶部

app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constrainedHeight="true"
app:layout_constraintVertical_bias="0.0"

约束链

在约束链上的第⼀个控件上加上 ​​chainStyle​​ ,⽤来改变⼀组控件的布局⽅式

  • packed(打包)
  • spread (扩散)
  • spread_inside(内部扩散)
垂直⽅向 packed

app:layout_constraintVertical_chainStyle="packed"

宽⾼⽐

  • ⾄少需要⼀个⽅向的值为 ​​match_constraint​
  • 默认的都是「宽⾼⽐」,然后根据另外⼀条边和⽐例算出match_constraint 的值
宽高比,宽高都是0dp,即充满父约束指定高是计算出来的:

android:layout_height = "0dp"
android:layout_width = "0dp"
app:layout_constraintDimensionRatio = "H,2:1"

百分⽐布局

-需要对应⽅向上的值为 match_constraint

-百分⽐是 parent 的百分⽐,⽽不是约束区域的百分⽐

宽度是⽗容器的 30%

android:layout_width="0dp"
app:layout_constraintWidth_percent="0.3"

辅助控件GuideLine

  • 设置辅助线的⽅向 android:orientation=“vertical”
  • 设置辅助线的位置,根据⽅向不同:
距离左侧或上侧的距离 layout_constraintGuide_begin

距离右侧或下侧的距离 layout_constraintGuide_end

百分⽐ layout_constraintGuide_percent

Group

通过 constraint_referenced_ids 使⽤引⽤的⽅式来避免布局嵌套。

可以为⼀组控件统⼀设置 setVisibility

只有设置可⻅度的功能,不要指望这个来通知设置点击事件…

Layer

和 Group 类似,同样通过引⽤的⽅式来避免布局嵌套,可以为⼀组控件统⼀设置旋

转/缩放/ 位移。

Barrier

通过设置⼀组控件的某个⽅向的屏障,来 避免布局嵌套 。

<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier"
android: layout_width="wrap_content"
android: layout_height="wrap_content"
app: barrierDirection="end"
app: constraint_referenced_ids="viewl, view2"/>

ConstraintHelper

example:用声明写动画

import android.view.ViewAnimationUtils
import androidx.constraintlayout.widget.ConstraintHelper
import androidx.constraintlayout.widget.ConstraintLayout
import kotlin.math.hypot

class CircularRevealHelper(context: Context, attrs: AttributeSet) : ConstraintHelper(context, attrs) {
override fun updatePostLayout (container: ConstraintLayout) {
super.updatePostLayout (container)

referencedIds.forEach {
val view = container.getViewById(it)
val radius = hypot (view.width.toDouble(), view.height.toDouble()).toInt ()
ViewAnimationUtils.createCircularReveal(view, centerX: 0, centerY: 0, startRadius: Of, radius.toFloat())
.setDuration(2000L)
.start()
}
}
}
<androidx.constraintlayout.widget.ConstraintLayout
...
>
...

<com.hencoder.CircularRevealHelper
android: layout _width="wrap_content"
android: layout_height="wrap_content"
app:constraint_referenced_ ids="image1, image2, image3, image4"
/>

</androidx.constraintlayout.widget.ConstraintLayout>

Placeholder

通过 setContentId 来将指定控件放到占位符的位置。

findViewById<Pfaceholder>(R.id.placeholder).setContentId(view.id)

Flow

通过引⽤的⽅式来避免布局嵌套。

wrapMode

  • chain
  • aligned
  • none(默认)
注意这个控件是可以被测量的,所以对应⽅向上的值可能需要被确定(即不能只约束同⼀⽅ 向的单个约束)

<androidx. constraintlayout.helper.widget.Flow
android: id="@+id/flow"
android: layout width="Odp"
android: layout_height="wrap_content"
android: layout _marginStart="16dp"
android: layout_marginTop="16dp"I
android: background="@color/colorAccent"
android:orientation="horizontal"
app: flow wrapMode="chain"
app: flow verticalGap="16dp" //竖直间距
app: flow_horizontalGap="16dp"//水平间距
app: constraint_referenced_ids="viewl, view2, view3, view4"
app: layout_constraintStart_toStartOf="parent"
app: layout_constraintTop_toTopOf="parent"/>

自定义一个简单linearLayout

class Linear (context: Context, attrs: AttributeSet) : VirtualLayout (context, attrs) {

private val constraintSet: ConstraintSet by lazy {
ConstraintSet().apply { this: ConstraintSet
isForceld = false
}

override fun updatePreLayout (container: ConstraintLayout) {
super.updatePreLayout (container)
constraintSet.clone(container)

val viewIds : IntArray! = referencedIds;
for (i: Int in 1 until mCount) {

val current: Int = viewIds[i]
val before : Int = viewIds[i - 1]
constraintSet. connect (current, ConstraintSet.START, before, ConstraintSet.START)
constraintSet.connect (current, ConstraintSet.TOP, before, ConstraintSet.BOTTOM)
constraintSet.applyTo(container)
}
}
}

ConstraintSet

使⽤ ConstraintSet 对象来动态修改布局。通过 ConstraintSet#clone 来从 xml 布局中获取约束集。

val constraintLayout = view as ConstraintLayout
val constraintSet = ConstraintSet ().apply { this: ConstraintSet
clone (constraintLayout)
connect (
R.id.twitter,
ConstraintSet.BOTTOM,
ConstraintSet.PARENT_ID,
ConstraintSet.BOTTOM
)
}
constraintSet.applyTo(constraintLayout)
防⽌布局中有⽆ id 控件时报错,需要设置 isForceId = false

布局扁平化更加容易做过渡动画

在布局修改(constraintSet)之前加上 TransitionManager 来⾃动完成过渡动画。

constraintSet如果采用代码的方式编写,较为复杂,可以写两个xml布局,一个动画之前的,一个动画之后的,再用clone,将constraintSet吸出来,加以动画即可:

TransitionManager.beginDelayedTransition(constraintLayout)

有关ConstraintLayout约束布局使用全解的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  3. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  4. ruby-on-rails - 使用 Ruby on Rails 进行自动化测试 - 最佳实践 - 2

    很好奇,就使用ruby​​onrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提

  5. ruby - 在 Ruby 中使用匿名模块 - 2

    假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于

  6. ruby - 使用 ruby​​ 和 savon 的 SOAP 服务 - 2

    我正在尝试使用ruby​​和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我

  7. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  8. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  9. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  10. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

随机推荐