构建项目时收到以下警告
DSL element 'android.dataBinding.enabled' is obsolete and has been replaced with 'android.buildFeatures.dataBinding'.
Run Code Online (Sandbox Code Playgroud)
我在用 Android Studio Canary 6
我正在尝试从官方谷歌文档https://developer.android.com/tools/data-binding/guide.html关注数据绑定示例
除了我试图将数据绑定应用于片段,而不是活动.
编译时我目前得到的错误是
Error:(37, 27) No resource type specified (at 'text' with value '@{marsdata.martianSols}.
onCreate 对于片段看起来像这样:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MartianDataBinding binding = MartianDataBinding.inflate(getActivity().getLayoutInflater());
binding.setMarsdata(this);
}
Run Code Online (Sandbox Code Playgroud)
onCreateView 对于片段看起来像这样:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.martian_data, container, false);
}
Run Code Online (Sandbox Code Playgroud)
我的片段布局文件的部分内容如下所示:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="marsdata"
type="uk.co.darkruby.app.myapp.MarsDataProvider" />
</data>
...
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@{marsdata.martianSols}"
/>
</RelativeLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
我怀疑是MartianDataBinding不知道它应该绑定哪个布局文件 - 因此错误.有什么建议?
我使用新的数据绑定库在Java中编写了以下片段类
import com.example.app.databinding.FragmentDataBdinding;
public class DataFragment extends Fragment {
@Nullable
private FragmentDataBinding mBinding;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_data, container, false);
return mBinding.getRoot();
}
}
Run Code Online (Sandbox Code Playgroud)
它编译并运行良好.
我试图在Kotlin中重写它并提出以下内容:
import com.example.app.databinding.FragmentDataBdinding
class ProfileFragment : Fragment() {
private var mBinding: FragmentDataBinding? = null
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_data, container, false)
return mBinding!!.getRoot()
}
}
Run Code Online (Sandbox Code Playgroud)
但现在步骤:app:compileDebugKotlin输出如下:
错误:(16,38)未解析的引用:数据绑定
错误:(37,27)未解析的引用:FragmentDataBinding
如何在Kotlin中使用android-databinding库?
我的顶级build.gradle:
buildscript { …Run Code Online (Sandbox Code Playgroud) 我试图使用Android数据绑定在xml中使用和"&&"运算符,
android:visibility="@{(bean.currentSpaceId == bean.selectedSpaceId **&&** bean.currentSpaceId > 0)? View.VISIBLE: View.GONE}"
但我得到了编译错误:
错误:任务':app:dataBindingProcessLayoutsDevDebug'的执行失败.org.xml.sax.SAXParseException; systemId:file:/Users/path/app/build/intermediates/res/merged/dev/debug/layout/fragment_space.xml; lineNumber:106; columnNumber:89; 实体名称必须紧跟实体引用中的"&".
安卓工作室中的红色高亮显示错误"未转义和未终止字符".
那我该怎么办?
编辑: 找到答案,这些角色需要转义:
'&' --> '&'
'<' --> '<'
'>' --> '>'
Run Code Online (Sandbox Code Playgroud) 我有一个TextView,它有一个硬编码的字符串,我有一个动态变量,我想放在这个字符串的末尾.这是我的代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<TextView
android:id="@+id/PeopleName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/Generic_Text"+"@{ Profile.name }" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我遇到了问题 android:text="@string/Generic_Text"+"@{ Profile.name }".该Generic_Text州"我的名字是",则Profile.name是动态的,并从轮廓明显的变化来分析.我想要它,以便整个TextView输出是我的名字是{Profile.name}.任何帮助都会很棒.
我刚开始使用Android Studio 3.0.0,但每次尝试构建我的项目时都会收到此错误:
Error:Circular dependency between the following tasks:
:app:compileDebugKotlin
+--- :app:dataBindingExportBuildInfoDebug
| \--- :app:compileDebugKotlin (*)
\--- :app:kaptDebugKotlin
\--- :app:dataBindingExportBuildInfoDebug (*)
(*) - details omitted (listed previously)
Run Code Online (Sandbox Code Playgroud)
我在用
kapt "com.android.databinding:compiler:2.2.0"
Run Code Online (Sandbox Code Playgroud)
在我使用之前
androidProcessor "com.android.databinding:compiler:2.2.0"
Run Code Online (Sandbox Code Playgroud)
它工作得很好......我做错了什么?
谢谢!
android kotlin android-studio android-databinding android-studio-3.0
上面的例子工作正常,因为1.0-rc4版修复了需要不必要变量的问题.
我完全按照文档中的描述进行操作,但它不起作用:
main.xml中:
<layout xmlns:andr...
<data>
</data>
<include layout="@layout/buttons"></include>
....
Run Code Online (Sandbox Code Playgroud)
buttons.xml:
<layout xmlns:andr...>
<data>
</data>
<Button
android:id="@+id/button"
...." />
Run Code Online (Sandbox Code Playgroud)
MyActivity.java:
... binding = DataBindingUtil.inflate...
binding.button; ->cannot resolve symbol 'button'
Run Code Online (Sandbox Code Playgroud)
如何获得按钮?
java data-binding android android-button android-databinding
我刚刚在Android Studio 3.2 Canary 16上创建了一个新项目,启用了Kotlin.然后我也启用了数据绑定,但是我收到一条错误,说它无法找到DataBindingComponent类.
这是我的项目gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.2.41'
ext.android_plugin_version = '3.2.0-alpha10'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0-alpha16'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)
我的模块gradle文件:
apply plugin: 'com.android.application'
apply plugin: …Run Code Online (Sandbox Code Playgroud) 自 Jetpack 发布以来,我们一直在使用DataBinding。Android 文档表明ViewBinding已添加到Android Studio 3.6 Canary 11+ 中。
我阅读了文档,但它看起来类似于DataBinding。
谁能解释一下这两个概念之间的区别?
data-binding android android-databinding android-jetpack android-viewbinding
我正在尝试使用数据绑定将drawable资源ID设置为ImageView的android:src
这是我的对象:
public class Recipe implements Parcelable {
public final int imageResource; // resource ID (e.g. R.drawable.some_image)
public final String title;
// ...
public Recipe(int imageResource, String title /* ... */) {
this.imageResource = imageResource;
this.title = title;
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="recipe"
type="com.example.android.fivewaystocookeggs.Recipe" />
</data>
<!-- ... -->
<ImageView
android:id="@+id/recipe_image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="@{recipe.imageResource}" />
<!-- ... -->
</layout>
Run Code Online (Sandbox Code Playgroud)
最后,活动类:
// ...
public class RecipeActivity extends AppCompatActivity {
public static …Run Code Online (Sandbox Code Playgroud)