赞
踩
<View
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="16:9" <!-- 当layout_width和layout_height都为match_constraints时,16:9和h,16:9是一样的-->
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
boolean verticalDimensionKnown = verticalBehavior == DimensionBehaviour.MATCH_PARENT || verticalBehavior == DimensionBehaviour.FIXED;
boolean horizontalDimensionKnown = horizontalBehavior == DimensionBehaviour.MATCH_PARENT || horizontalBehavior == DimensionBehaviour.FIXED;
boolean horizontalUseRatio = horizontalMatchConstraints && widget.mDimensionRatio > 0.0F;
boolean verticalUseRatio = verticalMatchConstraints && widget.mDimensionRatio > 0.0F;
if (horizontalUseRatio && verticalDimensionKnown) {
ratio = widget.mDimensionRatio;
width = (int)(0.5F + (float)height * ratio);
} else if (verticalUseRatio && horizontalDimensionKnown) {
ratio = widget.mDimensionRatio;
height = (int)(0.5F + (float)width / ratio);
}
layout_constraintWidth_default=“wrap” is deprecated.
Use layout_width=“WRAP_CONTENT” and layout_constrainedWidth=“true” instead.
如果想实现如下效果,那么就要使用链
框选要组成链的控件后,右键选择chains即可增加水平链或垂直链
之后可以选择对齐方式
在这里可以选择不同的style
Spread:视图是均匀分布的(在考虑外边距之后)。这是默认值。
Spread inside:第一个和最后一个视图固定在链两端的约束边界上,其余视图均匀分布。
Weighted:当链设置为 spread 或 spread inside 时,您可以通过将一个或多个视图设置为“match constraints”(0dp) 来填充剩余空间。默认情况下,设置为“match constraints”的每个视图之间的空间均匀分布,但您可以使用 layout_constraintHorizontal_weight 和 layout_constraintVertical_weight 属性为每个视图分配重要性权重。如果您熟悉线性布局中的 layout_weight 的话,就会知道该样式与它的原理是相同的。因此,权重值最高的视图获得的空间最大;相同权重的视图获得同样大小的空间。
Packed:视图打包在一起(在考虑外边距之后)。 然后,您可以通过更改链的头视图偏差调整整条链的偏差(左/右或上/下)
ActivityConstraintBinding binding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = ActivityConstraintBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); } public void onClickAnim(View view) { if (view.isSelected()) { view.setSelected(false); anim(R.layout.activity_constraint); } else { view.setSelected(true); anim(R.layout.activity_constraint_keyframe_two); } } /** * 这种效果只针对位置和尺寸,不包含颜色等其他 * @param layoutId */ private void anim(@LayoutRes int layoutId) { ConstraintSet constraintSet = new ConstraintSet(); constraintSet.load(this, R.layout.activity_constraint_keyframe_two); TransitionManager.beginDelayedTransition(binding.getRoot()); constraintSet.applyTo(binding.getRoot()); }
<!-- 在anchor视图右上角--> <View ... app:layout_constraintBottom_toTopOf="@+id/anchor" app:layout_constraintEnd_toEndOf="@+id/anchor" app:layout_constraintStart_toEndOf="@+id/anchor" app:layout_constraintTop_toTopOf="@+id/anchor" /> <!-- 在anchor视图右下角--> <View ... app:layout_constraintStart_toEndOf="@id/anchor" app:layout_constraintEnd_toEndOf="@id/anchor" app:layout_constraintTop_toBottomOf="@id/anchor" app:layout_constraintBottom_toBottomOf="@id/anchor" /> <!-- 在anchor视图左上角--> <View ... app:layout_constraintStart_toStartOf="@id/anchor" app:layout_constraintEnd_toStartOf="@id/anchor" app:layout_constraintTop_toTopOf="@id/anchor" app:layout_constraintBottom_toTopOf="@id/anchor" /> <!-- 在anchor视图左下角--> <View ... app:layout_constraintStart_toStartOf="@id/anchor" app:layout_constraintEnd_toStartOf="@id/anchor" app:layout_constraintTop_toBottomOf="@id/anchor" app:layout_constraintBottom_toBottomOf="@id/anchor" />
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:singleLine="true" app:layout_constrainedWidth="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@id/right" app:layout_constraintHorizontal_bias="0" app:layout_constraintHorizontal_chainStyle="packed" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:text="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" /> <TextView android:id="@+id/right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:background="@drawable/bg_label" android:paddingLeft="6dp" android:paddingRight="6dp" android:text="群聊" app:layout_constraintBottom_toBottomOf="@id/left" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/left" app:layout_constraintTop_toTopOf="@id/left" /> </androidx.constraintlayout.widget.ConstraintLayout>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。