【发布时间】:2014-04-20 14:26:26
【问题描述】:
背景
我正在尝试根据所选主题在 listView 项目上设置自定义边距值。
应用程序有多个主题,用户可以选择使用哪个主题,我通过调用“setTheme()”来设置。
问题
无论我尝试什么,我都会收到此错误:
java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2
请注意,这只发生在边距属性上,到目前为止还没有其他属性导致这种情况。
我尝试过的
首先,这是我用过的xml sn-ps
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>
....
listView 项的布局:
<RelativeLayout ...
android:layout_marginLeft="?attr/listview_item__horizontal_spacing" >
我也尝试使用“reference”作为属性类型,并引用“dimen”资源,但这也会导致同样的异常。
我尝试过的另一件事是动态获取它:
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);
但由于某种原因,我得到 0 作为这个调用的结果。只有在使用this method 时才有效。
问题
发生了什么事?我怎样才能避免这种情况?
真的没有办法克服这个问题,但使用代码(膨胀 xml 时)?
【问题讨论】:
标签: android android-theme margins dimension