Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Kotlin综合扩建和多个扩建包括相同的布局_Android_Kotlin_Kotlin Android Extensions - Fatal编程技术网

Android Kotlin综合扩建和多个扩建包括相同的布局

Android Kotlin综合扩建和多个扩建包括相同的布局,android,kotlin,kotlin-android-extensions,Android,Kotlin,Kotlin Android Extensions,如果我有如下布局,如何使用kotlin合成扩展访问视图: 文件:two_days_view.xml <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="wrap_c

如果我有如下布局,如何使用kotlin合成扩展访问视图:

文件:two_days_view.xml

<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="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/day1"
        layout="@layout/day_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <include
        android:id="@+id/day2"
        layout="@layout/day_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
我在Studio中看到我可以访问
dayName
,但dayName TextView中的哪一个是引用的

正常,如果我只有一个包括布局它的作品很好。但现在我已经多次包含相同的布局

当然,我总能做到:

day1.findViewById(R.id.dayName).text = "xxx"

但我正在寻找好的解决方案。:)

作为一般经验法则,您不应该构建最终具有相同id的多个视图的布局-正是出于这个原因

但是,要解决您的问题: 而不是进口

kotlinx.android.synthetic.main.layout.day\u row.*

你可以导入

kotlinx.android.synthetic.main.layout.day\u row.view.*
(注意末尾的附加
.view

这将导入视图,而不是作为活动/片段级别的属性,而是作为
视图
的扩展属性。这样,您就可以按照自己的方式进行操作,假设
day1
day2
包含您想要的视图:

day1.dayName.text = "xxx"
day2.dayName.text = "sss"
在这种情况下,请使用:

(day1 as TextView).text = ""
(day2 as TextView).text = ""

谢谢。顺便问一下:你能解释一下为什么不应该用这种方式构建布局吗?如果我修正了相同的7行?为什么我要用day(1..7)name等名称创建一个巨大的xml。有什么不同的方法可以避免庞大的xml吗?@LunaVulpo这更像是我这边的“免责声明”。我认为在您的情况下,对于一组固定的相同布局,这可能是有保证的,但在许多情况下,其他解决方案可能更适合,比如将布局设置为片段或自定义视图。我确实意识到,从技术上讲,这仍然是将具有相同ID的视图放在相同的结果布局中,但这样它们在代码中就完全分开了。
day1.dayName.text = "xxx"
day2.dayName.text = "sss"
(day1 as TextView).text = ""
(day2 as TextView).text = ""