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

原文标题:Kotlin for Android (IV): Custom Views and Android Extensions

原文链接:http://antonioleiva.com/kotlin-android-custom-views/

原文作者:Antonio Leiva(http://antonioleiva.com/about/

原文发布:2015-05-07

在阅读了扩展函数和默认值能做什么后,你可能想知道接下来是什么。如我们在Kotlin的第一篇文章说的那样,该语言可以使得Android开发更简单,所以还有很多事情我想谈谈。

定制视图

如果你还记得,在谈论Kotlin局限性时,我提到Kotlin前期版本(直至M10版本)不能够创建定制视图。其原因是我们只有一个选择:每个类仅能创建一个构造函数。由于使用可选参数,我们能按照自己需要产生构造函数的各种变化,通常,这就足够了。这里有一个例子:

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

3 
    init {
4// Initialization code
5 
    }

6 }

现在用唯一的构造函数,我们可以有四种方法产生类:

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

如你所见,我们只是用可选参数就得到很多组合。但是,如果我们试图通过扩展普通视图来创建Android定制视图,就带来一个问题。为了正确地工作,定制视图需要重载多个构造函数,我们没有方法实现它。幸好,从M11版本开始,我们有类似Java的方法,可声明多个构造函数了。下面是维持正方形比例的ImageView例子:

 1 class SquareImageView : ImageView {
 2  
 3     public constructor(context: Context) : super(context) {
 4     }
 5  
 6     public constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
 7     }
 8  
 9     public constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
10     }
11  
12     override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
13         super.onMeasure(widthMeasureSpec, heightMeasureSpec)
14         val width = getMeasuredWidth()
15         setMeasuredDimension(width, width)
16     }
17 }

十分简单吧。但或许不太详细,不过至少我们现在有方法实现了。

Kotlin Android扩展

在Kotlin M11版本中,还增加一个新插件,它可以帮助我们(Android开发者)以更便捷方法访问在XML中声明的视图。在你们遇到Butterknife时,有些人是会记住它使用起来更简单。

Kotlin Android扩展实际就是视图绑定。在代码中,它允许你通过视图的id就能用XML视图。不需要用任何外部注释或findViewById方法就可自动创建它们的属性。

要使用新插件,你需要安装Kotlin Android Extensions并将新的classpath添加到buildscript依赖关系中(在你的主build.gradle中):

1 buildscript {

2 3 
    dependencies {
4         …

5         classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
6     }
7 
}

假设你要声明下面main.xml</strong/>布局:

 1 <FrameLayout

 2     xmlns:android="..."
 3 
    android:id="@+id/frameLayout"
 4 
    android:orientation="vertical"
 5 
    android:layout_width="match_parent"
 6 
    android:layout_height="match_parent">
 7  
 8 

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


12  
13 </FrameLayout>

 

如果你要在Activity中使用这些视图,你只需要导入这个xml文件的symthetic属性

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

 

在我们例子中,它就是main

1 import kotlinx.android.synthetic.main.*

 

你现在就可以用视图的id访问它

1 override fun onCreate(savedInstanceState: Bundle?) {

2     super<BaseActivity>.onCreate(savedInstanceState)
3     setContentView(R.id.main)

4     frameLayout.setVisibility(View.VISIBLE)
5 
    welcomeText.setText("I´m a welcome text!!")

6 }

总结

这两项新特性的实现,明确地表明Kotlin团队非常感兴趣改善Android开发者的生活,让他们的生活更加轻松自如。他们还发布了Anko库,用DSL从Kotlin文件创建Android布局。我还没有使用过它的主要功能,但是在处理Android视图时,你可以用它来简化你的代码,在我上传到Github中的Kotlin项目里有一些它的例子。你可以找到这些例子以及其他许多东西。

下一篇文章将讨论Lambda表达式的使用以及它们怎样帮助我们简化代码和扩展编程语言。这十分有趣!对于我来说,与Java 1.7比较这是Kotlin最强大的一面。

前一篇:http://www.cnblogs.com/figozhg/p/4996136.html

原文地址:https://www.cnblogs.com/figozhg/p/4998865.html