Gue*_*OCs 3 android android-layout
我在我的活动中这样做:
<include
android:id="@+id/withGyroLayout"
layout="@layout/with_gyro_layout"/>
Run Code Online (Sandbox Code Playgroud)
哪里with_gyro_layout.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.util.FixedTransformerViewPager
android:id="@+id/viewPagerTop"
android:layout_width="0dp"
android:layout_height="143dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.example.util.FixedTransformerViewPager
android:id="@+id/viewPagerBottom"
android:layout_width="0dp"
android:layout_height="143dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/viewPagerTop" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
但是,我无法访问元素viewPagerBottom或viewPagerTop活动的绑定:
binding.viewPagerBottom.setVisibility(View.VISIBLE);
binding.viewPagerTop.setVisibility(View.VISIBLE);
Run Code Online (Sandbox Code Playgroud)
我试着摆弄with_gyro_layout.xml,<merge>...</merge>但也没有解决。
我希望能够以编程方式在with_gyro_layout.xml和 之间进行更改without_gyro_layout.xml,并通过绑定访问其内部元素。我怎样才能做到这一点?
为了将 ViewBinding 与包含的布局一起使用,需要做两件事。
<merge>不支持该文档仅涵盖Data Binding,而不涵盖 View Binding ,但它似乎确实适用于两者。请参阅https://developer.android.com/topic/libraries/data-binding/expressions#includes
数据绑定不支持包含为合并元素的直接子元素。
换句话说,布局必须有一个真实、具体的视图作为其根元素。支持以下内容:
<LinearLayout ...>
<TextView ... />
<TextView ... />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
<merge>但不支持具有根的布局:
<merge ...>
<TextView ... />
<TextView ... />
</merge>
Run Code Online (Sandbox Code Playgroud)
<include>必须指定 ID一般来说,可以在不显式指定 ID 的情况下包含布局。视图绑定不支持此:
<include layout="@layout/included_layout"/>
Run Code Online (Sandbox Code Playgroud)
即使包含的布局在其根元素上有一个 ID,它仍然不受支持。相反,您必须在标签上显式指定 ID <include>:
<include
android:id="@+id/some_id"
layout="@layout/included_layout"/>
Run Code Online (Sandbox Code Playgroud)
一旦满足这两个条件,您生成的外部布局绑定将包括对所包含布局的绑定的引用。假设我们的两个文件是outer_layout.xml和included_layout.xml。然后会生成这两个文件:
OuterLayoutBinding.javaIncludedLayoutBinding.java您可以像这样引用包含的视图:
val outerBinding = OuterLayoutBinding.inflate(layoutInflater)
val innerBinding = binding.someId // uses the id specified on the include tag
val innerView = innerBinding.viewPagerTop
Run Code Online (Sandbox Code Playgroud)
或者,简称:
val binding = OuterLayoutBinding.inflate(layoutInflater)
val innerView = binding.someId.viewPagerTop
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1105 次 |
| 最近记录: |