3
如何访问使用科特林合成的扩展,如果我有一个布局像下面查看:科特林合成的延伸和几个包括相同的布局
文件: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"
我在工作室,我有机会获得dayName
但看到这DAYNAME的TextView之一是参考?
正常,如果我只有一个包括的布局,它工作正常。但现在我有多次包括相同的布局。
当然我总是可以做:
day1.findViewById(R.id.dayName).text = "xxx"
,但我正在寻找很好的解决方案。 :)
THX它wokrs。 顺便说一句:你能解释为什么不应该以这种方式构建布局?如果我有固定相同的7行?为什么我应该使用名称day(1..7)名称等来创建一个巨大的xml。任何差异的方式来避免巨大的XML? – LunaVulpo
@LunaVulpo这更像是来自我身边的“免责声明”。我认为在你的情况下,对于固定的一组相同的布局,它可能是有保证的,但在很多情况下,其他解决方案可能更适合,比如使布局成为片段或自定义视图。我意识到这在技术上仍然将相同ID的视图放置在相同的结果布局中,但这样它们在代码中完全分离。 – Robin
@ Hikaaru755好的。谢谢 :) – LunaVulpo