android – Kotlin合成扩展和几个包含相同的布局

前端之家收集整理的这篇文章主要介绍了android – Kotlin合成扩展和几个包含相同的布局前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果我有如下布局,如何使用kotlin合成扩展访问视图:

文件:two_days_view.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical">
  6.  
  7. <include
  8. android:id="@+id/day1"
  9. layout="@layout/day_row"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content" />
  12.  
  13. <include
  14. android:id="@+id/day2"
  15. layout="@layout/day_row"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content" />
  18. </LinearLayout>

file:day_row.xml

  1. <LinearLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="vertical" >
  5.  
  6. <TextView
  7. android:id="@+id/dayName"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content" />
  10.  
  11. </LinearLayout>

如何访问dayName?我找了一些这样的:

  1. day1.dayName.text = "xxx"
  2. day2.dayName.text = "sss"

我在Studio中看到我可以访问dayName但是dayName TextView引用了哪一个?

正常,如果我只有一个包含的布局,它工作正常.但现在我有多次包含相同的布局.

我当然可以这样做:

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

但我正在寻找好的解决方案.

猜你在找的Android相关文章