Kotlin 合成扩展和几个包含相同的布局
Kotlin synthetic extension and several include same layout
如果我有如下布局,如何使用 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>
文件:day_row.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/dayName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
如何获取dayName?我找了一些这样的:
day1.dayName.text = "xxx"
day2.dayName.text = "sss"
我在 Studio 中看到我可以访问 dayName
但引用的是 dayName TextView 中的哪一个?
正常,如果我只有一个包含的布局,它工作正常。但是现在我多次包含相同的布局。
我当然可以:
day1.findViewById(R.id.dayName).text = "xxx"
但我正在寻找好的解决方案。 :)
作为一般经验法则,您不应构建最终具有多个具有相同 ID 的视图的布局 - 正是出于这个原因。
但是,要解决您的问题:
而不是导入
kotlinx.android.synthetic.main.layout.day_row.*
你可以导入
kotlinx.android.synthetic.main.layout.day_row.view.*
(注意末尾附加的 .view
)。
这不会将视图作为 Activity/Fragment 级别的属性导入,而是作为 View
的扩展属性导入。这样,您可以按照自己的方式进行,假设 day1
和 day2
包含您想要的视图:
day1.dayName.text = "xxx"
day2.dayName.text = "sss"
在这种情况下,使用:
(day1 as TextView).text = ""
(day2 as TextView).text = ""
如果我有如下布局,如何使用 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>
文件:day_row.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/dayName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
如何获取dayName?我找了一些这样的:
day1.dayName.text = "xxx"
day2.dayName.text = "sss"
我在 Studio 中看到我可以访问 dayName
但引用的是 dayName TextView 中的哪一个?
正常,如果我只有一个包含的布局,它工作正常。但是现在我多次包含相同的布局。
我当然可以:
day1.findViewById(R.id.dayName).text = "xxx"
但我正在寻找好的解决方案。 :)
作为一般经验法则,您不应构建最终具有多个具有相同 ID 的视图的布局 - 正是出于这个原因。
但是,要解决您的问题: 而不是导入
kotlinx.android.synthetic.main.layout.day_row.*
你可以导入
kotlinx.android.synthetic.main.layout.day_row.view.*
(注意末尾附加的 .view
)。
这不会将视图作为 Activity/Fragment 级别的属性导入,而是作为 View
的扩展属性导入。这样,您可以按照自己的方式进行,假设 day1
和 day2
包含您想要的视图:
day1.dayName.text = "xxx"
day2.dayName.text = "sss"
在这种情况下,使用:
(day1 as TextView).text = ""
(day2 as TextView).text = ""