Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.28 KB | None | 0 0
  1. class CustomView: LinearLayout {
  2.     private var drawable: Drawable? = null
  3.     private var text: String? = null
  4.  
  5.  
  6.     constructor(context: Context) : super(context) {
  7.         init(context, null)
  8.     }
  9.  
  10.     constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
  11.         init(context, attrs)
  12.     }
  13.  
  14.     constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
  15.         init(context, attrs)
  16.     }
  17.  
  18.     constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
  19.         init(context, attrs)
  20.     }
  21.  
  22.     private fun init(context: Context, attrs: AttributeSet?) {
  23.         val typedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.CustomView, 0, 0)
  24.         try {
  25.             drawable= typedArray.getDrawable(R.styleable.CustomView_android_src)
  26.             text= typedArray.getString(R.styleable.CustomView_android_text)
  27.         } finally {
  28.             typedArray.recycle()
  29.         }
  30.         View.inflate(context, R.layout.view_list_btn, this)
  31.         (findViewById(R.id.imgIcon) as ImageView).apply { setImageDrawable(drawable) }
  32.         (findViewById(R.id.tvTitle) as TextView).apply { setText(text) }
  33.  
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement