background=?primaryColor?View.minimumHeight : Int.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)
Most helpful comment
Here is what I ended up doing for this...