还是建议去官网学习
https://github.com/JetBrains/anko
总共分为了四个模块
Anko是Kotlin库,可以使Android应用程序的开发变得更快,更轻松。它使您的代码干净且易于阅读,并且使您无需理会Android SDK for Java的粗糙之处。
Anko包含以下几个部分:
- Anko Commons:一个轻量级的库,其中包含用于意图,对话框,日志记录等的帮助程序;
- Anko Layouts:一种快速且类型安全的方式来编写动态Android布局;
- Anko SQLite:Android SQLite的查询DSL和解析器集合;
- Anko Coroutines:基于kotlinx.coroutines库的实用程序。
基于Gradle的项目
Anko具有元依赖性,可将所有可用功能(包括Commons,Layouts,SQLite)立即插入您的项目中:
dependencies {
implementation "org.jetbrains.anko:anko:$anko_version"
}
确保$anko_version
在项目级别的gradle文件中已结算:
ext.anko_version='0.10.8'
如果仅需要某些功能,则可以引用Anko的任何部件:
dependencies {
// Anko Commons
implementation "org.jetbrains.anko:anko-commons:$anko_version"
// Anko Layouts
implementation "org.jetbrains.anko:anko-sdk25:$anko_version" // sdk15, sdk19, sdk21, sdk23 are also available
implementation "org.jetbrains.anko:anko-appcompat-v7:$anko_version"
// Coroutine listeners for Anko Layouts
implementation "org.jetbrains.anko:anko-sdk25-coroutines:$anko_version"
implementation "org.jetbrains.anko:anko-appcompat-v7-coroutines:$anko_version"
// Anko SQLite
implementation "org.jetbrains.anko:anko-sqlite:$anko_version"
}
Android支持库还有许多工件:
dependencies {
// Appcompat-v7 (only Anko Commons)
implementation "org.jetbrains.anko:anko-appcompat-v7-commons:$anko_version"
// Appcompat-v7 (Anko Layouts)
implementation "org.jetbrains.anko:anko-appcompat-v7:$anko_version"
implementation "org.jetbrains.anko:anko-coroutines:$anko_version"
// CardView-v7
implementation "org.jetbrains.anko:anko-cardview-v7:$anko_version"
// Design
implementation "org.jetbrains.anko:anko-design:$anko_version"
implementation "org.jetbrains.anko:anko-design-coroutines:$anko_version"
// GridLayout-v7
implementation "org.jetbrains.anko:anko-gridlayout-v7:$anko_version"
// Percent
implementation "org.jetbrains.anko:anko-percent:$anko_version"
// RecyclerView-v7
implementation "org.jetbrains.anko:anko-recyclerview-v7:$anko_version"
implementation "org.jetbrains.anko:anko-recyclerview-v7-coroutines:$anko_version"
// Support-v4 (only Anko Commons)
implementation "org.jetbrains.anko:anko-support-v4-commons:$anko_version"
// Support-v4 (Anko Layouts)
implementation "org.jetbrains.anko:anko-support-v4:$anko_version"
// ConstraintLayout
implementation "org.jetbrains.anko:anko-constraint-layout:$anko_version"
}
下面还是和上一篇得内容差不多 但是多了一个taost方法。和一个更大得布局
下面直接展示。
mian布局 这里采用了驼峰得id命名。因为很多google写的源码api都是这样子
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.activity.RegisterActivity">
<Button
android:id="@+id/mRegiterBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/register" />
</LinearLayout>
code
package com.anguo.user.ui.activity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.anguo.user.R
import kotlinx.android.synthetic.main.activity_register.*
import org.jetbrains.anko.startActivity
class RegisterActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register)
mRegiterBtn.setOnClickListener{
startActivity(intentFor<TestActivity>("id" to 5))
startActivity<TestActivity>("id" to 5)
toast(R.string.register)
}
}
}
test页面布局使用anko的dsl
package com.anguo.user.ui.activity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import org.jetbrains.anko.*
import org.jetbrains.anko.sdk27.coroutines.onClick
class TestActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// setContentView(R.layout.activity_test)
// toast(intent.extras!!["id"].toString())
verticalLayout{
padding = 30
editText{
hint="Name"
textSize=24f
}
editText{
hint="Pwd"
textSize=24f
}
button{
text="Test"
onClick {
toast("10")
}
}
}
}
}
done
来源:oschina
链接:https://my.oschina.net/u/4396922/blog/4326611