|
|
@ -6,7 +6,7 @@ import androidx.constraintlayout.widget.ConstraintSet |
|
|
|
import androidx.transition.TransitionManager |
|
|
|
import androidx.transition.TransitionManager |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ConstraintUtil(val constraintLayout: ConstraintLayout) { |
|
|
|
class ConstraintUtil(private val constraintLayout: ConstraintLayout) { |
|
|
|
|
|
|
|
|
|
|
|
private var begin: ConstraintBegin? = null |
|
|
|
private var begin: ConstraintBegin? = null |
|
|
|
private val applyConstraintSet = ConstraintSet() |
|
|
|
private val applyConstraintSet = ConstraintSet() |
|
|
@ -19,14 +19,14 @@ class ConstraintUtil(val constraintLayout: ConstraintLayout) { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 开始修改 |
|
|
|
* 开始修改 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun begin(): ConstraintBegin? { |
|
|
|
fun begin(): ConstraintBegin { |
|
|
|
synchronized(ConstraintBegin::class.java) { |
|
|
|
synchronized(ConstraintBegin::class.java) { |
|
|
|
if (begin == null) { |
|
|
|
if (begin == null) { |
|
|
|
begin = ConstraintBegin() |
|
|
|
begin = ConstraintBegin(constraintLayout, applyConstraintSet) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
applyConstraintSet.clone(constraintLayout) |
|
|
|
applyConstraintSet.clone(constraintLayout) |
|
|
|
return begin |
|
|
|
return begin!! |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -53,214 +53,228 @@ class ConstraintUtil(val constraintLayout: ConstraintLayout) { |
|
|
|
resetConstraintSet.applyTo(constraintLayout) |
|
|
|
resetConstraintSet.applyTo(constraintLayout) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
inner class ConstraintBegin { |
|
|
|
} |
|
|
|
/** |
|
|
|
|
|
|
|
* 清除关系<br></br> |
|
|
|
|
|
|
|
* 注意:这里不仅仅会清除关系,还会清除对应控件的宽高为 w:0,h:0 |
|
|
|
|
|
|
|
* @param viewIds |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun clear(@IdRes vararg viewIds: Int): ConstraintBegin { |
|
|
|
|
|
|
|
for (viewId in viewIds) { |
|
|
|
|
|
|
|
applyConstraintSet.clear(viewId) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return this |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
class ConstraintBegin( |
|
|
|
* 清除某个控件的,某个关系 |
|
|
|
private val constraintLayout: ConstraintLayout, |
|
|
|
* @param viewId |
|
|
|
private val applyConstraintSet: ConstraintSet |
|
|
|
* @param anchor |
|
|
|
) { |
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun clear(viewId: Int, anchor: Int): ConstraintBegin { |
|
|
|
|
|
|
|
applyConstraintSet.clear(viewId, anchor) |
|
|
|
|
|
|
|
return this |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 为某个控件设置 margin |
|
|
|
* 清除关系<br></br> |
|
|
|
* @param viewId 某个控件ID |
|
|
|
* 注意:这里不仅仅会清除关系,还会清除对应控件的宽高为 w:0,h:0 |
|
|
|
* @param left marginLeft |
|
|
|
* @param viewIds |
|
|
|
* @param top marginTop |
|
|
|
* @return |
|
|
|
* @param right marginRight |
|
|
|
*/ |
|
|
|
* @param bottom marginBottom |
|
|
|
fun clear(@IdRes vararg viewIds: Int): ConstraintBegin { |
|
|
|
* @return |
|
|
|
for (viewId in viewIds) { |
|
|
|
*/ |
|
|
|
applyConstraintSet.clear(viewId) |
|
|
|
fun setMargin( |
|
|
|
|
|
|
|
@IdRes viewId: Int, |
|
|
|
|
|
|
|
left: Int, |
|
|
|
|
|
|
|
top: Int, |
|
|
|
|
|
|
|
right: Int, |
|
|
|
|
|
|
|
bottom: Int |
|
|
|
|
|
|
|
): ConstraintBegin { |
|
|
|
|
|
|
|
setMarginLeft(viewId, left) |
|
|
|
|
|
|
|
setMarginTop(viewId, top) |
|
|
|
|
|
|
|
setMarginRight(viewId, right) |
|
|
|
|
|
|
|
setMarginBottom(viewId, bottom) |
|
|
|
|
|
|
|
return this |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return this |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 为某个控件设置 marginLeft |
|
|
|
* 清除某个控件的,某个关系 |
|
|
|
* @param viewId 某个控件ID |
|
|
|
* @param viewId |
|
|
|
* @param left marginLeft |
|
|
|
* @param anchor |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun setMarginLeft(@IdRes viewId: Int, left: Int): ConstraintBegin { |
|
|
|
fun clear(viewId: Int, anchor: Int): ConstraintBegin { |
|
|
|
applyConstraintSet.setMargin(viewId, ConstraintSet.LEFT, left) |
|
|
|
applyConstraintSet.clear(viewId, anchor) |
|
|
|
return this |
|
|
|
return this |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
fun setHorizontalWeight(viewId: Int, weight: Float): ConstraintBegin { |
|
|
|
* 为某个控件设置 marginRight |
|
|
|
applyConstraintSet.setHorizontalWeight(viewId, weight) |
|
|
|
* @param viewId 某个控件ID |
|
|
|
return this |
|
|
|
* @param right marginRight |
|
|
|
} |
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun setMarginRight(@IdRes viewId: Int, right: Int): ConstraintBegin { |
|
|
|
|
|
|
|
applyConstraintSet.setMargin(viewId, ConstraintSet.RIGHT, right) |
|
|
|
|
|
|
|
return this |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
fun setVerticalWeight(viewId: Int, weight: Float): ConstraintBegin { |
|
|
|
* 为某个控件设置 marginTop |
|
|
|
applyConstraintSet.setVerticalWeight(viewId, weight) |
|
|
|
* @param viewId 某个控件ID |
|
|
|
return this |
|
|
|
* @param top marginTop |
|
|
|
} |
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun setMarginTop(@IdRes viewId: Int, top: Int): ConstraintBegin { |
|
|
|
|
|
|
|
applyConstraintSet.setMargin(viewId, ConstraintSet.TOP, top) |
|
|
|
|
|
|
|
return this |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 为某个控件设置marginBottom |
|
|
|
* 为某个控件设置 margin |
|
|
|
* @param viewId 某个控件ID |
|
|
|
* @param viewId 某个控件ID |
|
|
|
* @param bottom marginBottom |
|
|
|
* @param left marginLeft |
|
|
|
* @return |
|
|
|
* @param top marginTop |
|
|
|
*/ |
|
|
|
* @param right marginRight |
|
|
|
fun setMarginBottom(@IdRes viewId: Int, bottom: Int): ConstraintBegin { |
|
|
|
* @param bottom marginBottom |
|
|
|
applyConstraintSet.setMargin(viewId, ConstraintSet.BOTTOM, bottom) |
|
|
|
* @return |
|
|
|
return this |
|
|
|
*/ |
|
|
|
} |
|
|
|
fun setMargin( |
|
|
|
|
|
|
|
@IdRes viewId: Int, |
|
|
|
|
|
|
|
left: Int, |
|
|
|
|
|
|
|
top: Int, |
|
|
|
|
|
|
|
right: Int, |
|
|
|
|
|
|
|
bottom: Int |
|
|
|
|
|
|
|
): ConstraintBegin { |
|
|
|
|
|
|
|
setMarginLeft(viewId, left) |
|
|
|
|
|
|
|
setMarginTop(viewId, top) |
|
|
|
|
|
|
|
setMarginRight(viewId, right) |
|
|
|
|
|
|
|
setMarginBottom(viewId, bottom) |
|
|
|
|
|
|
|
return this |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 为某个控件设置关联关系 left_to_left_of |
|
|
|
* 为某个控件设置 marginLeft |
|
|
|
* @param startId |
|
|
|
* @param viewId 某个控件ID |
|
|
|
* @param endId |
|
|
|
* @param left marginLeft |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun Left_toLeftOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
fun setMarginLeft(@IdRes viewId: Int, left: Int): ConstraintBegin { |
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.LEFT, endId, ConstraintSet.LEFT) |
|
|
|
applyConstraintSet.setMargin(viewId, ConstraintSet.LEFT, left) |
|
|
|
return this |
|
|
|
return this |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 为某个控件设置关联关系 left_to_right_of |
|
|
|
* 为某个控件设置 marginRight |
|
|
|
* @param startId |
|
|
|
* @param viewId 某个控件ID |
|
|
|
* @param endId |
|
|
|
* @param right marginRight |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun Left_toRightOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
fun setMarginRight(@IdRes viewId: Int, right: Int): ConstraintBegin { |
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.LEFT, endId, ConstraintSet.RIGHT) |
|
|
|
applyConstraintSet.setMargin(viewId, ConstraintSet.RIGHT, right) |
|
|
|
return this |
|
|
|
return this |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 为某个控件设置关联关系 top_to_top_of |
|
|
|
* 为某个控件设置 marginTop |
|
|
|
* @param startId |
|
|
|
* @param viewId 某个控件ID |
|
|
|
* @param endId |
|
|
|
* @param top marginTop |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun Top_toTopOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
fun setMarginTop(@IdRes viewId: Int, top: Int): ConstraintBegin { |
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.TOP, endId, ConstraintSet.TOP) |
|
|
|
applyConstraintSet.setMargin(viewId, ConstraintSet.TOP, top) |
|
|
|
return this |
|
|
|
return this |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 为某个控件设置关联关系 top_to_bottom_of |
|
|
|
* 为某个控件设置marginBottom |
|
|
|
* @param startId |
|
|
|
* @param viewId 某个控件ID |
|
|
|
* @param endId |
|
|
|
* @param bottom marginBottom |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun Top_toBottomOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
fun setMarginBottom(@IdRes viewId: Int, bottom: Int): ConstraintBegin { |
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.TOP, endId, ConstraintSet.BOTTOM) |
|
|
|
applyConstraintSet.setMargin(viewId, ConstraintSet.BOTTOM, bottom) |
|
|
|
return this |
|
|
|
return this |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 为某个控件设置关联关系 right_to_left_of |
|
|
|
* 为某个控件设置关联关系 left_to_left_of |
|
|
|
* @param startId |
|
|
|
* @param startId |
|
|
|
* @param endId |
|
|
|
* @param endId |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun Right_toLeftOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
fun leftToLeftOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.RIGHT, endId, ConstraintSet.LEFT) |
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.LEFT, endId, ConstraintSet.LEFT) |
|
|
|
return this |
|
|
|
return this |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 为某个控件设置关联关系 right_to_right_of |
|
|
|
* 为某个控件设置关联关系 left_to_right_of |
|
|
|
* @param startId |
|
|
|
* @param startId |
|
|
|
* @param endId |
|
|
|
* @param endId |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun Right_toRightOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
fun leftToRightOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.RIGHT, endId, ConstraintSet.RIGHT) |
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.LEFT, endId, ConstraintSet.RIGHT) |
|
|
|
return this |
|
|
|
return this |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 为某个控件设置关联关系 bottom_to_bottom_of |
|
|
|
* 为某个控件设置关联关系 top_to_top_of |
|
|
|
* @param startId |
|
|
|
* @param startId |
|
|
|
* @param endId |
|
|
|
* @param endId |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun Bottom_toBottomOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
fun topToTopOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.BOTTOM, endId, ConstraintSet.BOTTOM) |
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.TOP, endId, ConstraintSet.TOP) |
|
|
|
return this |
|
|
|
return this |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 为某个控件设置关联关系 bottom_to_top_of |
|
|
|
* 为某个控件设置关联关系 top_to_bottom_of |
|
|
|
* @param startId |
|
|
|
* @param startId |
|
|
|
* @param endId |
|
|
|
* @param endId |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun Bottom_toTopOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
fun topToBottomOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.BOTTOM, endId, ConstraintSet.TOP) |
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.TOP, endId, ConstraintSet.BOTTOM) |
|
|
|
return this |
|
|
|
return this |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 为某个控件设置宽度 |
|
|
|
* 为某个控件设置关联关系 right_to_left_of |
|
|
|
* @param viewId |
|
|
|
* @param startId |
|
|
|
* @param width |
|
|
|
* @param endId |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun setWidth(@IdRes viewId: Int, width: Int): ConstraintBegin { |
|
|
|
fun rightToLeftOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
applyConstraintSet.constrainWidth(viewId, width) |
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.RIGHT, endId, ConstraintSet.LEFT) |
|
|
|
return this |
|
|
|
return this |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 某个控件设置高度 |
|
|
|
* 为某个控件设置关联关系 right_to_right_of |
|
|
|
* @param viewId |
|
|
|
* @param startId |
|
|
|
* @param height |
|
|
|
* @param endId |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun setHeight(@IdRes viewId: Int, height: Int): ConstraintBegin { |
|
|
|
fun rightToRightOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
applyConstraintSet.constrainHeight(viewId, height) |
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.RIGHT, endId, ConstraintSet.RIGHT) |
|
|
|
return this |
|
|
|
return this |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 提交应用生效 |
|
|
|
* 为某个控件设置关联关系 bottom_to_bottom_of |
|
|
|
*/ |
|
|
|
* @param startId |
|
|
|
fun commit() { |
|
|
|
* @param endId |
|
|
|
applyConstraintSet.applyTo(constraintLayout) |
|
|
|
* @return |
|
|
|
} |
|
|
|
*/ |
|
|
|
|
|
|
|
fun bottomToBottomOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
|
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.BOTTOM, endId, ConstraintSet.BOTTOM) |
|
|
|
|
|
|
|
return this |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 为某个控件设置关联关系 bottom_to_top_of |
|
|
|
|
|
|
|
* @param startId |
|
|
|
|
|
|
|
* @param endId |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun bottomToTopOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
|
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.BOTTOM, endId, ConstraintSet.TOP) |
|
|
|
|
|
|
|
return this |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
/** |
|
|
|
|
|
|
|
* 为某个控件设置宽度 |
|
|
|
|
|
|
|
* @param viewId |
|
|
|
|
|
|
|
* @param width |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun setWidth(@IdRes viewId: Int, width: Int): ConstraintBegin { |
|
|
|
|
|
|
|
applyConstraintSet.constrainWidth(viewId, width) |
|
|
|
|
|
|
|
return this |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 某个控件设置高度 |
|
|
|
|
|
|
|
* @param viewId |
|
|
|
|
|
|
|
* @param height |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun setHeight(@IdRes viewId: Int, height: Int): ConstraintBegin { |
|
|
|
|
|
|
|
applyConstraintSet.constrainHeight(viewId, height) |
|
|
|
|
|
|
|
return this |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 提交应用生效 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun commit() { |
|
|
|
|
|
|
|
applyConstraintSet.applyTo(constraintLayout) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|