Kotlin引用其他xml的view對象過程詳解
Kotlin 中如何引用其他xml中的view對象
比如,我們的 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對象時(shí),系統(tǒng)會(huì)導(dǎo)入 import kotlinx.android.synthetic.main.content_main.*,這樣,我們就可以直接獲取其他 Layout 的 View 對象了。
到此這篇關(guān)于Kotlin引用其他xml的view對象過程詳解的文章就介紹到這了,更多相關(guān)Kotlin引用xml的view對象內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Flutter 中的PageStorage小部件使用及最佳實(shí)踐
在Flutter中,PageStorage小部件提供了一種方法來保存和恢復(fù)頁面間的信息,這對于具有多個(gè)頁面且需要在這些頁面之間共享狀態(tài)的應(yīng)用程序非常有用,本文將詳細(xì)介紹PageStorage的用途、如何使用它以及一些最佳實(shí)踐,感興趣的朋友跟隨小編一起看看吧2024-05-05
Android自定義popupwindow實(shí)例代碼
這篇文章主要為大家詳細(xì)介紹了Android自定義popupwindow實(shí)例代碼,popupwindow彈出菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android開發(fā)之ImageSwitcher相冊功能實(shí)例分析
這篇文章主要介紹了Android開發(fā)之ImageSwitcher相冊功能,結(jié)合實(shí)例形式分析了Android ImageSwitcher相冊的原理、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-03-03
Android實(shí)現(xiàn)簡易計(jì)算器(可以實(shí)現(xiàn)連續(xù)計(jì)算)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡易計(jì)算器,可以實(shí)現(xiàn)連續(xù)計(jì)算,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
Android中的動(dòng)態(tài)加載機(jī)制的學(xué)習(xí)研究
本篇文章主要介紹了Android中的動(dòng)態(tài)加載機(jī)制,對android項(xiàng)目開發(fā)有著一定的幫助,有興趣的同學(xué)可以了解一下。2016-11-11
Android Studio3.0升級后使用注意事項(xiàng)及解決方法
這篇文章主要介紹了Android Studio3.0升級后使用注意事項(xiàng)及解決方法,需要的朋友參考下吧2017-12-12

