Kotlin synthetic extension and several include same layout

后端 未结 2 1508
傲寒
傲寒 2021-02-07 06:58

How to access to view using kotlin synthetic extension if I have a layout like below:

file:two_days_view.xml



        
相关标签:
2条回答
  • 2021-02-07 07:42

    As a general rule of thumb, you should not construct layouts that end up having multiple views with the same id - for this very reason.

    But, to solve your problem: Instead of importing

    kotlinx.android.synthetic.main.layout.day_row.*

    you can import

    kotlinx.android.synthetic.main.layout.day_row.view.* (Notice the additional .view at the end).

    This will import the views not as properties on the Activity/Fragment level, but instead as extension properties for View. That way, you can do it the way you want, assuming that day1 and day2 contain the views you want:

    day1.dayName.text = "xxx"
    day2.dayName.text = "sss"
    
    0 讨论(0)
  • 2021-02-07 07:51

    In this case, use:

    (day1 as TextView).text = ""
    (day2 as TextView).text = ""
    
    0 讨论(0)
提交回复
热议问题