【问题标题】:Static data-binding in Android [literals]Android 中的静态数据绑定 [literals]
【发布时间】:2017-06-20 18:35:34
【问题描述】:

我创建了包含图像和标题的自定义布局。为了重用这个布局,我使用了<include> 标签。问题是我什至无法 将字符串文字 绑定到包含的布局中。我试图关注这些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 中我为模块启用了数据绑定:

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

【问题讨论】:

  • 这应该可以,确保您在 Activity/Fragment 中绑定了该 xml
  • @RaviRupareliya 所以我需要绑定来自 Activity 类的数据?我虽然传递字符串常量不需要在课堂上写东西。有什么办法可以从 xml 绑定常量?
  • 我还没有尝试过,但我猜至少你需要绑定你的 xml。我仍然不确定,但您可以尝试绑定 Activity,例如 DatabindingUtil.setContentView(this,&lt;activityLayout&gt;);
  • @RaviRupareliya DatabindingUtil.setContentView(this,R.layout.otherlayout); 成功了!谢谢!

标签: android android-databinding


【解决方案1】:

基于@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>

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

public class OtherActivity extends Activity {

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

【讨论】:

    【解决方案2】:

    来自文档:

    变量可以从 使用应用程序命名空间和变量包含布局 属性中的名称

    这意味着,titlebarotherlayout XML 文件中必须包含以下数据变量:

    <data>
        <variable name="title" type="java.lang.String"/>
    </data>
    

    &lt;include&gt; 应该是这样的:

    <include layout="@layout/titlebar"
           bind:title="@{title}"/>
    

    有关详细信息,请参阅数据绑定文档:

    https://developer.android.com/topic/libraries/data-binding/index.html#includes

    【讨论】:

      猜你喜欢
      • 2012-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多