Skip to content

Instantly share code, notes, and snippets.

@wangyung
Forked from anonymous/_ConstraintLayout.kt
Created August 9, 2017 03:45
Show Gist options
  • Save wangyung/cd3d6a85bae2b3cfd0b153bbf1867ee5 to your computer and use it in GitHub Desktop.
Save wangyung/cd3d6a85bae2b3cfd0b153bbf1867ee5 to your computer and use it in GitHub Desktop.
Anko ConstraintLayout
import android.app.Activity
import android.content.Context
import android.support.constraint.ConstraintLayout
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.view.ViewManager
import org.jetbrains.anko.AnkoViewDslMarker
import org.jetbrains.anko.custom.ankoView
open class _ConstraintLayout(ctx: Context) : ConstraintLayout(ctx) {
inline fun <T : View> T.lparams(
c: Context?,
attrs: AttributeSet?,
init: ConstraintLayout.LayoutParams.() -> Unit
): T {
val layoutParams = ConstraintLayout.LayoutParams(c!!, attrs!!)
layoutParams.init()
layoutParams.validate()
this@lparams.layoutParams = layoutParams
return this
}
inline fun <T : View> T.lparams(
c: Context?,
attrs: AttributeSet?
): T {
val layoutParams = ConstraintLayout.LayoutParams(c!!, attrs!!)
layoutParams.validate()
this@lparams.layoutParams = layoutParams
return this
}
inline fun <T : View> T.lparams(
width: Int = android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
height: Int = android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
init: ConstraintLayout.LayoutParams.() -> Unit
): T {
val layoutParams = ConstraintLayout.LayoutParams(width, height)
layoutParams.init()
layoutParams.validate()
this@lparams.layoutParams = layoutParams
return this
}
inline fun <T : View> T.lparams(
width: Int = android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
height: Int = android.view.ViewGroup.LayoutParams.WRAP_CONTENT
): T {
val layoutParams = ConstraintLayout.LayoutParams(width, height)
layoutParams.validate()
this@lparams.layoutParams = layoutParams
return this
}
inline fun <T : View> T.lparams(
source: ViewGroup.LayoutParams?,
init: ConstraintLayout.LayoutParams.() -> Unit
): T {
val layoutParams = ConstraintLayout.LayoutParams(source!!)
layoutParams.init()
layoutParams.validate()
this@lparams.layoutParams = layoutParams
return this
}
inline fun <T : View> T.lparams(
source: ViewGroup.LayoutParams?
): T {
val layoutParams = ConstraintLayout.LayoutParams(source!!)
this@lparams.layoutParams = layoutParams
return this
}
inline fun <T : View> T.lparams(
source: ViewGroup.MarginLayoutParams?,
init: ConstraintLayout.LayoutParams.() -> Unit
): T {
val layoutParams = ConstraintLayout.LayoutParams(source!!)
layoutParams.init()
layoutParams.validate()
this@lparams.layoutParams = layoutParams
return this
}
inline fun <T : View> T.lparams(
source: ViewGroup.MarginLayoutParams?
): T {
val layoutParams = ConstraintLayout.LayoutParams(source!!)
layoutParams.validate()
this@lparams.layoutParams = layoutParams
return this
}
inline fun <T : View> T.lparams(
source: ConstraintLayout.LayoutParams?,
init: ConstraintLayout.LayoutParams.() -> Unit
): T {
val layoutParams = ConstraintLayout.LayoutParams(source!!)
layoutParams.init()
layoutParams.validate()
this@lparams.layoutParams = layoutParams
return this
}
inline fun <T : View> T.lparams(
source: ConstraintLayout.LayoutParams?
): T {
val layoutParams = ConstraintLayout.LayoutParams(source!!)
layoutParams.validate()
this@lparams.layoutParams = layoutParams
return this
}
}
inline fun ViewManager.constraintLayout(): _ConstraintLayout = constraintLayout {}
inline fun ViewManager.constraintLayout(init: (@AnkoViewDslMarker _ConstraintLayout).() -> Unit): _ConstraintLayout {
return ankoView(::_ConstraintLayout, theme = 0) { init() }
}
inline fun ViewManager.themedConstraintLayout(theme: Int = 0): _ConstraintLayout = themedConstraintLayout(theme) {}
inline fun ViewManager.themedConstraintLayout(theme: Int = 0, init: (@AnkoViewDslMarker _ConstraintLayout).() -> Unit): _ConstraintLayout {
return ankoView(::_ConstraintLayout, theme) { init() }
}
inline fun Context.constraintLayout(): _ConstraintLayout = constraintLayout {}
inline fun Context.constraintLayout(init: (@AnkoViewDslMarker _ConstraintLayout).() -> Unit): _ConstraintLayout {
return ankoView(::_ConstraintLayout, theme = 0) { init() }
}
inline fun Context.themedConstraintLayout(theme: Int = 0): _ConstraintLayout = themedConstraintLayout(theme) {}
inline fun Context.themedConstraintLayout(theme: Int = 0, init: (@AnkoViewDslMarker _ConstraintLayout).() -> Unit): _ConstraintLayout {
return ankoView(::_ConstraintLayout, theme) { init() }
}
inline fun Activity.constraintLayout(): _ConstraintLayout = constraintLayout {}
inline fun Activity.constraintLayout(init: (@AnkoViewDslMarker _ConstraintLayout).() -> Unit): _ConstraintLayout {
return ankoView(::_ConstraintLayout, theme = 0) { init() }
}
val parentId: Int = ConstraintLayout.LayoutParams.PARENT_ID
val matchConstraint: Int = ConstraintLayout.LayoutParams.MATCH_CONSTRAINT
val matchConstraintWrap: Int = ConstraintLayout.LayoutParams.MATCH_CONSTRAINT_WRAP
val matchConstraintSpread: Int = ConstraintLayout.LayoutParams.MATCH_CONSTRAINT_SPREAD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment