用Kotlin開發Android應用(IV):定製視圖和Android擴充,kotlinandroid

來源:互聯網
上載者:User

用Kotlin開發Android應用(IV):定製視圖和Android擴充,kotlinandroid

原文標題: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 code5 
    }
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 9 
        android: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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.