anko的使用

https://github.com/Kotlin/anko

一、基本导入

ext.anko_version='0.10.8'
implementation "org.jetbrains.anko:anko:$anko_version"

二、dp、sp、px的相互转换

Context方法,直接调用

dip    将dip单位的数值转换为以px为单位的数值
sp    将sp单位的数值转换为以px为单位的数值
px2dip    将px单位的数值转换为以dip为单位的数值
px2sp    将px单位的数值转换为以sp为单位的数值
dimen    将dip单位的数值转换为以sp为单位的数值

三、Anko中RelativeLayout类的相对位置

Anko库的相对位置 RelativeLayout类的相对位置
leftOf LEFT_OF
sameTop ALIGN_TOP
above ABOVE
sameLeft ALIGN_LEFT
rightOf RIGHT_OF
sameBottom ALIGN_BOTTOM
below BELOW
sameRight ALIGN_RIGHT
centerInParent CENTER_IN_PARENT
alignParentLeft ALIGN_PARENT_LEFT
centerVertically CENTER_VERTICAL
alignParentTop ALIGN_PARENT_TOP
centerHorizontally CENTER_HORIZONTAL
alignParentRight ALIGN_PARENT_RIGHT
alignParentBottom ALIGN_PARENT_BOTTOM

四、创建组件

1. alertDialog

alert("message", "title") {
    positiveButton("ok" ) {
    }
    negativeButton("cancel") {
    }
}.show()

2. selector(Spinner)

selector("title", list) { _, i->
     tv.text = list[i]

3. progressDialog

4. indeterminateProgressDialog

五、跳转startActivity

startActivity(
                    "a" to ss		
                    , Pair("b", ss1))	//参数,以上两种写法都可

六、序列化data数据类

module的gradle根节点下导入

androidExtensions {
    experimental = true
}
  1. 添加注解@Parcelize
  2. 实现接口Parcelable
@Parcelize
data class E(var a: Int, var b: String) : Parcelable

你可能感兴趣的:(笔记)