Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load value from dimens.xml and make it negative

Tags:

android

xml

In an xml document, is there any way to load a value from dimens.xml and make it negative? For example, if I have:

<dimen name="x">20dp</dimen>

Is it possible to do something like this:

android:layout_marginRight="-@dimen/x"

So I in de facto get:

android:layout_marginRight="-20dp"
like image 607
Velixo Avatar asked Jun 16 '14 13:06

Velixo


1 Answers

That is not possible in standard layout XML. You could retrieve the dimension value in Java, multiply it by -1, and apply it in Java.

You could even do that in the form of a custom attribute (e.g., yourapp:layout_negativeMarginRight) in your own custom subclass of ViewGroup, if you really wanted. This would seem to be overkill.


UPDATE: This is now somewhat possible via data binding expressions:

android:padding="@{0.75f * @dimen/icon}"

It does not work for margins or any other layout attributes due to the lack of binding adapters which you can implement. See this bug.

like image 191
CommonsWare Avatar answered Oct 03 '22 19:10

CommonsWare