【发布时间】:2016-12-11 19:17:15
【问题描述】:
https://developer.android.com/topic/libraries/data-binding/index.html:
数据绑定库提供了灵活性和广泛性 兼容性——它是一个支持库,所以你可以使用它 Android 平台版本回溯到 Android 2.1(API 级别 7+)。
但是当我尝试在我的项目中使用数据绑定时,它不起作用,对此没有任何反应,TextViews 是空的,onClick 绑定不起作用x,请帮助我我需要对 android 4.1 的支持,谷歌告诉它应该是可能的,即使是 2.1,我不认为他们在撒谎:/
示例代码:
<FrameLayout
android:layout_width="130dp"
android:layout_height="0dp"
android:layout_weight="15"
android:layout_gravity="center"
android:background="@color/colorSecondary"
android:clickable="true"
android:onClick="@{ () -> view.save() }" <!-- binding -->
android:layout_margin="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/save"
android:textAllCaps="true"
android:textColor="@color/colorForeground"/>
</FrameLayout>
后面的代码:
public void save()
{
if(viewModel.car.save() != 0) //If you put breakpoint here it won't be hit
getLayoutRoot().goBack();
}
代码在 API 级别 19+ 上运行良好
【问题讨论】:
-
它是否适用于其他 API 级别?
-
请贴出您目前尝试过的代码。
-
是的,完全在 19+ API 级别上工作正常。
-
尝试使用 AppcompatTextView
-
@Harsh 现在是问题所在,这是对我不起作用的示例代码。
标签: android data-binding binding android-databinding