如何在android中使用kotlin合成从包含的布局访问视图

2024-03-07

在我的片段布局中,我添加了一个包含 include 的布局

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

<data>

  
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:itemCount="3"
        tools:listitem="@layout/list_item_eceived" />

    <include
        layout="@layout/item_user_input"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

我包含的布局

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

<data>

    <variable
        name="hint"
        type="String" />
    <variable
        name="available"
        type="Boolean" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/message_box_parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="12dp"
    android:elevation="2dp"
    android:minHeight="50dp"
    app:layout_goneMarginStart="6dp">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/message_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="4dp"
        android:focusable="true"
        android:hint="@{hint}"
        android:maxHeight="128dp"
        android:padding="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/send_btn_view"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/send_btn_view"
        android:layout_width="44dp"
        android:layout_height="44dp"
        android:layout_marginEnd="2dp"
        android:paddingStart="12dp"
        android:paddingTop="8dp"
        android:paddingEnd="8dp"
        android:paddingBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

我访问片段中的视图

send_btn_view.setOnClickListener { 
        Log.e("button","clicked")
    }

合成进口为import kotlinx.android.synthetic.main.item_user_input.*

当我运行该应用程序时,我收到错误

Unresolved reference: item_user_input

你不能,因为如果多次包含相同的布局,综合库将不知道该怎么做。

无论如何,Kotlin Synthetic 已被弃用,Google 官方建议迁移到视图/数据绑定:https://developer.android.com/topic/libraries/view-binding/migration https://developer.android.com/topic/libraries/view-binding/migration

由于您已经使用数据绑定,我建议使用绑定类来访问视图。通过这种方式,您可以明确提及包含的布局。

首先,将 id 添加到包含的布局中:

   <include
        android:id="@+id/item_user_input"
        layout="@layout/item_user_input"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

二、访问视图:

binding.itemUserInput.sendBtnView
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在android中使用kotlin合成从包含的布局访问视图 的相关文章

  • 将 Apple Touch ID 或 Samsung 指纹识别器与我的应用程序集成

    我目前正在开发一个 Android ios 移动应用程序 它有以下要求 请告诉我是否可以使用最新的智能手机 平板电脑上内置的指纹识别器来实现我的要求 我有一个在云上运行的人力资源管理系统 其中有员工详细信息 现在我正在开发一个用于员工考勤的
  • 返回重复假期活动的日历

    我发现 当有两个帐户链接到他们的设备 并且两个帐户都同步其日历 并且两个帐户都设置为显示 Google 假日日历时 CalendarContract Instances 将每个假日返回两次 每个帐户返回一次日历 因此 在这种情况下 您会看到
  • Eclipse 运行时 Dalvik 错误

    当我要运行任何程序时 会显示此对话框 在 Eclipse 中 这些错误显示在控制台中 2013 02 25 19 05 09 Dex Loader Unable to execute dex Target out of range 0000
  • 切换大小写错误。用于 Mac 上 Android 开发的 Eclipse IDE:将工作区合规性更改为 JRE 1.7

    当尝试在 android 项目中使用带有 switch string 的 Switch Case 时 我在 eclipse IDE 中收到错误消息 将工作区合规性更改为 JRE1 7 对于低于 1 7 的源级别 无法打开字符串类型的值 仅允
  • Android Studio 3.1.3不显示布局设计预览

    自从我上次在 android studio 中创建一个新项目以来已经有一段时间了 今天当我这样做时 android studio 没有从布局预览中显示任何内容 它是一个空白窗口 并显示 没有可显示的内容 我已经尝试过了 更新了sdks工具
  • ViewPager - 无限滚动? [复制]

    这个问题在这里已经有答案了 我需要在屏幕上显示文本或图像 文本或图像 一个请求总共 10 个 是通过 HttpClient 从我的服务器获取的 当用户向右滑动并到达第 8 张幻灯片时 我需要从服务器获取下一组 10 个文本或图像 使用 Vi
  • 将 match_parent 转换为“0dp”

    当我使用android layout height match parent or android layout width match parent 作为约束布局中子项的高度 宽度并构建 Gradle 文件 它会自动更改为android
  • 如何根据 ListActivity 中长按的项目设置特定的上下文菜单?

    我有一个列表活动 我选择手动添加第一个项目 即 添加新项目 我已经注册了整个列表视图的上下文菜单 使用registerForContextMenu getListView 直接进入onCreate 当建立上下文菜单时 系统调用onCreat
  • 动态添加的 RemoteView 上的布局权重

    在我的小部件中 我使用以下内容将项目 R layout widget item 动态添加到我的主小部件布局中定义的 LinearLayout 中 Main widget layout RemoteViews views new Remote
  • DP5 7.0 - 向待处理意图添加额外内容是否会失败?

    在跟踪器上添加链接的问题 因此 我今天在我的 Nexus 5X 上安装了 DP5 Android 7 0 版本 我一直在开发一个应用程序 它使用 Android 的 AlarmManager 类在特定时间安排本地通知 在此版本之前 代码在运
  • 制作教程(教练标记)叠加层。需要帮助根据另一个视图的位置移动视图

    你好 我正在尝试在教程上做一个教练标记 主程序 java public class Main extends Activity private Button button1 private int x Override public voi
  • 为什么 CheckBox 检查不能以编程方式与 Kotlin 一起使用?

    我想这个问题以前可能有人问过 但这个问题也发生在我身上 所以我在这里再次询问 看看我们能否找到解决方案 所以基本上问题是以编程方式检查复选框不与 Kotlin 代码一起工作 为了解释一下 我正在分享我的代码和问题的屏幕截图 filterCo
  • Android:直接从浏览器下载文件

    我试图让 Android 浏览器下载特定类型 xxx 的文件 这样我就可以设置一个应用程序与其关联 我已经成功完成了关联部分 因为我已经做到了 以便在资源管理器应用程序中单击正确类型的文件会加载适当的应用程序 我希望这会转移到浏览器 这样如
  • Android SearchView 自定义

    我是 Android 新手 我被困在一些我认为很简单但我很困惑的事情上 我需要不在操作栏 工具栏中而是在我的相对布局中创建自定义 searchView 问题是我不知道如何自定义背景 文本输入颜色 XML 中的搜索图标颜色 或者只是它们的属性
  • 具有 jsonObject 的 android 列表视图

    我正在开展一项活动 该活动请求服务器上的一个 php 文件 此 php 文件将返回给我一个JSONArray having JSONObjects作为它的元素 我明白了jArray并提取其内容 例如所有jsonObjects 每个 json
  • 将 jstring 转换为 QString

    我正在调用一个返回字符串的 Java 函数 QAndroidJniObject obj QAndroidJniObject callStaticObjectMethod
  • Kotlin - 从地图列表到按键分组的地图

    我有一个List
  • Android:将“内部”链接添加到 TextView 的一部分,该链接链接到我的代码中的操作

    正如标题所解释的 我想添加链接到我的TextView 有这两个警告 我希望链接能够作用于TextView 不是完整的 类似于AHTML 中的锚点 我希望链接指向我的代码中的操作 而不是网站 我可以在我的活动中定义一个方法 或者实现一个OnC
  • 查看 Android 应用程序的共享首选项?

    当我在 Eclipse 中处理我的应用程序时 有没有办法在模拟器中调试时查看我对应用程序的共享首选项所做的更改 提前致谢 在模拟器中运行项目 然后从 Eclipse 选择菜单 Windows gt 打开透视图 gt DDMS 从选项卡设备中
  • 如何使用 androidX 不破坏片段

    现在 我尝试 androidX 导航和底部导航栏 当我像下面一样使用它时 supportFragmentManager getSupportFragmentManager navHostFragment NavHostFragment su

随机推荐