A bug in Android Data Binder

Source: Internet
Author: User
<span id="Label3"></p><p><p>Learning the usage of data binding with the official tutorials, which is really powerful, is a big step for Android toward MVVM and a small step toward the Web as Native's development approaches.</p></p><p><p>One way to bind is to use resource data directly, for Example:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs perl">android:padding=<span class="hljs-string">"<span class="hljs-subst">@{large? <span class="hljs-variable">@dimen</span>/largePadding : <span class="hljs-variable">@dimen</span>/smallPadding}</span>"</span></code></pre></pre><p><p>Official tutorials:</p></p><p><p></p></p><p><p>The full version of the layout file is as Follows:</p></p><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">layout</span> <span class="hljs-attribute">xmlns:android</span>=<span class="hljs-value">"http://schemas.android.com/apk/res/android"</span>></span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">data</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"resourcebinding"</span>></span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">variable</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"large"</span> <span class="hljs-attribute">Type</span>=<span class="hljs-value">"boolean"</span> /></span></span> <span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">Data</span>></span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">linearlayout</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"match_parent"</span><span class="hljs-attribute">android:layout_ Height</span>=<span class="hljs-value">"match_parent"</span>> </span></span> <span class="hljs-tag"><span class="hljs-tag"><<span class="hljs-title">TextView</span><span class="hljs-attribute">android:padding</span>=<span class="hljs-value">"@{large? @dimen/largepadding: @dimen/smallpadding} "</span><span class="hljs-attribute">android:background</span>=<span class="hljs-value">" @android: color/black "</span> <span class="hljs-attribute"> Android:textcolor</span>=<span class="hljs-value">"@android: color/white"</span><span class="hljs-attribute">android:layout_width</span>=<span class="hljs-value">"wrap_content"</span> <span class="hljs-attribute">android:layout_height</span>=<span class="hljs-value">"wrap_content"</span><span class="hljs-attribute">android:text</span>=<span class="hljs-value">"@string/hello_world" </span>/> </span></span> <span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">linearlayout</span>></span></span><span class="hljs-tag"><span class="hljs-tag"></<span class="hljs-title">layout</span>></span></span></code></pre><p><p><code>largePadding</code>And <code>smallPadding</code> both are resource data that are defined in <code>dimens.xml</code> the File.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-tag"><<span class="hljs-title">dimen</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"largePadding"</span>></span>20dp<span class="hljs-tag"></<span class="hljs-title">dimen</span>></span><span class="hljs-tag"><<span class="hljs-title">dimen</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"smallPadding"</span>></span>5dp<span class="hljs-tag"></<span class="hljs-title">dimen</span>></span></code></pre></pre><p><p>In Java code, bind the large variable and assign the value to <code>ture</code> .</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"><span class="hljs-annotation">@Override</span><span class="hljs-keyword">protected</span><span class="hljs-keyword">void</span><span class="hljs-title">onCreate</span>(Bundle savedInstanceState) { <span class="hljs-keyword">super</span>.onCreate(savedInstanceState); ResourceBinding binding = DataBindingUtil.setContentView(<span class="hljs-keyword">this</span>, R.layout.activity_resource); binding.setLarge(<span class="hljs-keyword">true</span></code></pre></pre><p><p>theoretically, this should not be a problem, but when the Run works, there are errors, the error message is as Follows:</p></p><pre><pre>Cannot find the setter for attribute ' android:padding ' in Android.widget.TextView with parameter type Float.</pre></pre><p><p>Looks like a databinder <code>@dimen/largePadding</code> . to parse into a <code>float</code> type, you can try the type conversion:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs perl">android:padding=<span class="hljs-string">"<span class="hljs-subst">@{large? (<span class="hljs-keyword">int</span>)<span class="hljs-variable">@dimen</span>/largePadding : (<span class="hljs-keyword">int</span>)<span class="hljs-variable">@dimen</span>/smallPadding}</span>"</span></code></pre></pre><p><p>Compile through, Run the result is also correct, should be databinder out a bug, original resource binding also has so advanced usage, can directly carry on type conversion- <code>(int)@dimen/largePadding</code> .</p></p><p><p>Feel good magic, must read the source code, understand the DataBinder implementation Principle.</p></p> <p><p>A bug in Android Data Binder</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.