Android Data Binder bug
Following the official tutorial to learn how to use data binding, the function is indeed very powerful. This is a huge step for Android to become MVVM and a small step for Native development to move closer to the Web.
One of the binding methods is to directly use resource data, for example:
android:padding="@{large? @dimen/largePadding : @dimen/smallPadding}"
Official Tutorial:
The layout file of the full version is as follows:
largePaddingAndsmallPaddingAll are defined indimens.xmlFile.
20dp
5dp
Bind the large variable in the Java code and assign the valueture.
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ResourceBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_resource); binding.setLarge(true);}
Theoretically, this should be fine, but an error occurs during the Run project. The error message is as follows:
cannot find the setter for attribute 'android:padding' on android.widget.TextView with parameter type float.
It seems like DataBinder@dimen/largePaddingResolvedfloatType, you can try type conversion:
android:padding="@{large? (int)@dimen/largePadding : (int)@dimen/smallPadding}"
After compilation is passed, the running result is correct. It should be a bug in DataBinder. The original resource binding has such advanced usage, and type conversion can be performed directly-(int)@dimen/largePadding.
It feels amazing. You must read the source code to understand how DataBinder works.