如何在绑定中既包含Android布局又访问其元素?
我在我的活动中这样做:如何在绑定中既包含Android布局又访问其元素?,android,android-layout,Android,Android Layout,我在我的活动中这样做: <include android:id="@+id/withGyroLayout" layout="@layout/with_gyro_layout"/> binding.viewPagerBottom.setVisibility(View.VISIBLE); binding.viewPagerTop.setVisibility(View.VISIBLE); 但是,我无法
<include
android:id="@+id/withGyroLayout"
layout="@layout/with_gyro_layout"/>
binding.viewPagerBottom.setVisibility(View.VISIBLE);
binding.viewPagerTop.setVisibility(View.VISIBLE);
但是,我无法从我的活动的绑定中访问元素viewpagerbotom
或viewPagerTop
:
<include
android:id="@+id/withGyroLayout"
layout="@layout/with_gyro_layout"/>
binding.viewPagerBottom.setVisibility(View.VISIBLE);
binding.viewPagerTop.setVisibility(View.VISIBLE);
我试着把和_gyro_layout.xml
放在…
周围,但也没有解决问题
我希望能够通过编程方式在
带_gyro_layout.xml
和不带_gyro_layout.xml
之间进行更改,并通过绑定访问其内部元素。如何才能做到这一点?要在包含的布局中使用ViewBinding,需要做两件事
不支持
本文档仅涉及数据绑定,而不涉及视图绑定,但似乎两者都适用。看
数据绑定不支持将include作为合并元素的直接子元素
换句话说,布局必须有一个真实、具体的视图作为其根元素。支持以下内容:
<LinearLayout ...>
<TextView ... />
<TextView ... />
</LinearLayout>
<merge ...>
<TextView ... />
<TextView ... />
</merge>
或者,简而言之:
val binding = OuterLayoutBinding.inflate(layoutInflater)
val innerView = binding.someId.viewPagerTop
我试图导入它,但没有。甚至尝试重新启动安德烈工作室。而且,即使它生成,它也不是活动中的,我必须手动插入它,对吗?可能吗?存在
绑定。使用GyroLayout
但绑定。使用GyroLayout.ViewPageBottom
不存在,可能我需要使用findViewById?有没有办法把它作为一种绑定呢?@GuerlandoOCs我更新了我的答案,提供了更多的细节。我能够在我自己的Android Studio中复制所有这些内容(我使用的是4.0版)。请发布完整的布局代码,包括
标记以及如何初始化绑定。
val outerBinding = OuterLayoutBinding.inflate(layoutInflater)
val innerBinding = binding.someId // uses the id specified on the include tag
val innerView = innerBinding.viewPagerTop
val binding = OuterLayoutBinding.inflate(layoutInflater)
val innerView = binding.someId.viewPagerTop