1.visibility的使用錯誤
<ProgressBar
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_weight="1"
android:layout_gravity="center"
android:visibility="@{!viewModel.isLoaded.get}"/>
其中的
android:visibility="@{!viewModel.isLoaded.get}"應該修改爲
android:visibility="@{!viewModel.isLoaded.get ? View.VISIBLE : View.GONE}"
2.使用view而未引用view
添加一行import,引入View進來:
3.xml中data標籤只是綁定model的聲名,切記要在代碼中進行數據綁定:
首先在xml內
然後是在代碼內設置數據進行數據綁定
4.可以在表達式中直接引用帶 id 的 view,引用時採用駝峯命名法。
<TextView
android:id="@+id/first_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={user.firstName}" />
<TextView
android:text="@{user.lastName}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="@{firstName.getVisibility() == View.GONE ? View.GONE : View.VISIBLE}" />
<!-- 這裏TextView直接引用第一次TextView,firstName爲id 的駝峯命名 -->
5.在使用應用命名空間的佈局中,變量可以傳遞到任何 include 佈局中,並有兩種方法
分別是bind和app
在include內
在include的引用文件中接收
6.在xml中使用DataBinding拼接字符串時,直接用中文拼接
報錯:
Error:Execution failed for task ‘:app:compileDebugJavaWithJavac’.
android.databinding.tool.util.LoggedErrorException: failure, see logs for details.
Exception while handling step android.databinding.annotationprocessor.ProcessExpressions@7cf285d5 javax.xml.bind.UnmarshalException- with linked exception:
[org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:333)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:563)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:249)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:204)
at android.databinding.tool.store.ResourceBundle
只需要將中文寫在string.xml中,從string.xml中獲取中文來拼接就行!!!(注:只要不是直接用中文拼接就行,其他方式如將中文寫成變量引入,也是可以的!!!)