Geo*_*rge 5 java data-binding android android-studio android-databinding
我正在尝试制作一种tick-tac-toe android应用程序(尽管具有4x4网格)。我决定使用按钮来描绘每个网格正方形,并希望使用数据绑定将按钮上的文本绑定到String [] []数组中的数据,该数组内部表示网格。我尝试做一些类似于http://www.vogella.com/tutorials/AndroidDatabinding/article.html的内容,因此创建了此类:
public class ModelJoc extends BaseObservable{
private String[][] tabla_joc;
public ModelJoc() {
for (String[] line : tabla_joc)
for (String element : line)
element = "";
tabla_joc[0][0] = "M";
tabla_joc[0][1] = "W";
}
Run Code Online (Sandbox Code Playgroud)
然后将数据绑定添加到activity_main.xml中:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tgeorge.temajoc.MainActivity">
<data>
<variable
name="state"
type="com.example.tgeorge.temajoc.ModelJoc"/>
</data>
Run Code Online (Sandbox Code Playgroud)
然后尝试将按钮的文本设置为数组中的值:
<Button
android:id="@+id/button1"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="@={state.getBlockState()[0][0]}"/>
Run Code Online (Sandbox Code Playgroud)
但这给了我这些错误:“这里不允许元素数据”以及“属性缺少android:前缀”。我无法从教程中的示例中看出我在做错什么,所以问题是我应该将它们放在哪里?
我想我现在看到了问题。您必须使用<layout>标签概述布局:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.tgeorge.temajoc.MainActivity">
<data>
<variable
name="state"
type="com.example.tgeorge.temajoc.ModelJoc"/>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- ... -->
<Button
android:id="@+id/button1"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="@{state.getBlockState()[0][0]}"/>
Run Code Online (Sandbox Code Playgroud)
如果您不这样做,Android数据绑定将不会将其识别为数据绑定布局文件。
| 归档时间: |
|
| 查看次数: |
2633 次 |
| 最近记录: |