android - Error when getting a dimension in theme for margins -


background

i'm trying set custom margin value on listview items based on selected theme.

the app has multiple themes, , user can choose theme use , set calling "settheme()" .

the problem

whatever try, error:

java.lang.unsupportedoperationexception: can't convert dimension: type=0x2 

note occurs margin attribute, , far no other attribute has caused this.

what i've tried

first, here's xml snippets i've used

attrs.xml

<attr name="listview_item__horizontal_spacing" format="dimension" /> 

styles.xml

<style name="apptheme_holodark" parent="@style/theme.sherlock">   <item name="listview_item__horizontal_spacing">4dp</item>   .... 

the layout of listview item:

<relativelayout ...     android:layout_marginleft="?attr/listview_item__horizontal_spacing" > 

i've tried using "reference" attribute type, , reference "dimen" resource, cause same exception.

another thing i've tried getting dynamically:

public static int getresidfromattribute(final activity activity,final int attr)     {     final typedvalue typedvalueattr=new typedvalue();     activity.gettheme().resolveattribute(attr,typedvalueattr,true);     return typedvalueattr.resourceid;     }  ... final int spacingresid=getresidfromattribute(activity,r.attr.listview_item__horizontal_spacing); 

but reason 0 result of call. when using this method worked.

the question

what going on? how can avoid this?

is there no way overcome using code (when inflating xml) ?

you don't need use attr xml, , issue.

place value want in file named dimens.xml in res folder (same location strings.xml).

this file this:

<resources>     <dimen name="value1">15dp</dimen>     <dimen name="value2">20dp</dimen> </resources> 

then in layout xml, can reference dimen directly (just reference string), this:

<relativelayout ...     android:layout_marginleft="@dimen/value1" > 

or when defining style, in xml like:

 <style name="mystyle1">         <item name="android:layout_marginleft">@dimen/value1</item>     </style> 

and other style:

<style name="mystyle2">             <item name="android:layout_marginleft">@dimen/value2</item>         </style> 

Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -