Android 使用DataBinding跟include问题

ChansEbm ChansEbm 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
直接上代码吧

test.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">


<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<include
android:id="@+id/include_content"
layout="@layout/content" />

</FrameLayout>

</layout>



content.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"/>
</FrameLayout>

</layout>



java 代码:
TestBinding binding;

@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
binding = (TestBinding) viewDataBinding;
Button button = binding.includeContent.button;
}


运行到Button button = binding.includeContent.button;的时候 报错


Error:(11, 39) 错误: 找不到符号
符号: 类 Test1Binding
位置: 程序包 com.appbaba.digital.databinding
...全文

相关推荐

安卓—DataBinding的使用(三)

DataBinding 的使用(三) 目录 DataBinding 双向绑定 DataBinding 事件绑定 引用 类方法 使用 运算符 双向绑定 在以前的学习过程中,我们一般只是数据的单向流动,比如使用TextView将数据展现出来,基本就是进行“读”操作,但是在某些情况下,比如当我们使用EditText更新信息时,仅仅使用单向的数据流动是不能满足条件的,这时我们就需要使用DataBinding的双向绑定。 双向绑定的使用是十分简单的,我们只需要改变一下布局文件即可,将我们原来单向绑定的固定模式@{}改为@={}。 同样是在活动中进行初始化即可 初始化赋值 public cla

weixin_38631182

记一个jetpack databinding和include一起使用时出现的问题

1.简介 databinding可以获取当前布局的View对象 但是当前layout包含include子视图时,子视图的View元素不存在于binding上 2.解决 修改子视图,使用layout为根元素,使子视图生成binding &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns..

crayon-shin-chan

Databinding案例

展示了DataBinding的用法,基本包含了开发常用的用法

lxn_李小牛

Android中使用include标签和merge标签重复使用布局

尽管Android提供了各种组件来实现小而可复用的交互元素,你也可能因为布局需要复用一个大组件。为了高效复用完整布局,你可以使用<include>和<merge>标签嵌入另一个布局到当前布局。所以当你通过写一个自定义视图创建独立UI组件,你可以放到一个布局文件里,这样更容易复用。 复用布局因为其允许你创建可复用的复杂布局而显得非常强大。如,一个 是/否 按钮面板,或带描述文本的自定义进度条。这同样意味着,应用里多个布局里共同的元素可以被提取出来,独立管理,然后插入到每个布局里。 创建可复用布局 如果你已经知道哪个布局需要重用,就创建一个新的xml文件来定义布局。如,下面是一个来自G-Ken

weixin_38652870

Android布局技巧之使用ViewStub

多亏了<include>标签,在Android里,很容易就能做到共享和重用UI组件。在Android开发中,很容易就能创建出复杂的UI结构,结果呢,用了很多的View,且其中的一些很少使用。针对这种情况,谢天谢地,Android还为我们提供了一个特别的构件——ViewStub,它可以使你充分享受<include>的好处而不会造成无用View的浪费。 ViewStub是一个看不见的,轻量级的View。它没有尺寸,也不会绘制以及以某种形式参与到布局中来。这意味着ViewStub去inflate以及保留在View层次中的代价是很廉价的。ViewStub最佳的描述称之为“懒惰的include”

weixin_38593723
评论 7
收藏 已收藏
分享
返回顶部
ChansEbm
Android 80,489 成员 91,375 内容