|
|
|
@ -6,7 +6,7 @@ import androidx.constraintlayout.widget.ConstraintSet |
|
|
|
|
import androidx.transition.TransitionManager |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ConstraintUtil(val constraintLayout: ConstraintLayout) { |
|
|
|
|
class ConstraintUtil(private val constraintLayout: ConstraintLayout) { |
|
|
|
|
|
|
|
|
|
private var begin: ConstraintBegin? = null |
|
|
|
|
private val applyConstraintSet = ConstraintSet() |
|
|
|
@ -19,14 +19,14 @@ class ConstraintUtil(val constraintLayout: ConstraintLayout) { |
|
|
|
|
/** |
|
|
|
|
* 开始修改 |
|
|
|
|
*/ |
|
|
|
|
fun begin(): ConstraintBegin? { |
|
|
|
|
fun begin(): ConstraintBegin { |
|
|
|
|
synchronized(ConstraintBegin::class.java) { |
|
|
|
|
if (begin == null) { |
|
|
|
|
begin = ConstraintBegin() |
|
|
|
|
begin = ConstraintBegin(constraintLayout, applyConstraintSet) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
applyConstraintSet.clone(constraintLayout) |
|
|
|
|
return begin |
|
|
|
|
return begin!! |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -53,7 +53,13 @@ class ConstraintUtil(val constraintLayout: ConstraintLayout) { |
|
|
|
|
resetConstraintSet.applyTo(constraintLayout) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
inner class ConstraintBegin { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class ConstraintBegin( |
|
|
|
|
private val constraintLayout: ConstraintLayout, |
|
|
|
|
private val applyConstraintSet: ConstraintSet |
|
|
|
|
) { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 清除关系<br></br> |
|
|
|
|
* 注意:这里不仅仅会清除关系,还会清除对应控件的宽高为 w:0,h:0 |
|
|
|
@ -78,6 +84,16 @@ class ConstraintUtil(val constraintLayout: ConstraintLayout) { |
|
|
|
|
return this |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun setHorizontalWeight(viewId: Int, weight: Float): ConstraintBegin { |
|
|
|
|
applyConstraintSet.setHorizontalWeight(viewId, weight) |
|
|
|
|
return this |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun setVerticalWeight(viewId: Int, weight: Float): ConstraintBegin { |
|
|
|
|
applyConstraintSet.setVerticalWeight(viewId, weight) |
|
|
|
|
return this |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 为某个控件设置 margin |
|
|
|
|
* @param viewId 某个控件ID |
|
|
|
@ -151,7 +167,7 @@ class ConstraintUtil(val constraintLayout: ConstraintLayout) { |
|
|
|
|
* @param endId |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
fun Left_toLeftOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
|
fun leftToLeftOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.LEFT, endId, ConstraintSet.LEFT) |
|
|
|
|
return this |
|
|
|
|
} |
|
|
|
@ -162,7 +178,7 @@ class ConstraintUtil(val constraintLayout: ConstraintLayout) { |
|
|
|
|
* @param endId |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
fun Left_toRightOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
|
fun leftToRightOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.LEFT, endId, ConstraintSet.RIGHT) |
|
|
|
|
return this |
|
|
|
|
} |
|
|
|
@ -173,7 +189,7 @@ class ConstraintUtil(val constraintLayout: ConstraintLayout) { |
|
|
|
|
* @param endId |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
fun Top_toTopOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
|
fun topToTopOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.TOP, endId, ConstraintSet.TOP) |
|
|
|
|
return this |
|
|
|
|
} |
|
|
|
@ -184,7 +200,7 @@ class ConstraintUtil(val constraintLayout: ConstraintLayout) { |
|
|
|
|
* @param endId |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
fun Top_toBottomOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
|
fun topToBottomOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.TOP, endId, ConstraintSet.BOTTOM) |
|
|
|
|
return this |
|
|
|
|
} |
|
|
|
@ -195,7 +211,7 @@ class ConstraintUtil(val constraintLayout: ConstraintLayout) { |
|
|
|
|
* @param endId |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
fun Right_toLeftOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
|
fun rightToLeftOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.RIGHT, endId, ConstraintSet.LEFT) |
|
|
|
|
return this |
|
|
|
|
} |
|
|
|
@ -206,7 +222,7 @@ class ConstraintUtil(val constraintLayout: ConstraintLayout) { |
|
|
|
|
* @param endId |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
fun Right_toRightOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
|
fun rightToRightOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.RIGHT, endId, ConstraintSet.RIGHT) |
|
|
|
|
return this |
|
|
|
|
} |
|
|
|
@ -217,7 +233,7 @@ class ConstraintUtil(val constraintLayout: ConstraintLayout) { |
|
|
|
|
* @param endId |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
fun Bottom_toBottomOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
|
fun bottomToBottomOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.BOTTOM, endId, ConstraintSet.BOTTOM) |
|
|
|
|
return this |
|
|
|
|
} |
|
|
|
@ -228,7 +244,7 @@ class ConstraintUtil(val constraintLayout: ConstraintLayout) { |
|
|
|
|
* @param endId |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
fun Bottom_toTopOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
|
fun bottomToTopOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin { |
|
|
|
|
applyConstraintSet.connect(startId, ConstraintSet.BOTTOM, endId, ConstraintSet.TOP) |
|
|
|
|
return this |
|
|
|
|
} |
|
|
|
@ -261,6 +277,4 @@ class ConstraintUtil(val constraintLayout: ConstraintLayout) { |
|
|
|
|
fun commit() { |
|
|
|
|
applyConstraintSet.applyTo(constraintLayout) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |