欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2023年02月16日 13:59:44   作者:破浪會有時  
這篇文章主要介紹了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>

即,包含一個 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 中,當我們使用 layout id 名稱獲取 content_main.xml view對象時,系統(tǒng)會導入 import kotlinx.android.synthetic.main.content_main.*,這樣,我們就可以直接獲取其他 Layout 的 View 對象了。

到此這篇關于Kotlin引用其他xml的view對象過程詳解的文章就介紹到這了,更多相關Kotlin引用xml的view對象內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 詳解Activity之singletast啟動模式及如何使用intent傳值

    詳解Activity之singletast啟動模式及如何使用intent傳值

    在一個新棧中創(chuàng)建該Activity實例,并讓多個應用共享改棧中的該Activity實例。一旦改模式的Activity的實例存在于某個棧中,任何應用再激活改Activity時都會重用該棧中的實例,其效果相當于多個應用程序共享一個應用,不管誰激活該Activity都會進入同一個應用中
    2015-11-11
  • Android用注解與反射實現(xiàn)Butterknife功能

    Android用注解與反射實現(xiàn)Butterknife功能

    Butterknife是一個在android上實現(xiàn)ioc(控制反轉(zhuǎn))的一個庫。ioc的核心是解耦。解耦的目的是修改耦合對象時不影響另外一個對象,降低模塊之間的關聯(lián)。在Spring中ioc更多的是依靠xml的配置。而android上的IOC框架可以不使用xml配置
    2022-11-11
  • Android Activity啟動模式全面解析

    Android Activity啟動模式全面解析

    這篇文章主要介紹了Android Activity啟動模式全面解析的相關資料,需要的朋友可以參考下
    2016-02-02
  • Android自定義view制作抽獎轉(zhuǎn)盤

    Android自定義view制作抽獎轉(zhuǎn)盤

    這篇文章主要為大家詳細介紹了Android自定義view制作抽獎轉(zhuǎn)盤,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • 正確在Flutter中添加webview實現(xiàn)詳解

    正確在Flutter中添加webview實現(xiàn)詳解

    這篇文章主要為大家介紹了正確在Flutter中添加webview實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Android監(jiān)聽ScrollView滑動距離的簡單處理

    Android監(jiān)聽ScrollView滑動距離的簡單處理

    這篇文章主要為大家詳細介紹了Android監(jiān)聽ScrollView滑動距離的簡單處理,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Android簡單實現(xiàn)無限滾動自動滾動的ViewPager

    Android簡單實現(xiàn)無限滾動自動滾動的ViewPager

    這篇文章主要介紹了Android簡單實現(xiàn)無限滾動自動滾動的ViewPager,百度谷歌上面也有很多關于這方面的教程,但是感覺都略顯麻煩,而且封裝的都不是很徹底。所以試著封裝一個比較好用的ViewPager,實現(xiàn)思路一起通過本文學習吧
    2016-12-12
  • Android嵌套滑動沖突的解決方法

    Android嵌套滑動沖突的解決方法

    本篇文章主要介紹了Android嵌套滑動沖突的解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Android ViewFlipper簡單用法解析

    Android ViewFlipper簡單用法解析

    這篇文章主要為大家詳細介紹了Android ViewFlipper簡單用法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android Studio查看Android 5.x源碼的步驟詳解

    Android Studio查看Android 5.x源碼的步驟詳解

    Google為Android開發(fā)者帶來Android Studio,用來取代Eclipse。從Android Studio出現(xiàn)起,整機開發(fā)和Android源碼閱讀和編輯一定能用上它。這篇文章小編就帶大家學習下如何使用Android Studio查看Android 5.x源碼,有需要的可以參考借鑒。
    2016-09-09

最新評論