根据条件在 XML 中包含布局

我有带数据绑定的父布局。我想根据逻辑添加子布局。 我希望需要类似的代码:

    <include
        android:id="@+id/layout_id"
        layout="@{model.isOk ? @layout/layout_container_1 : @layout/layout_container_2}"
        app:model="@{model}" />

但我得到错误:

android.databinding.tool.processing.ScopedException: [databinding] {"msg":"included value (@{model.isOk ? @layout/layout_container_1 : @layout/layout_container_2}) must start with @layout/.","file":"x.xml","pos":[]}

是否可以通过“if-​​else”条件动态包含子布局?如果是,最好的方法是什么?欢迎任何样品!

jiedong0827 回答:根据条件在 XML 中包含布局

这是不可能的。 <include> 标记是在布局膨胀过程中解析的,而数据绑定是您在布局成功膨胀后可以使用的。

,

是否可以通过“if-​​else”条件动态包含子布局?

到目前为止,这是不可能的,因为在数据绑定工作之前,layout 属性必须指向某个布局:

(数据绑定) 顾名思义,就是将数据绑定到一个布局上,所以必须先有一个布局,才能绑定数据。

换句话说,layout="@{model.isOk ? @layout/layout_container_1 : @layout/layout_container_2}" 不起作用,因为在检查 model.isOk 之前应该有一个现有的布局,而且 layout 属性需要在 {{1} 之后的布局资源}} 符号。

所以,we need to do that programmatically instead of the XML layout。这可以通过将 @ 替换为 ViewStub 来完成,这是一种在活动中完成的延迟布局膨胀。

因此在对 <include> 布局进行膨胀之前,您可以先检查一下 Activity 中的 ViewStub 是否存在,然后再决定可以在 model.isOk 上膨胀哪种布局。

因此基于 ViewStub 值,我们可以使用 model.isOk

设置布局

最终我们可以使用 binding.myLayoutStub.myViewStub?.layoutResource = R.layout.layout_container_1 方法来扩充新的 ViewStub 布局。

使用 binding.myLayoutStub.myViewStub?.inflate(),将使您拥有一对绑定对象,一个用于活动(或片段),另一个用于 ViewStub 的膨胀布局。

这里有一个像你这样的例子:

activity_main.xml:

ViewStub

我们有 <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <data> <variable name="model" type="com.example.android.databindingexample.ActivityViewModel" /> </data> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ViewStub android:id="@+id/layout_stub" android:layout_width="match_parent" android:layout_height="match_parent"/> </androidx.constraintlayout.widget.ConstraintLayout> </layout> layout_container_1,它们仅在 textview 值上有所不同:

layout_container_1.xml:

layout_container_2

视图模型:

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/purple_200">

        <TextView
            android:id="@+id/textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Layout 1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

然后根据 class ActivityViewModel : ViewModel() { var isOk = true } 决定 Activity 中的哪个布局,每当您膨胀 ViewStub 时:

model.isOk

预览:

enter image description here

本文链接:https://www.f2er.com/26857.html

大家都在问