最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Kotlin引用其他xml的view對象過程詳解

 更新時間:2023年02月16日 13:59:44   作者:破浪會有時  
這篇文章主要介紹了Kotlin中如何引用其他xml中的view對象,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧

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>

即,包含一個 EditText 和一個 Button。

那么問題來了,如何在 MainActivity.kt 中使用 buttonAdd 這個按鈕呢?

其實很簡單,首先,我們需要在 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對象時,系統(tǒng)會導(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小部件使用及最佳實踐

    Flutter 中的PageStorage小部件使用及最佳實踐

    在Flutter中,PageStorage小部件提供了一種方法來保存和恢復(fù)頁面間的信息,這對于具有多個頁面且需要在這些頁面之間共享狀態(tài)的應(yīng)用程序非常有用,本文將詳細(xì)介紹PageStorage的用途、如何使用它以及一些最佳實踐,感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • Android自定義popupwindow實例代碼

    Android自定義popupwindow實例代碼

    這篇文章主要為大家詳細(xì)介紹了Android自定義popupwindow實例代碼,popupwindow彈出菜單效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • android藍(lán)牙控制PC端代碼分享

    android藍(lán)牙控制PC端代碼分享

    這篇文章主要為大家分享了android藍(lán)牙控制PC端的詳細(xì)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Android開發(fā)之ImageSwitcher相冊功能實例分析

    Android開發(fā)之ImageSwitcher相冊功能實例分析

    這篇文章主要介紹了Android開發(fā)之ImageSwitcher相冊功能,結(jié)合實例形式分析了Android ImageSwitcher相冊的原理、使用方法及相關(guān)操作注意事項,需要的朋友可以參考下
    2019-03-03
  • Kotlin函數(shù)式編程超詳細(xì)介紹

    Kotlin函數(shù)式編程超詳細(xì)介紹

    一個函數(shù)式應(yīng)用通常由三大類函數(shù)構(gòu)成:變換transform、過濾filters合并combineo每類函數(shù)都針對集合數(shù)據(jù)類型設(shè)計,目標(biāo)是產(chǎn)生一個最終結(jié)果。函數(shù)式編程用到的函數(shù)生來都是可組合的,也就是說,你可以組合多個簡單函數(shù)來構(gòu)建復(fù)雜的計算行為
    2022-09-09
  • Android實現(xiàn)簡易計算器(可以實現(xiàn)連續(xù)計算)

    Android實現(xiàn)簡易計算器(可以實現(xiàn)連續(xù)計算)

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)簡易計算器,可以實現(xiàn)連續(xù)計算,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • Android中的動態(tài)加載機制的學(xué)習(xí)研究

    Android中的動態(tài)加載機制的學(xué)習(xí)研究

    本篇文章主要介紹了Android中的動態(tài)加載機制,對android項目開發(fā)有著一定的幫助,有興趣的同學(xué)可以了解一下。
    2016-11-11
  • Android Studio3.0升級后使用注意事項及解決方法

    Android Studio3.0升級后使用注意事項及解決方法

    這篇文章主要介紹了Android Studio3.0升級后使用注意事項及解決方法,需要的朋友參考下吧
    2017-12-12
  • Android 仿微信數(shù)字鍵盤

    Android 仿微信數(shù)字鍵盤

    大部分的金融App會對默認(rèn)的數(shù)字鍵盤進(jìn)行處理,以實現(xiàn)自定義的數(shù)字安全鍵盤?;诖耍疚膶ξ⑿艛?shù)字鍵盤樣式進(jìn)行了仿寫,實現(xiàn)了一套自定義的數(shù)字安全鍵盤(支持隨機數(shù)字分布)。
    2021-05-05
  • Android BaseAdapter適配器詳解用法

    Android BaseAdapter適配器詳解用法

    BaseAdapter是最基礎(chǔ)的Adapter類,也是最實用最常用的一個類,但是相比于ArrayAdapter之類的,對初學(xué)者來說卻比較難理解。所以本篇文章在這里介紹一下BaseAdapter
    2021-10-10

最新評論

南木林县| 罗田县| 朝阳市| 武义县| 饶河县| 长白| 岑溪市| 喀喇沁旗| 合水县| 南康市| 轮台县| 赤水市| 青州市| 西盟| 恩施市| 鞍山市| 红安县| 台北市| 淮南市| 彭阳县| 永川市| 大名县| 阿城市| 龙岩市| 枣庄市| 安阳市| 夏河县| 沧源| 赤城县| 定南县| 岳阳市| 公安县| 和平县| 宁远县| 蒙阴县| 精河县| 澄城县| 祥云县| 泸定县| 儋州市| 万源市|