zoukankan      html  css  js  c++  java
  • 使用Kotlin开发Android应用(IV):自定义视图和Android扩展

    在读完扩展函数和默认值这篇文章之后,那么接下来要介绍什么呢?在本系列第一篇文章中我们说过,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最强大的方面。

  • 相关阅读:
    Stm32CubeMX5 配置 STM32的串口DMA接受方式 --- 基于 stm32f051k8u6
    Stm32 控制1.44寸液晶显示图片 基于stm32f051k8u6
    makefile自动编译
    Stm32CubeMX5 创建LED控制工程
    ARM 汇编与C之间 的调用
    shell 脚本文件类型.sh ,变量
    bzoj3589 动态树 求链并 容斥
    bzoj2287【POJ Challenge】消失之物 缺一01背包
    bzoj2916: [Poi1997]Monochromatic Triangles 思路
    [NOI2010]超级钢琴 主席树
  • 原文地址:https://www.cnblogs.com/simadi/p/6688430.html
Copyright © 2011-2022 走看看