转载请标明出处:
http://blog.csdn.net/yuguo_tianqing/article/details/78191438
本文出自YuGuo_TianQing的博客
原话是这么说的:
Anko is a Kotlin library which makes Android application development faster and easier. It makes your code clean and easy to read, and lets you forget about rough edges of the Android SDK for Java.
基础翻译:anko是一个kotlin library,能使Android应用程序开发更快更容易。让使您的代码简洁,易于阅读,让您忘记Android SDK for java的粗糙边缘。
我也不多说了,直接给github上面的连接:https://github.com/Kotlin/anko
直接上代码,让你体会一下它的魅力。基础代码:
在开发之前你必须要: compile "org.jetbrains.anko:anko:$anko_version" , 详细情况github上面也已经说明。
首先创建了一个AnkoActivity,并在AndroidManifest.xml中声明了。然后就开始我们的anko布局。
class AnkoActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
verticalLayout {
val name = editText {
hint = getString(R.string.button_hint)
background = null
}
button("Say Hello") {
onClick { toast("Hello,${name.text}!") }
backgroundColor = ContextCompat.getColor(this@AnkoActivity, R.color.colorPrimary)
}.lparams(width = wrapContent, height = wrapContent) {
gravity = Gravity.CENTER
}
}
}
}
看了上面的代码和效果图之后,感觉如何? 嘿嘿........
在开发之前一定要记得先: compile "org.jetbrains.anko:anko-recyclerview-v7:$anko_version"
activity:
class AnkoActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val str = arrayOfNulls(20)
for (i in 0..19) {
str[i] = i.toString()
}
verticalLayout {
val name = editText {
hint = getString(R.string.button_hint) // 你为何这么叼
background = null
}
button("Say Hello") {
onClick { toast("Hello,${name.text}!") }
backgroundColor = ContextCompat.getColor(this@AnkoActivity, R.color.colorPrimary)
}.lparams(width = wrapContent, height = wrapContent) {
gravity = Gravity.CENTER
}
recyclerView {
backgroundColor = ContextCompat.getColor(this@AnkoActivity, android.R.color.white)
layoutManager = LinearLayoutManager(this@AnkoActivity, LinearLayoutManager.VERTICAL, false)
val adapter = MainAdapter(this@AnkoActivity, str)
this.adapter = adapter
adapter.setOnItemClickListener(object : OnItemClickListener {
override fun onclick(v: View, position: Int) {
toast(position.toString())
}
})
}
}
}
}
有一点不一样,其它的都一样,我把不一样的地方贴出来:
class MainAdapter(val context: Context, val data: Array) : RecyclerView.Adapter() {
多了一个问号,因为传递的数据我是这样定义的:val str = arrayOfNulls
效果图:
class AnkoActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val str = arrayOfNulls(20)
for (i in 0..19) {
str[i] = i.toString()
}
verticalLayout {
val name = editText {
hint = getString(R.string.button_hint)
background = null
}
button("Say Hello") {
onClick { toast("Hello,${name.text}!") }
backgroundColor = ContextCompat.getColor(this@AnkoActivity, R.color.colorPrimary)
}.lparams(width = wrapContent, height = wrapContent) {
gravity = Gravity.CENTER
}
swipeRefreshLayout {
setOnRefreshListener {
Timer().schedule(object : TimerTask() { // 模拟网络请求
override fun run() {
runOnUiThread {
isRefreshing = false
toast("22222222")
}
}
}, 2500)
}
recyclerView {
backgroundColor = ContextCompat.getColor(this@AnkoActivity, android.R.color.white)
layoutManager = LinearLayoutManager(this@AnkoActivity, LinearLayoutManager.VERTICAL, false)
val adapter = MainAdapter(this@AnkoActivity, str)
this.adapter = adapter
adapter.setOnItemClickListener(object : OnItemClickListener {
override fun onclick(v: View, position: Int) {
toast(position.toString())
}
})
}
}
}
}
}
接下来我贴上所有的代码以及:
class AnkoActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val str = arrayOfNulls(20)
for (i in 0..19) {
str[i] = i.toString()
}
verticalLayout {
backgroundColor = ContextCompat.getColor(this@AnkoActivity, R.color.colorAccent) // 背景色
val name = editText {
hint = getString(R.string.button_hint) // 你为何这么叼
background = null
}
button("Say Hello") {
onClick { toast("Hello,${name.text}!") } // 点击事件
backgroundColor = ContextCompat.getColor(this@AnkoActivity, R.color.colorPrimary)
}.lparams(width = wrapContent, height = wrapContent) {
gravity = Gravity.CENTER
}
swipeRefreshLayout {
setOnRefreshListener {
Timer().schedule(object : TimerTask() { // 模拟网络请求
override fun run() {
runOnUiThread {
isRefreshing = false
toast("22222222")
}
}
}, 2500)
}
recyclerView {
backgroundColor = ContextCompat.getColor(this@AnkoActivity, android.R.color.white) // 背景色
layoutManager = LinearLayoutManager(this@AnkoActivity, LinearLayoutManager.VERTICAL, false)
val adapter = MainAdapter(this@AnkoActivity, str)
this.adapter = adapter
adapter.setOnItemClickListener(object : OnItemClickListener {
override fun onclick(v: View, position: Int) {
toast(position.toString())
}
})
}
}.lparams(width = matchParent, height = dip(300))
verticalLayout {
orientation = LinearLayout.HORIZONTAL
textView("我是第一") {
}
textView("我是第二") {
}
}
}
}
}
总结:
前面的都是一些基本的应用。anko的运用中,肯定还可以自定义控件,同时你只需自定义一个,其它都可以进行引用。等等·······功能
这一章就到这里了, 也不太善言辞,不对的地方请指出,我及时修正,我会继续进步的,争取写出好的文章。