Created
April 11, 2019 09:17
-
-
Save underwindfall/b734072c9c921d840f6bc57360bdc9d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Utility functions for Android View | |
*/ | |
fun View.setZTranslation(z: Float) { | |
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { | |
this.translationZ = z | |
} | |
} | |
/** | |
* Get a typedArray to obtain styledAttributes and recycle it | |
*/ | |
inline fun View.withStyleableRes(set: AttributeSet, @StyleableRes attrs: IntArray, init: TypedArray.() -> Unit) { | |
val typedArray = context.theme.obtainStyledAttributes(set, attrs, 0, 0) | |
if ( typedArray != null ) { | |
try { | |
typedArray.init() | |
} finally { | |
typedArray.recycle() | |
} | |
} | |
} | |
/** | |
* Layout inflation shorter form | |
*/ | |
fun ViewGroup.inflateLayout(layoutId: Int, attachToRoot: Boolean = true) { | |
android.view.LayoutInflater.from(context).inflate(layoutId, this, attachToRoot) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment