Kotlin引用其他xml的view對(duì)象過程詳解
Kotlin 中如何引用其他xml中的view對(duì)象
比如,我們的 activity_main.xml 這么寫:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/navigation_header_main"
app:menu="@menu/activity_main_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
這里的 activity_main.xml 由兩部分組成:content_main 的 layout 以及 nav_view 的側(cè)邊欄。

content_main.xml 如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tool_bar"
android:textColor="#FFFFFF"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"/>
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/buttonAdd"
android:layout_width="88dp"
android:layout_height="48dp"
android:text="@string/add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/menuEditText"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/menuEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="@string/menu_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/buttonAdd"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
即,包含一個(gè) EditText 和一個(gè) Button。

那么問題來了,如何在 MainActivity.kt 中使用 buttonAdd 這個(gè)按鈕呢?
其實(shí)很簡單,首先,我們需要在 build.gradle (Module) 中添加 'kotlin-android-extensions':
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android-extensions'
}
然后,在 MainActivity.kt 中,當(dāng)我們使用 layout id 名稱獲取 content_main.xml view對(duì)象時(shí),系統(tǒng)會(huì)導(dǎo)入 import kotlinx.android.synthetic.main.content_main.*,這樣,我們就可以直接獲取其他 Layout 的 View 對(duì)象了。
到此這篇關(guān)于Kotlin引用其他xml的view對(duì)象過程詳解的文章就介紹到這了,更多相關(guān)Kotlin引用xml的view對(duì)象內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Activity之singletast啟動(dòng)模式及如何使用intent傳值
在一個(gè)新棧中創(chuàng)建該Activity實(shí)例,并讓多個(gè)應(yīng)用共享改棧中的該Activity實(shí)例。一旦改模式的Activity的實(shí)例存在于某個(gè)棧中,任何應(yīng)用再激活改Activity時(shí)都會(huì)重用該棧中的實(shí)例,其效果相當(dāng)于多個(gè)應(yīng)用程序共享一個(gè)應(yīng)用,不管誰激活該Activity都會(huì)進(jìn)入同一個(gè)應(yīng)用中2015-11-11
Android用注解與反射實(shí)現(xiàn)Butterknife功能
Butterknife是一個(gè)在android上實(shí)現(xiàn)ioc(控制反轉(zhuǎn))的一個(gè)庫。ioc的核心是解耦。解耦的目的是修改耦合對(duì)象時(shí)不影響另外一個(gè)對(duì)象,降低模塊之間的關(guān)聯(lián)。在Spring中ioc更多的是依靠xml的配置。而android上的IOC框架可以不使用xml配置2022-11-11
Android Activity啟動(dòng)模式全面解析
這篇文章主要介紹了Android Activity啟動(dòng)模式全面解析的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android自定義view制作抽獎(jiǎng)轉(zhuǎn)盤
這篇文章主要為大家詳細(xì)介紹了Android自定義view制作抽獎(jiǎng)轉(zhuǎn)盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
正確在Flutter中添加webview實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了正確在Flutter中添加webview實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android監(jiān)聽ScrollView滑動(dòng)距離的簡單處理
這篇文章主要為大家詳細(xì)介紹了Android監(jiān)聽ScrollView滑動(dòng)距離的簡單處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Android簡單實(shí)現(xiàn)無限滾動(dòng)自動(dòng)滾動(dòng)的ViewPager
這篇文章主要介紹了Android簡單實(shí)現(xiàn)無限滾動(dòng)自動(dòng)滾動(dòng)的ViewPager,百度谷歌上面也有很多關(guān)于這方面的教程,但是感覺都略顯麻煩,而且封裝的都不是很徹底。所以試著封裝一個(gè)比較好用的ViewPager,實(shí)現(xiàn)思路一起通過本文學(xué)習(xí)吧2016-12-12
Android Studio查看Android 5.x源碼的步驟詳解
Google為Android開發(fā)者帶來Android Studio,用來取代Eclipse。從Android Studio出現(xiàn)起,整機(jī)開發(fā)和Android源碼閱讀和編輯一定能用上它。這篇文章小編就帶大家學(xué)習(xí)下如何使用Android Studio查看Android 5.x源碼,有需要的可以參考借鑒。2016-09-09

