18

我有以下布局:

<?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 扩展时,它不起作用。我最终做了 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 中提取视图。如何在程序膨胀布局中使用它并避免findViewById

注意:这个问题完全属于Kotlin Android Extensions,而不是语言本身。

编辑 我已经导入了两个:

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

但是 Android Studio 仍然尝试从 R.id 导入并且不识别这两个导入。有什么遗漏吗?

4

3 回答 3

44

您链接的文档中:

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

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

也就是说,导入kotlinx.android.synthetic.main.layout.view.*以及加载View扩展属性。

然后:

val view = LayoutInflater.from(context).inflate(...)
view.tvErrorTitle.text = "test"
于 2017-06-25T16:40:34.890 回答
7

它返回一个膨胀的视图:

layoutInflater.inflate(R.layout.your_layout, null)

看,当你的类从 Context 超类扩展时,你可以用 this 替换LayoutInflater.from(context)layoutInflater

于 2018-11-27T17:01:48.623 回答
1

在 kotlin 中,您可以尝试使用数据绑定在线性布局内膨胀布局

val inflater: LayoutInflater = LayoutInflater.from(activity).context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater

 val mBindingDeno: LayoutDenominationBinding =
        DataBindingUtil.inflate(
            inflater, R.layout.layout_denomination, null, false
        )
layout.addView(mBindingDeno.root)

这里的布局是你的 LinearLayout

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fillViewport="true"
                android:nestedScrollingEnabled="true"
                android:visibility="gone">

                <LinearLayout
                    android:id="@+id/linear_denomination"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" />


            </ScrollView>
于 2020-05-13T15:09:27.140 回答