使用Kotlin开发Android应用(IV):自定义视图和Android扩展

@author ASCE1885的 Github 简书 微博 CSDN
原文链接

在读完扩展函数和默认值这篇文章之后,那么接下来要介绍什么呢?在本系列第一篇文章中我们说过,Kotlin使得Android开发更加简单,本文我们将进一步作介绍。

自定义视图

你应该还记得,在说到Kotlin的局限性时,我们提到了在Kotlin早期版本(M10之前)是不支持自定义视图的,因为当时只能为每个类创建一个构造函数。这通常是足够的,因为使用可选参数,我们可以创建足够多的构造函数变种,例如:

class MyClass(param: Int, optParam1: String = "", optParam2: Int = 1) {

    init {
        // Initialization code
    }
}

通过这一个构造函数,我们有如下四种方式创建这个类:

val myClass1 = MyClass(1)val myClass2 = MyClass(1, "hello")val myClass3 = MyClass(param = 1, optParam2 = 4)val myClass4 = MyClass(1, "hello", 4)

正如你所见,使用可选参数我们得到了一堆的组合。但是通过继承普通Views的方式来创建自定义Views时,我们可能会遇到问题。自定义Views需要重写一个或者多个构造函数以便正常工作,但我们做不到这一点。幸运的是,自动Kotlin M11开始,我们可以声明多个构造函数,类似Java。下面是一个正方形的ImageView:

class SquareImageView : ImageView { public constructor(context: Context) : super(context) { }    public constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { }    public constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { }    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { super.onMeasure(widthMeasureSpec, heightMeasureSpec) val width = getMeasuredWidth() setMeasuredDimension(width, width) }}

通过很简单的代码我们实现了自定义Views。

Kotlin Android扩展

从Kotlin M11开始增加的扩展插件使得Android开发者更容易取得XML文件中定义的Views。你可能会觉得它很像ButterKnife,但使用起来更简单。

Kotlin Android扩展本质上是一个视图绑定,使得开发者在代码中通过id就可以使用XML文件中定义的Views。它将自动为Views创建属性值,而不用使用第三方注解框架或者findViewById函数。

要使用这个特性,首先需要安装插件“Kotlin Android Extension”,并在build.gradle文件的构建脚本依赖中增加一个新的classpath:

buildscript {
    …
    dependencies {        …
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"    }
}

假如你定义了名为main.xml的布局文件:

<FrameLayout
  xmlns:android="..."android:id="@+id/frameLayout"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent">

    <TextView 
 android:id="@+id/welcomeText"android:layout_width="wrap_content"android:layout_height="wrap_content"/>

</FrameLayout>

如果想在Activity中使用这个文件里面定义的Views,你只需要导入这个xml文件的synthetic属性值,如下所示:

import kotlinx.android.synthetic.<xml_name>.*

具体到我们的例子,则如下所示:

import kotlinx.android.synthetic.main.*

这样你就可以使用id来取得这些views的引用了:

override fun onCreate(savedInstanceState: Bundle?) {
    super<BaseActivity>.onCreate(savedInstanceState)    setContentView(R.id.main)
    frameLayout.setVisibility(View.VISIBLE)
    welcomeText.setText("I´m a welcome text!!")
}

总结

这两个新特性的引入,让我们看到了Kotlin团队致力于让Android开发更简单。他们也发布了一个名为Anko的函数库,这个一门从Kotlin文件创建Android布局的领域特定语言(DSL)。我还没有使用过它的主要功能,但当处理Android视图时,你可以使用它来简化代码。在我上传到Github上的开源项目Bandhook-Kotlin也有相关的例子。

下一篇文章将介绍lambda表达式的使用,以及它如何简化代码和扩展语言的特性。非常有趣的一个特性,在我看来,这是Kotlin相比Java 1.7最强大的方面。

文末摄影鉴赏

更多相关文章

  1. 一款常用的 Squid 日志分析工具
  2. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  3. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  4. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  5. Android(安卓)Studio导入第三方jar包或依赖工程的方法
  6. 【多媒体编解码】Openmax IL (二)Android多媒体编解码Component架
  7. Android(安卓)自动生成的R类
  8. 【iOS-cocos2d-X 游戏开发之七】整合Cocos2dX的Android项目到Xco
  9. 安卓下载安装更新包,各个版本注意事项

随机推荐

  1. Android应用程序启动
  2. Android视图加载到窗口的过程分析
  3. 将 cglib 动态代理思想带入 Android 开发
  4. 在Android 中使用KSOAP2调用WebService(一
  5. Android Recovery 解析
  6. android 弹出的软键盘遮挡住EditText文本
  7. Android的binder机制分析
  8. Android之Http网络编程(一)
  9. Android之A面试题③应用程序启动过程源代
  10. Android Service之MountService源码分析