I have a following layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@android:color/white"
    android:paddingLeft="20dp"
    android:paddingRight="20dp">

    <TextView
        android:id="@+id/tvErrorTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textColor="@android:color/background_dark"
        android:textSize="18sp"
        />
    <TextView
        android:id="@+id/tvErrorDesc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:textColor="@android:color/darker_gray"
        android:textSize="16sp"
        />
    <TextView
        android:id="@+id/tvAction"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="10dp"
        android:layout_gravity="end"
        android:padding="5dp"
        android:textSize="15sp"
        android:textStyle="bold"
        android:textAllCaps="true"
        android:textColor="@android:color/holo_purple"
        />
</LinearLayout>

当我想在下面这样的活动之外使用kotlin android extensions时,它不起作用.最后我做了findViewById.

...
...
import kotlinx.android.synthetic.main.dialog_error.*
...
...
 val view = LayoutInflater.from(context).inflate(R.layout.dialog_error, null, false)
    val tvErrorTitle = view.findViewById(R.id.tvErrorTitle) as TextView
    val tvErrorDesc = view.findViewById(R.id.tvErrorDesc) as TextView
    val tvErrorAction = view.findViewById(R.id.tvAction) as TextView

它不会直接从xml中提取视图.如何在程序inflating 布局中使用它,避免findViewById

Note:这个问题严格属于Kotlin Android Extensions人,而不是语言本身.

Edit I have imported both :

import kotlinx.android.synthetic.main.dialog_error.view.*
import kotlinx.android.synthetic.main.dialog_error.*

But Android Studio still tries to import from R.id and doesn't recognize those two imports. Is there anything missing?

推荐答案

From the docs you linked:

如果我们想在视图中调用合成属性(在适配器类中很有用),我们还应该导入

kotlinx.android.synthetic.main.activity_main.view.*.

That is, import kotlinx.android.synthetic.main.layout.view.* as well to load the View extension properties.

Then:

val view = LayoutInflater.from(context).inflate(...)
view.tvErrorTitle.text = "test"

Kotlin相关问答推荐

这些Kotlin函数等效吗?

如何让Gradle8+在编译Kotlin代码之前编译Groovy代码?然后把它们混合在一个项目中?

Kotlin Poet 的导入不明确

kotlin短路列表判断吗?

使用 Jetpack Compose 使用参数导航

如何在 Spring Boot 3 中为内部类提供运行时提示

mutableStateOf 在 Jetpack Compose 中不记住 API 的情况下保持重组后的值

内容更改后的 var 重新计算

Eclipse:无法安装 Kotlin 插件

有没有一种简单的方法可以将数组/列表中的每个元素相互相乘 - Kotlin?

伴随对象在变量更改时更改它的值

DatabaseManager_Impl 不是抽象的,不会覆盖 RoomDatabase 中的抽象方法 clearAllTables()

androidx.core:core-ktx:1.0.0 小部件包丢失

Kotlin中的下划线名称是为什么保留的?

Kotlin中具有多个参数的绑定适配器

TypeConverter()在Android的TypeConverter错误中具有私有访问权限

在Kotlin中为Android编写库会有开销吗?

Kotlin:相互递归函数的尾部递归

类型不匹配推断类型为单位,但应为空

导航架构组件 - 未生成 DestinationFragmentArgs