kotlin anko 第一个项目

参考链接:官方kotlinAnko anko-example

官方提供 demo 提示一下错误

org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all files for configuration ':classpath

按照提示修改 project build.gradle

 ext.kotlin_version = '1.1.2'  -->  1.2.31  // 我本地的kotlin 下载的插件版本

classpath 'com.android.tools.build:gradle:2.4.0-alpha7' --> com.android.tools.build:gradle:3.0.1

继续出错:

Unable to find method 'com.android.build.gradle.internal.variant.BaseVariantData.getOutputs()Ljava/util/List;'.
Possible causes for this unexpected error include:

  • Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
    Re-download dependencies and sync project (requires network)
  • The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
    Stop Gradle build processes (requires restart)
  • Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.
In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.
下载不成功对应的 .jar 包。重复下载一直提示错误,修改了 ext.kotlin_version = '1.1.2'  --》 ext.kotlin_version = '1.2.31' 工程终于不报错。最终 project bugild.gradle 配置。

buildscript {
    ext.kotlin_version = '1.2.31'
    ext.anko_version = '0.10.0-beta-2'

    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url "http://dl.bintray.com/kotlin/kotlin-dev" }
    }
}

以为已经可以直接运行然而并没有。run 以后又出一下错误:

gravity = CENTER 报错,布局中直接使用错误,应该替换为 setGravity(Gravity.CENTER)

后续错误又来:

E:\android_kotlin\anko-example\app\src\main\java\org\example\ankodemo\RichView.kt: (59, 79): None of the following functions can be called with the arguments supplied:
public constructor LinearLayout(p0: Context!) defined in android.widget.LinearLayout
public constructor LinearLayout(p0: Context!, p1: AttributeSet!) defined in android.widget.LinearLayout

注释三个参数的 constructor 方法。代码终于可以运行。 修改后自定义 View 代码。刚学习,不清楚具体原因后续有时间好好找找原因。

class RichView : LinearLayout {
    private lateinit var image: ImageView
    private lateinit var text: TextView

    private fun init() = AnkoContext.createDelegate(this).apply {
        padding = dip(24)
        setGravity(Gravity.CENTER)
        image = imageView(imageResource = R.drawable.kotlin) {
            onClick { startAnimation() }
//            gravity = CENTER_VERTICAL
            padding = dip(8)
            layoutParams = LinearLayout.LayoutParams(dip(48), dip(48))
        }

        text = textView("Anko Rich view") {
            textSize = 19f
        }

        startAnimation()
    }

    private fun startAnimation() {
        if (image.animation != null) return

        image.startAnimation(ScaleAnimation(1f, 1.3f, 1f, 1.3f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
        ).apply {
            repeatCount = 1
            repeatMode = Animation.REVERSE
            duration = 1000
        })
    }

    constructor(context: Context?) : super(context) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    /*constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init()
    }*/
}

@Suppress("NOTHING_TO_INLINE")
inline fun ViewManager.myRichView(theme: Int = 0) = myRichView({}, theme)
inline fun ViewManager.myRichView(init: RichView.() -> Unit, theme: Int = 0) = ankoView(::RichView, theme, init)

自己写了一个 demo 页面

class HelloAnkoActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        HelloAnkoActivityUI().setContentView(this)
    }
}

class HelloAnkoActivityUI : AnkoComponent {
    override fun createView(ui: AnkoContext) = with(ui) {
       // TODO("not implemented") //To change body of created functions use File | Settings | File Templates.

        verticalLayout {
            padding = dip(15)
            backgroundColor = Color.rgb(88, 88, 88)

            val tv = textView {
                text = "hello Anko"
                textSize = resources.getDimension(R.dimen.abc_text_size_medium_material)
                textColor = ContextCompat.getColor(context, R.color.abc_background_cache_hint_selector_material_dark)
            }
            tv.setOnClickListener {
                val srt = "ssss${tv.text}"
                print(srt)
            }
            button {
                text = "Click Toast text"
                onClick {
                    toast(tv.text)
                }
            }
            button{
                text = "radioButton"
                textColor = Color.YELLOW
                onClick {
                    Snackbar.make(view, "snackbar${text}", Snackbar.LENGTH_LONG).setAction("返回") {
                        ui.owner.finish()
                    }.show()
                }
            }
        }      
    }
}

 

你可能感兴趣的:(Kotlin,学习)