366,关于ConstraintLayout的部分属性总结(设置

2020-12-05  本文已影响0人  枫叶1234

为什么会出现ConstraintLayout呢?

截屏2020-12-03 下午8.37.25.png

layout_constraintBaseline_toBaselineOf 指的是文本基线

截屏2020-12-05 上午8.07.19.png

三、Margins属性:同RelativeLayout属性

四、Margins when connected to a Gone widget
当前View与另一个View绑定后,另一个View的属性设置为了Gone,则以下属性会生效

截屏2020-12-03 上午9.38.37.png

五、center position and bias
居中并设置权重,使view居中并且设置权重,同RelativeLayout的center_horizontal/vertical=“true”
设置方法:以横向居中为例: 将ConstraintLayout的子View的属性如下进行设置

<TextView android:layout_width="wrap_content" 
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。即正中央显示,效果如图

image.png

app:layout_constraintHorizontal_bias="0.3"
可以通过设置app:layout_constraintHorizontal_bias属性进行位置的调整,表示距离左侧(因为是横向,纵向则是距离顶部)30%的的距离。

如图:


截屏2020-12-05 上午7.53.39.png
六、Dimension constraints

android:minWidth set the minimum width for the layout
android:minHeight set the minimum height for the layout

当子View的宽/高设置为wrap_content时,会用到minWidth和minHeight这两个属性

七、Widget dimension constraints

注意:
ConstraintLayout 不支持match_parent属性,但支持wrap_content属性。如果你需要用match_parent,将宽度/高度指定为0dp,然后设置left_toleft,right_toRight为parent即可实现横向充满,同理设置竖向的

八、Ratio比例大小属性

当你的父控件为ConstraintLayout,可以利用这个属性来控制当前View的宽高比。在利用这个属性时,你必须指明一个方向上的大小为0dp,另一个方向可指定为明确的dp值也可以使用wrap_content这样才能够按照比例来为你摆放

<ImageView android:layout_width="100dp" 
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" 属性来指定控件宽高的比,默认为宽:高

你也可以通过下面的方法进行设置:

<ImageView android:layout_width="0dp" 
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已经指定了高度上的大小。而并非是指定比例为高:宽

你也可以通过下面的方法进行设置:

<ImageView android:layout_width="0dp" 
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已经指定了高度上的大小。而并非是指定比例为高:宽

截屏2020-12-03 上午9.38.37.png

权重链

截屏2020-12-05 上午7.59.08.png

链条样式

截屏2020-12-05 上午8.03.33.png
上一篇 下一篇

猜你喜欢

热点阅读