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 的侧边栏。

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对象时,系统会导入 import kotlinx.android.synthetic.main.content_main.*,这样,我们就可以直接获取其他 Layout 的 View 对象了。

到此这篇关于Kotlin引用其他xml的view对象过程详解的文章就介绍到这了,更多相关Kotlin引用xml的view对象内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Flutter 中的PageStorage小部件使用及最佳实践

    Flutter 中的PageStorage小部件使用及最佳实践

    在Flutter中,PageStorage小部件提供了一种方法来保存和恢复页面间的信息,这对于具有多个页面且需要在这些页面之间共享状态的应用程序非常有用,本文将详细介绍PageStorage的用途、如何使用它以及一些最佳实践,感兴趣的朋友跟随小编一起看看吧
    2024-05-05
  • Android自定义popupwindow实例代码

    Android自定义popupwindow实例代码

    这篇文章主要为大家详细介绍了Android自定义popupwindow实例代码,popupwindow弹出菜单效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • android蓝牙控制PC端代码分享

    android蓝牙控制PC端代码分享

    这篇文章主要为大家分享了android蓝牙控制PC端的详细代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • Android开发之ImageSwitcher相册功能实例分析

    Android开发之ImageSwitcher相册功能实例分析

    这篇文章主要介绍了Android开发之ImageSwitcher相册功能,结合实例形式分析了Android ImageSwitcher相册的原理、使用方法及相关操作注意事项,需要的朋友可以参考下
    2019-03-03
  • Kotlin函数式编程超详细介绍

    Kotlin函数式编程超详细介绍

    一个函数式应用通常由三大类函数构成:变换transform、过滤filters合并combineo每类函数都针对集合数据类型设计,目标是产生一个最终结果。函数式编程用到的函数生来都是可组合的,也就是说,你可以组合多个简单函数来构建复杂的计算行为
    2022-09-09
  • Android实现简易计算器(可以实现连续计算)

    Android实现简易计算器(可以实现连续计算)

    这篇文章主要为大家详细介绍了Android实现简易计算器,可以实现连续计算,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-03-03
  • Android中的动态加载机制的学习研究

    Android中的动态加载机制的学习研究

    本篇文章主要介绍了Android中的动态加载机制,对android项目开发有着一定的帮助,有兴趣的同学可以了解一下。
    2016-11-11
  • Android Studio3.0升级后使用注意事项及解决方法

    Android Studio3.0升级后使用注意事项及解决方法

    这篇文章主要介绍了Android Studio3.0升级后使用注意事项及解决方法,需要的朋友参考下吧
    2017-12-12
  • Android 仿微信数字键盘

    Android 仿微信数字键盘

    大部分的金融App会对默认的数字键盘进行处理,以实现自定义的数字安全键盘。基于此,本文对对微信数字键盘样式进行了仿写,实现了一套自定义的数字安全键盘(支持随机数字分布)。
    2021-05-05
  • Android BaseAdapter适配器详解用法

    Android BaseAdapter适配器详解用法

    BaseAdapter是最基础的Adapter类,也是最实用最常用的一个类,但是相比于ArrayAdapter之类的,对初学者来说却比较难理解。所以本篇文章在这里介绍一下BaseAdapter
    2021-10-10

最新评论