Anko: custom views, view attrs, attrs

Created on 12 Apr 2015  ·  10Comments  ·  Source: Kotlin/anko

  1. It doesn't support custom view good enough - custom attrs.
  2. It also doesn't support all included attrs (like minHeight)
  3. How do I set background=?primaryColor?
fixed question

Most helpful comment

Here is what I ended up doing for this...

fun Context.attribute(value : Int) : TypedValue {
    var ret = TypedValue()
    getTheme().resolveAttribute(value, ret, true)
    return ret
}

fun Context.attrAsDimen(value : Int) : Int{
    return TypedValue.complexToDimensionPixelSize(attribute(value).data, getResources().getDisplayMetrics())
}

//inside the DSL of an activity onCreate
    val toolbar = toolbarSupport{
        setTitle("hello")
        setElevationCompat(dip(4))
        backgroundColor = attribute(R.attr.colorPrimary).data
    }.layoutParams(width = org.jetbrains.anko.matchParent, height = attrAsDimen(net.schwiz.koat.R.attr.actionBarSize))

All 10 comments

  1. Maybe there will be an intension in Anko plugin that will create new properties for Java get/set methods.
  2. There is a property View.minimumHeight : Int.
  3. backgroundColor = getResources().getColor(android.R.color.primary_text_dark).

can we have something like easy way to pass hardcoded attrs?

What hardcoded attrs do you mean?

custom attrs :)

Hi, how do I get android:layout_height="?attr/actionBarSize" functionality? or any other attr?

Here is what I ended up doing for this...

fun Context.attribute(value : Int) : TypedValue {
    var ret = TypedValue()
    getTheme().resolveAttribute(value, ret, true)
    return ret
}

fun Context.attrAsDimen(value : Int) : Int{
    return TypedValue.complexToDimensionPixelSize(attribute(value).data, getResources().getDisplayMetrics())
}

//inside the DSL of an activity onCreate
    val toolbar = toolbarSupport{
        setTitle("hello")
        setElevationCompat(dip(4))
        backgroundColor = attribute(R.attr.colorPrimary).data
    }.layoutParams(width = org.jetbrains.anko.matchParent, height = attrAsDimen(net.schwiz.koat.R.attr.actionBarSize))

custom views :setBackgroundResource、setBackgroundColor() both don't work even though i override the onDraw method.

this is how i use attr.
val a = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleView)

        //获取属性
        if (a.hasValue(R.styleable.CustomTitleView_center_title)) {
            centerTitle = a.getString(R.styleable.CustomTitleView_center_title)
        }
        if (a.hasValue(R.styleable.CustomTitleView_right_title)){
            rightTitle = a.getString(R.styleable.CustomTitleView_right_title)
        }
        if(a.hasValue(R.styleable.CustomTitleView_bgcolor)){
            bgcolor = a.getResourceId(R.styleable.CustomTitleView_bgcolor, R.color.colorAccent)
        }
        if(a.hasValue(R.styleable.CustomTitleView_leftsrc)){
            leftsrc = a.getResourceId(R.styleable.CustomTitleView_leftsrc, R.drawable.abc_btn_radio_material)
        }

or use like this.
//获取属性
for (i: Int in 0..a.indexCount) {
var attr : Int = a.getIndex(i)
when(attr){
R.styleable.CustomTitleView_leftsrc ->
leftsrc = a.getResourceId(attr, R.drawable.abc_btn_radio_material)
R.styleable.CustomTitleView_right_title -> rightTitle = a.getString(attr)
R.styleable.CustomTitleView_center_title -> centerTitle = a.getString(attr)
}
}

There's now a method for this dimenAttr(R.attr.actionBarSize)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maomaobug picture maomaobug  ·  4Comments

vlonjatg picture vlonjatg  ·  3Comments

SuleymanovTat picture SuleymanovTat  ·  4Comments

jonathanlip1100 picture jonathanlip1100  ·  4Comments

bapspatil picture bapspatil  ·  4Comments