如何设置数据绑定中包含布局的可见性? [英] How to set visibility for include layout in databinding?

查看:41
本文介绍了如何设置数据绑定中包含布局的可见性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的项目中实现了数据绑定.我有一个特定的屏幕,在 include 标签中有两个嵌套布局.我无法以编程方式使用数据绑定更改包含布局的可见性.

I have implemented data binding in my project. I have a particular screen with two nested layouts in include tags. I couldn't change the visibility for include layouts using data binding programmatically.

但是,我是通过布尔值实现的,但我的问题是如何以编程方式设置包含标记的可见性.

However, I have achieved it through a boolean, but my question is how to set visibility for that include tag programmatically.

我的 xml:

<include
  android:id="@+id/reg_email"
  layout="@layout/custom_email"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>


<include
  android:id="@+id/reg_phone"
  layout="@layout/custom_phone"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

在活动中:当我尝试设置它时 - 它会变成红色,这意味着它不会将其视为视图.

And in Activity: when I try to set this - it becomes red meaning it doesn't take it as a view.

  dataBinding.regPhone.setVisibility(View.GONE);
  dataBinding.regEmail.setVisibility(View.VISIBLE);

推荐答案

add get root to your view

add get root to your view

dataBinding.regPhone.getRoot().setVisibility(View.GONE);
dataBinding.regEmail.getRoot().setVisibility(View.VISIBLE);

这篇关于如何设置数据绑定中包含布局的可见性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆