2023年7月13日发(作者:)
androidConstraintLayout的使⽤(约束布局)⼀、使⽤⽅式:Android studio 2.2以上 并添加依赖compile 'aint:constraint-layout:1.0.2'Android studio 3.0 以上,不需要添加依赖库,Kotlin 默认就是ConstraintLayout布局.⼆、相对位置属性如下: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_toRightOflayout_constraintStart_toStartOf :同left_toLeftOflayout_constraintEnd_toStartOf :同right_toLeftOflayout_constraintEnd_toEndOf :同right_toRightOf三、Margins属性:同RelativeLayout属性android:layout_marginStartandroid:layout_marginEndandroid:layout_marginLeftandroid:layout_marginTopandroid:layout_marginRightandroid:layout_marginBottom四、Margins when connected to a Gone widget当前View与另⼀个View绑定后,另⼀个View的属性设置为了Gone,则以下属性会⽣效layout_goneMarginStartlayout_goneMarginEndlayout_goneMarginLeftlayout_goneMarginToplayout_goneMarginRightlayout_goneMarginBottom五、center position and bias
居中并设置权重,使view居中并且设置权重,同RelativeLayout的center_horizontal/vertical=“true” **设置⽅法:以横向居中为例: 将ConstraintLayout的⼦View的属性如下进⾏设置 android:layout_height="wrap_content" android:text="Hello,World" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />将宽度设置为wrap_content并设置两个属性:app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"即可将该View横向居中显⽰。(竖向同理)。 利⽤bias设置权重: 设置居中之后默认的权重的为0.5。即正中央显⽰,效果如图默认的居中显⽰效果app:layout_constraintHorizontal_bias="0.3"可以通过设置app:layout_constraintHorizontal_bias属性进⾏位置的调整,表⽰距离左侧(因为是横向,纵向则是距离顶部)30%的的距离。六、Dimension constraintsandroid:minWidth set the minimum width for the layoutandroid:minHeight set the minimum height for the layout当⼦View的宽/⾼设置为wrap_content时,会⽤到minWidth和minHeight这两个属性七、Widget dimension constraints使⽤⼀个具体的值 Using a specific dimension (either a literal value such as 123dp or a Dimension reference)使⽤wrap_content让控件⾃⼰来计算⼤⼩ Using WRAP_CONTENT, which will ask the widget to compute its own size⽤0dp来指定,意思就是Match_Constraint Using 0dp, which is the equivalent of “MATCH_CONSTRAINT”注意:ConstraintLayout 不⽀持match_parent属性,但⽀持wrap_content属性。如果你需要⽤match_parent,将宽度/⾼度指定为0dp,然后设置left_toleft,right_toRight为parent即可实现横向充满,同理设置竖向的⼋、Ratio⽐例⼤⼩属性当你的⽗控件为ConstraintLayout,可以利⽤这个属性来控制当前View的宽⾼⽐。在利⽤这个属性时,你必须指明⼀个⽅向上的⼤⼩为0dp,另⼀个⽅向可指定为明确的dp值也可以使⽤wrap_content这样才能够按照⽐例来为你摆放 android:layout_height="0dp" android:src="@mipmap/ic_launcher" app:layout_constraintDimensionRatio="1:4" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" />通过指定 app:layout_constraintDimensionRatio="1:1" 属性来指定控件宽⾼的⽐,默认为宽:⾼你也可以通过下⾯的⽅法进⾏设置: android:layout_height="0dp" android:src="@mipmap/ic_launcher" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintDimensionRatio="H,3:1" app:layout_constraintTop_toTopOf="parent" />上⾯的这种情况将宽和⾼均指定为了0dp,但是通过top_toTopOf bottom_toBottomOf 两个属性指定了在⾼度上的⼤⼩。另外通过在ratio属性中指明的H,是为了告诉ConstraintLayout已经指定了⾼度上的⼤⼩。⽽并⾮是指定⽐例为⾼:宽九、Chainschains:锁链,通过设置属性可以将⼀个⽅向上的控件形成锁链(相互依赖),并且能够实现⽐例分布(类似于LinearLayout的weight分布)形成chains的条件:head(即chains的第⼀个view)必须包含top_toTopOf或者left_toLeftOfchain的最后⼀个View必须指定bottom_toBottomOf或者right_toRightOfchain中的View必须互相依赖如:view a 和view b在竖直⽅向上形成锁链:a的属性设置为 top_toTopOf = “parent” bottom_toTopOf = “b” b的属性设置为top_toBottomOf=“a” bottom_toBottom = “parent"形成依赖的好处:可以使⽤⽐例进⾏设置(使⽤⽅式同LinearLayout的weight属性,效果也相同)chains Style(chains 样式)提供了三种默认的样式:spread、packed、spread_inside spread样式:这⾥写图⽚描述spread_inside样式:这⾥写图⽚描述包含weight属性的chain这⾥写图⽚描述packed样式这⾥写图⽚描述有bias的packed样式这⾥写图⽚描述⼗、ConstraintSet类:这个类是辅助进⾏代码中动态设置ConstraintSet属性的。 GuideLine⼀共有3种指定的⽅式: specifying a fixed distance from the left or the top of a layout(layout_constraintGuide_begin) //在距离顶部或者左侧⼀定距离的地⽅放置⼀条导航线 specifying a fixed distance from the rightor the bottom of a layout (layout_constraintGuide_end) //在距离右侧或者底部⼀定距离的地⽅放置⼀条导航线 specifying apercentage of the width or the height of a layout (layout_constraintGuide_percent)//按照⽗视图的百分⽐放置⼀条导航线
发布者:admin,转转请注明出处:http://www.yc00.com/news/1689218130a222543.html
评论列表(0条)