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

Kotlin引用其他xml的view對(duì)象過(guò)程詳解

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

Kotlin 中如何引用其他xml中的view對(duì)象

比如,我們的 activity_main.xml 這么寫(xiě):

<?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。

那么問(wèn)題來(lái)了,如何在 MainActivity.kt 中使用 buttonAdd 這個(gè)按鈕呢?

其實(shí)很簡(jiǎn)單,首先,我們需要在 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ò)程詳解的文章就介紹到這了,更多相關(guān)Kotlin引用xml的view對(duì)象內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Flutter 中的PageStorage小部件使用及最佳實(shí)踐

    Flutter 中的PageStorage小部件使用及最佳實(shí)踐

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

    Android自定義popupwindow實(shí)例代碼

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

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

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

    Android開(kāi)發(fā)之ImageSwitcher相冊(cè)功能實(shí)例分析

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

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

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

    Android實(shí)現(xiàn)簡(jiǎn)易計(jì)算器(可以實(shí)現(xiàn)連續(xù)計(jì)算)

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎ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ī)制的學(xué)習(xí)研究

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

    Android Studio3.0升級(jí)后使用注意事項(xiàng)及解決方法

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

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

    大部分的金融App會(huì)對(duì)默認(rèn)的數(shù)字鍵盤(pán)進(jìn)行處理,以實(shí)現(xiàn)自定義的數(shù)字安全鍵盤(pán)?;诖?,本文對(duì)對(duì)微信數(shù)字鍵盤(pán)樣式進(jìn)行了仿寫(xiě),實(shí)現(xiàn)了一套自定義的數(shù)字安全鍵盤(pán)(支持隨機(jī)數(shù)字分布)。
    2021-05-05
  • Android BaseAdapter適配器詳解用法

    Android BaseAdapter適配器詳解用法

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

最新評(píng)論

农安县| 绥阳县| 渭源县| 鄂托克旗| 长寿区| 翁牛特旗| 临潭县| 尖扎县| 南康市| 溧水县| 东莞市| 宜州市| 灵山县| 东城区| 满洲里市| 甘孜| 本溪市| 二连浩特市| 安新县| 贵德县| 武平县| 东海县| 沭阳县| 班戈县| 新干县| 日照市| 凌云县| 棋牌| 永川市| 宁城县| 晋宁县| 龙里县| 开阳县| 义马市| 徐州市| 乌苏市| 永州市| 闽侯县| 新营市| 化隆| 涟源市|