包含两次相同布局时如何使用合成属性
How to use syntethic properties when you have the same layout included twice
通常,当我需要在一个屏幕中包含相同的布局时,我会将此布局包装在两个具有不同 ID 的不同容器中,然后对这两个父级调用 findViewById
,但我不知道是否以及我如何使用 kotlin 的 syntethic
属性获得相同的结果。更清楚一点:
我的情况是fragment_xyz.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="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/include1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<include layout="@layout/item_detail" />
</FrameLayout>
<FrameLayout
android:id="@+id/include2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<include layout="@layout/item_detail" />
</FrameLayout>
</LinearLayout>
item_detail.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="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
我想按常规访问 title 和 description:
import kotlinx.android.synthetic.main.item_detail.*
但是我需要区分FrameLayout里面的include1和FrameLayout include2
提前致谢!
编辑:打字错误。
您需要导入:
import kotlinx.android.synthetic.main.fragment_xyz.*
然后从 framelayout
include1
和 include2
中获取标题和描述,如下所示:
val title1 by lazy {
include1.title
}
val description1 by lazy {
include1.description
}
val title2 by lazy {
include2.title
}
val description2 by lazy {
include2.description
}
通常,当我需要在一个屏幕中包含相同的布局时,我会将此布局包装在两个具有不同 ID 的不同容器中,然后对这两个父级调用 findViewById
,但我不知道是否以及我如何使用 kotlin 的 syntethic
属性获得相同的结果。更清楚一点:
我的情况是fragment_xyz.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="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/include1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<include layout="@layout/item_detail" />
</FrameLayout>
<FrameLayout
android:id="@+id/include2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<include layout="@layout/item_detail" />
</FrameLayout>
</LinearLayout>
item_detail.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="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
我想按常规访问 title 和 description:
import kotlinx.android.synthetic.main.item_detail.*
但是我需要区分FrameLayout里面的include1和FrameLayout include2
提前致谢!
编辑:打字错误。
您需要导入:
import kotlinx.android.synthetic.main.fragment_xyz.*
然后从 framelayout
include1
和 include2
中获取标题和描述,如下所示:
val title1 by lazy {
include1.title
}
val description1 by lazy {
include1.description
}
val title2 by lazy {
include2.title
}
val description2 by lazy {
include2.description
}