【发布时间】:2016-08-01 19:48:49
【问题描述】:
我正在使用数据绑定来绑定我的 Android 应用中的布局。
我已经设置好我的布局(my_custom.xml)并生成了绑定类(MyCustomBinding),但是Android Studio似乎并没有马上找到Binding类的.inflate(...)方法,标记一下作为一个错误(红色文本!)。
不过,代码似乎是正确的,因为它可以很好地编译并构建到 APK 中。
如何让 Android Studio 正确更新?
代码示例:
这是我的自定义视图代码:
public class MyCustomView extends FrameLayout {
public MyCustomView(Context context) {
this(context, null, 0);
}
public MyCustomView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MyCustomBinding binding = MyCustomBinding.inflate(inflater, this, true);
binding.aButton.setText("Whatever");
}
}
布局定义为:
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/a_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:padding="10dp"
android:background="#000"
android:textColor="#fff"
android:layout_gravity="center"
/>
</FrameLayout>
</layout>
这就是问题所在:(以红色突出显示)
【问题讨论】:
-
请发布您的代码。你用的是Activity还是Fragment?
-
发布“MyCutomBinding”类代码。
-
这在我看来是 Android Studio 2.2 中的一个错误。几个月来我一直在使用没有这个问题的数据绑定,直到今天升级,现在我也看到了。有bug report,如果你想关注它。
标签: android android-layout android-databinding