android - Android 中的静态数据绑定(bind) [文字]

标签 android android-databinding

我创建了包含图像和标题的自定义布局。为了重用这个布局,我使用了 <include>标签。问题是我什至无法将字符串文字绑定(bind) 到包含的布局中。我试着按照这些instructions , 但没有成功。

layout/titlebar.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="title" type="String"/>
        <!-- <variable name="imgSrc" type="android.graphics.drawable.Drawable" /> -->
    </data>
    <LinearLayout ... >
        <!-- <ImageView ... android:src="{imgSrc}" /> -->
        <TextView ... android:text="@{title, default=DefaultTitle}" />
    </LinearLayout>
</layout>

layout/otherlayout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:bind="http://schemas.android.com/apk/res-auto"
              ... 
              >
    <!-- bind:imgSrc="@{@drawable/some_image}" -->
    <include layout="@layout/titlebar"
             bind:title="@{Example}"  <---------- does not work 
             />
    ...
</LinearLayout>

在 gradle 中,我为模块启用了数据绑定(bind):

android {
    ...
    dataBinding {
        enabled = true
    }
    ...
}

最佳答案

根据@CzarMatt 的回答修复了 layout/otherlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 
layout with bindings has to be wrapped in <layout> tag so {LayoutName}Bindings 
class can be auto-generated for binding purposes 

xmlns:alias="http://schemas.android.com/apk/res-auto" 
creates an "app namespace" for custom attributes
-->
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:bind="http://schemas.android.com/apk/res-auto">
    <LinearLayout  ... >
        <!-- 
        // if this layout is also using title "data variable" 
        // and we want to use default value if title is null
        bind:title='@{title ?? "Settings"} 

        // passing literal reference into the binding
        bind:title="@{@string/settings_constant}"
        -->
        <include layout="@layout/titlebar"
                 bind:title='@{"Settings"}'
                 />
        ...
    </LinearLayout>
</layout>

数据绑定(bind)需要按照@RaviRupareliya 的建议通过DataBindingUtil 设置布局,否则数据绑定(bind)将不起作用:

public class OtherActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        // setContentView(R.layout.otherlayout);
        DataBindingUtil.setContentView(this, R.layout.otherlayout);
    }
    ...
}

关于android - Android 中的静态数据绑定(bind) [文字],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44660660/

相关文章:

android - 这个嵌套的 android 布局 View 的最佳方法是什么

android - 使用数据绑定(bind)和 lambda 的单选按钮 onCheckedChanged

java - 如何在 android 中使用数据绑定(bind)库绑定(bind)一个可以是 String 或 double 的变量?

java - 无法在android数据绑定(bind)中制作点击事件

android - 如何使用数据绑定(bind)初始化 Recycler View 适配器上的点击监听器?

java - Android Studio 数据绑定(bind)中此处不允许使用元素数据

android - 如何在主要 Activity 中注册接收者?

java - 防止 ProGuard 混淆类成员名称

android - 虚拟盒子安卓模拟器和Eclipse

java - 在 ConstraintLayout 中的 View 的所有方向上应用约束是最佳实践吗?