Skip to content

Instantly share code, notes, and snippets.

@vorobeij
Created April 17, 2018 11:36
Show Gist options
  • Select an option

  • Save vorobeij/918cbe8c09116f9a02d2be54c2f93bac to your computer and use it in GitHub Desktop.

Select an option

Save vorobeij/918cbe8c09116f9a02d2be54c2f93bac to your computer and use it in GitHub Desktop.
compound view parent
import android.annotation.TargetApi
import android.content.Context
import android.graphics.Outline
import android.os.Build
import android.support.constraint.ConstraintLayout
import android.util.AttributeSet
import android.view.View
import android.view.ViewOutlineProvider
/**
* class for compound views
*/
abstract class CompoundView : ConstraintLayout {
constructor(context: Context) : super(context) {
initLayout()
}
constructor(context: Context,
attrs: AttributeSet) : super(context, attrs) {
initLayout()
loadAttrs(attrs)
}
constructor(context: Context,
attrs: AttributeSet,
defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
initLayout()
loadAttrs(attrs)
}
/**
* // usual content:
* val a = context.obtainStyledAttributes(attrs, R.styleable.IconLabel, 0, 0)
val t = a.getString(R.styleable.IconLabel_a_label)
if (t != null) textView!!.text = t
val im = a.getDrawable(R.styleable.IconLabel_a_icon)
if (im != null) icon!!.setImageDrawable(im)
a.recycle()
*/
abstract fun loadAttrs(attrs: AttributeSet)
/**
* // usual content:
* val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
inflater.inflate(R.layout.w_action_item, this)
textView = this.v_label as TextView
icon = this.v_icon
*/
abstract fun initLayout()
/**
* elevation support for compound views
*/
override fun onSizeChanged(w: Int,
h: Int,
oldw: Int,
oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
outlineProvider = CustomOutline(w, h)
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private inner class CustomOutline internal constructor(internal var width: Int,
internal var height: Int) : ViewOutlineProvider() {
override fun getOutline(view: View,
outline: Outline) {
outline.setRect(0, 0, view.width, view.height)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment