java - Textview中的Android条件字符串,未评估数据绑定
问题描述
我现在已经搜索了网络,但没有找到解决我这个问题的方法。
我的问题是我有一个TextView
并且我想在其中使用该android:text
属性显示文本。@string
应根据FLAVOR
正在运行的内容以有条件的方式从中获取此文本。
我已经将我的 xml 布局文件包装在<layout></layout>
标签中,并且在这些标签中我导入了一些类:
<data>
<import type="esy.es.matmatt.pidroCounter.BuildConfig"/>
<import type="android.view.View"/>
</data>
要根据 FLAVOR 有条件地在 Textview 中显示不同的文本,我有以下 TextView:
<TextView
android:text="@{String.valueOf(BuildConfig.FLAVOR.equals(`free`) ? @string/about_text : @string/about_textPro)}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textViewFree"
android:layout_below="@+id/aboutTitle"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textAlignment="center"
android:autoLink="email"
android:textColorLink="@color/textn"/>
我不知道为什么这只是输出一个空白的TextView...它一个都不显示...
我在这篇文章中读到我应该在@之后有一个=符号,但是当我添加它时,我只是得到了一个编译错误:
M:\Git Repositories\PidroCounter\app\build\generated\ap_generated_sources\freeDebug\out\esy\es\matmatt\pidroCounter\DataBinderMapperImpl.java:10: error: cannot find symbol import esy.es.matmatt.pidroCounter.databinding.FragmentAboutBindingImpl;
^
我不能简单地找到错误。我使用的导入和条件语法似乎在广告视图的可见性上工作得很好,但在 TextViews 上根本不行?我不知道我做错了什么。有任何想法吗?谢谢!
编辑:
实际上我现在意识到这些条件在广告视图中也不起作用(XML)。我认为这是因为我也有条件地以MobileAds
编程方式加载......
解决方案
我认为您的问题存在于您编写的绑定表达式中,因为使用了String.valueOf
. 它应该是
android:text="@{BuildConfig.FLAVOR.equals(`free`) ? @string/about_text : @string/about_textPro}"
如果这不起作用,请打开数据绑定生成的BindingImpl.java
(例如:)类。YourLayoutNameBindingImpl.java
搜索您的视图 ID ( textViewFree
) 并在此处发布生成的代码。
推荐阅读
- javascript - 使用 CORS 访问其他站点 cookie
- java - 从数组中查找奇数或偶数并将值插入所需的数组。没有得到想要的输出
- django - 覆盖 Django LoginView
- php - 动态博客在本地工作,但在服务器博客页面上显示显示 http 错误 500
- xcode - 尝试构建库时,Ninja 中的 Sysroot 不正确
- sql - 数组的SQL过滤元素
- r - R 传单。将点数据分组到单元格中以汇总许多数据点
- oracle - oracle安装后sqlplus不显示
- flask - 未找到 在服务器上未找到请求的 URL。如果您手动输入了 URL,请检查您的拼写并重试
- memory-management - 内核进程kswapd是如何一步步启动的?